Skip to content

Commit aaf2547

Browse files
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.
1 parent 8750d38 commit aaf2547

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

‎Lib/imaplib.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,8 +1408,11 @@ def _check_bye(self):
14081408

14091409
def _command(self, name, *args):
14101410

1411-
if self.state not in Commands[name]:
1411+
literal = self.literal
1412+
if literal is not None:
14121413
self.literal = None
1414+
1415+
if self.state not in Commands[name]:
14131416
raise self.error("command %s illegal in state %s, "
14141417
"only allowed in states %s" %
14151418
(name, self.state,
@@ -1434,9 +1437,7 @@ def _command(self, name, *args):
14341437
raise ValueError("NUL, CR and LF not allowed in commands")
14351438
data = data + b' ' + arg
14361439

1437-
literal = self.literal
14381440
if literal is not None:
1439-
self.literal = None
14401441
if type(literal) is type(self._command):
14411442
literator = literal
14421443
else:

‎Lib/test/test_imaplib.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,6 +2326,28 @@ def test_control_characters(self):
23262326
self.assertEqual(name.encode('ascii').decode('utf-7-imap'),
23272327
f'a{c}b')
23282328

2329+
def test_control_characters_append_literal(self):
2330+
client, server = self._setup(SimpleIMAPHandler)
2331+
client.login('user', 'pass')
2332+
for bad_mailbox in (b'a\nb', b'a\rb', b'a\0b'):
2333+
with self.subTest(mailbox=bad_mailbox):
2334+
with self.assertRaises(ValueError):
2335+
client.append(bad_mailbox, None, None, b'message')
2336+
self.assertIsNone(client.literal)
2337+
typ, _ = client.select('INBOX')
2338+
self.assertEqual(typ, 'OK')
2339+
with self.subTest(case='flags'):
2340+
with self.assertRaises(ValueError):
2341+
client.append('INBOX', '(\\Flag\n)', None, b'message')
2342+
self.assertIsNone(client.literal)
2343+
typ, _ = client.select('INBOX')
2344+
self.assertEqual(typ, 'OK')
2345+
client.logout()
2346+
with self.subTest(case='illegal_state'):
2347+
with self.assertRaises(imaplib.IMAP4.error):
2348+
client.append('INBOX', None, None, b'message')
2349+
self.assertIsNone(client.literal)
2350+
23292351
def test_mutf7_mailbox_name(self):
23302352
# Without UTF8=ACCEPT a non-ASCII mailbox name is sent as modified
23312353
# UTF-7 (RFC 3501, 5.1.3). A name that is already modified UTF-7 (e.g.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix an issue in :mod:`imaplib` where a rejected :meth:`~imaplib.IMAP4.append`
2+
command left its message literal pending, causing it to be sent with the
3+
subsequent command.

0 commit comments

Comments
 (0)