Skip to content

Commit d847034

Browse files
fix
1 parent 0e15a81 commit d847034

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎Lib/imaplib.py‎

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

974974
def _command(self, name, *args):
975975

976-
if self.state not in Commands[name]:
976+
literal = self.literal
977+
if literal is not None:
977978
self.literal = None
979+
980+
if self.state not in Commands[name]:
978981
raise self.error("command %s illegal in state %s, "
979982
"only allowed in states %s" %
980983
(name, self.state,
@@ -999,9 +1002,7 @@ def _command(self, name, *args):
9991002
raise ValueError("NUL, CR and LF not allowed in commands")
10001003
data = data + b' ' + arg
10011004

1002-
literal = self.literal
10031005
if literal is not None:
1004-
self.literal = None
10051006
if type(literal) is type(self._command):
10061007
literator = literal
10071008
else:

‎Lib/test/test_imaplib.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,29 @@ def test_quoted_control_characters(self):
560560
send.assert_called_once_with(
561561
tag + b' LOGIN user ' + arg.encode('ascii') + b'\r\n')
562562

563+
def test_rejected_append_clears_literal(self):
564+
client, _ = self._setup(SimpleIMAPHandler)
565+
client.login('user', 'pass')
566+
for char in '\0\r\n':
567+
for mailbox in (f'a{char}b', f'a{char}b'.encode('ascii')):
568+
with self.subTest(mailbox=mailbox):
569+
with self.assertRaises(ValueError):
570+
client.append(mailbox, None, None, b'message')
571+
self.assertIsNone(client.literal)
572+
typ, _ = client.select('INBOX')
573+
self.assertEqual(typ, 'OK')
574+
with self.subTest(case='flags'):
575+
with self.assertRaises(ValueError):
576+
client.append('INBOX', '(\\Flag\n)', None, b'message')
577+
self.assertIsNone(client.literal)
578+
typ, _ = client.select('INBOX')
579+
self.assertEqual(typ, 'OK')
580+
client.logout()
581+
with self.subTest(case='illegal_state'):
582+
with self.assertRaises(imaplib.IMAP4.error):
583+
client.append('INBOX', None, None, b'message')
584+
self.assertIsNone(client.literal)
585+
563586
def test_append_literal_crlf(self):
564587
class AppendServer(SimpleIMAPHandler):
565588
def cmd_APPEND(self, tag, args):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Reject NUL, CR and LF in IMAP command arguments (CVE-2025-15366).
22
Other control characters in quoted strings and literal data remain allowed.
3+
Discard pending message literals when a command is rejected.

0 commit comments

Comments
 (0)