From aaf2547972bc2bb931fd68a5d02a855600c3d95f Mon Sep 17 00:00:00 2001 From: Abhijeet Jha Date: Sun, 15 Nov 2026 01:33:05 +0530 Subject: [PATCH] gh-162010: Drop pending literal on rejected imaplib.APPEND commands When IMAP4.append() is called, it stores the message payload in self.literal and calls _command('APPEND', ...). Following gh-143921 (CVE-2025-15366), if any argument contains control characters (NUL, CR, or LF), _command() raises ValueError before popping and clearing self.literal. This left self.literal pending on the client instance, attaching itself as an unexpected literal to whatever command was executed next (such as NOOP). Ingest and clear self.literal into local scope at the start of _command(), guaranteeing that any exception occurring prior to transmission drops the literal immediately and prevents session corruption or data leakage. --- Lib/imaplib.py | 7 +++--- Lib/test/test_imaplib.py | 22 +++++++++++++++++++ ...-11-15-01-20-00.gh-issue-162010.aBcDeF.rst | 3 +++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Security/2026-11-15-01-20-00.gh-issue-162010.aBcDeF.rst diff --git a/Lib/imaplib.py b/Lib/imaplib.py index d9ec48cbc21959d..a366f43e821ba0a 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -1408,8 +1408,11 @@ def _check_bye(self): def _command(self, name, *args): - if self.state not in Commands[name]: + literal = self.literal + if literal is not None: self.literal = None + + if self.state not in Commands[name]: raise self.error("command %s illegal in state %s, " "only allowed in states %s" % (name, self.state, @@ -1434,9 +1437,7 @@ def _command(self, name, *args): raise ValueError("NUL, CR and LF not allowed in commands") data = data + b' ' + arg - literal = self.literal if literal is not None: - self.literal = None if type(literal) is type(self._command): literator = literal else: diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py index 906c78f07d9c337..0acd39bd89be3ec 100644 --- a/Lib/test/test_imaplib.py +++ b/Lib/test/test_imaplib.py @@ -2326,6 +2326,28 @@ def test_control_characters(self): self.assertEqual(name.encode('ascii').decode('utf-7-imap'), f'a{c}b') + def test_control_characters_append_literal(self): + client, server = self._setup(SimpleIMAPHandler) + client.login('user', 'pass') + for bad_mailbox in (b'a\nb', b'a\rb', b'a\0b'): + with self.subTest(mailbox=bad_mailbox): + with self.assertRaises(ValueError): + client.append(bad_mailbox, None, None, b'message') + self.assertIsNone(client.literal) + typ, _ = client.select('INBOX') + self.assertEqual(typ, 'OK') + with self.subTest(case='flags'): + with self.assertRaises(ValueError): + client.append('INBOX', '(\\Flag\n)', None, b'message') + self.assertIsNone(client.literal) + typ, _ = client.select('INBOX') + self.assertEqual(typ, 'OK') + client.logout() + with self.subTest(case='illegal_state'): + with self.assertRaises(imaplib.IMAP4.error): + client.append('INBOX', None, None, b'message') + self.assertIsNone(client.literal) + def test_mutf7_mailbox_name(self): # Without UTF8=ACCEPT a non-ASCII mailbox name is sent as modified # UTF-7 (RFC 3501, 5.1.3). A name that is already modified UTF-7 (e.g. diff --git a/Misc/NEWS.d/next/Security/2026-11-15-01-20-00.gh-issue-162010.aBcDeF.rst b/Misc/NEWS.d/next/Security/2026-11-15-01-20-00.gh-issue-162010.aBcDeF.rst new file mode 100644 index 000000000000000..6e8e9605a7d7394 --- /dev/null +++ b/Misc/NEWS.d/next/Security/2026-11-15-01-20-00.gh-issue-162010.aBcDeF.rst @@ -0,0 +1,3 @@ +Fix an issue in :mod:`imaplib` where a rejected :meth:`~imaplib.IMAP4.append` +command left its message literal pending, causing it to be sent with the +subsequent command.