From 0e15a816bcc30bba68d0e11e7898c8a0cc763fbb Mon Sep 17 00:00:00 2001 From: Saakshaat Singh <97133528+saakshaat-singh@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:11:41 -0500 Subject: [PATCH 1/2] fix --- Lib/imaplib.py | 3 ++ Lib/test/test_imaplib.py | 38 +++++++++++++++++++ ...11-16-19-00-00.gh-issue-143921.imap312.rst | 2 + 3 files changed, 43 insertions(+) create mode 100644 Misc/NEWS.d/next/Security/2026-11-16-19-00-00.gh-issue-143921.imap312.rst diff --git a/Lib/imaplib.py b/Lib/imaplib.py index e337fe6471069f0..abc1c6e3adbac59 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -132,6 +132,7 @@ # We compile these in _mode_xxx. _Literal = br'.*{(?P\d+)}$' _Untagged_status = br'\* (?P\d+) (?P[A-Z-]+)( (?P.*))?' +_control_chars = re.compile(b'[\x00\r\n]') @@ -994,6 +995,8 @@ def _command(self, name, *args): if arg is None: continue if isinstance(arg, str): arg = bytes(arg, self._encoding) + if _control_chars.search(arg): + raise ValueError("NUL, CR and LF not allowed in commands") data = data + b' ' + arg literal = self.literal diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py index 4429a90050a14d8..55a440a2b0b542a 100644 --- a/Lib/test/test_imaplib.py +++ b/Lib/test/test_imaplib.py @@ -537,6 +537,44 @@ def test_unselect(self): self.assertEqual(data[0], b'Returned to authenticated state. (Success)') self.assertEqual(client.state, 'AUTH') + def test_command_control_characters(self): + client, _ = self._setup(SimpleIMAPHandler) + with mock.patch.object(client, 'send') as send: + for char in '\0\r\n': + for arg in (f'a{char}b', f'a{char}b'.encode('ascii')): + with self.subTest(arg=arg): + with self.assertRaisesRegex(ValueError, + 'NUL, CR and LF not allowed in commands'): + client._command('LOGIN', arg, 'password') + send.assert_not_called() + + def test_quoted_control_characters(self): + client, _ = self._setup(SimpleIMAPHandler) + for char in support.control_characters_c0(): + if char in '\0\r\n': + continue + with self.subTest(char=char): + arg = f'"a{char}b"' + with mock.patch.object(client, 'send') as send: + tag = client._command('LOGIN', 'user', arg) + send.assert_called_once_with( + tag + b' LOGIN user ' + arg.encode('ascii') + b'\r\n') + + def test_append_literal_crlf(self): + class AppendServer(SimpleIMAPHandler): + def cmd_APPEND(self, tag, args): + size = int(args[-1].strip('{}')) + self._send_textline('+') + self.server.response = self.rfile.read(size + 2) + self._send_tagged(tag, 'OK', 'APPEND completed') + + client, server = self._setup(AppendServer) + client.login('user', 'pass') + message = b'Subject: test\r\n\r\nbody\tend' + typ, _ = client.append('INBOX', None, None, message) + self.assertEqual(typ, 'OK') + self.assertEqual(server.response, message + b'\r\n') + class NewIMAPTests(NewIMAPTestsMixin, unittest.TestCase): imap_class = imaplib.IMAP4 diff --git a/Misc/NEWS.d/next/Security/2026-11-16-19-00-00.gh-issue-143921.imap312.rst b/Misc/NEWS.d/next/Security/2026-11-16-19-00-00.gh-issue-143921.imap312.rst new file mode 100644 index 000000000000000..3e49ac0a60e8074 --- /dev/null +++ b/Misc/NEWS.d/next/Security/2026-11-16-19-00-00.gh-issue-143921.imap312.rst @@ -0,0 +1,2 @@ +Reject NUL, CR and LF in IMAP command arguments (CVE-2025-15366). +Other control characters in quoted strings and literal data remain allowed. From d847034ed85e8ba91f4ef201b93444b5b35991fd Mon Sep 17 00:00:00 2001 From: Saakshaat Singh <97133528+saakshaat-singh@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:38:16 -0500 Subject: [PATCH 2/2] fix --- Lib/imaplib.py | 7 +++--- Lib/test/test_imaplib.py | 23 +++++++++++++++++++ ...11-16-19-00-00.gh-issue-143921.imap312.rst | 1 + 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Lib/imaplib.py b/Lib/imaplib.py index abc1c6e3adbac59..23503f0cbf009d1 100644 --- a/Lib/imaplib.py +++ b/Lib/imaplib.py @@ -973,8 +973,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, @@ -999,9 +1002,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 55a440a2b0b542a..de6cd237a1e9a36 100644 --- a/Lib/test/test_imaplib.py +++ b/Lib/test/test_imaplib.py @@ -560,6 +560,29 @@ def test_quoted_control_characters(self): send.assert_called_once_with( tag + b' LOGIN user ' + arg.encode('ascii') + b'\r\n') + def test_rejected_append_clears_literal(self): + client, _ = self._setup(SimpleIMAPHandler) + client.login('user', 'pass') + for char in '\0\r\n': + for mailbox in (f'a{char}b', f'a{char}b'.encode('ascii')): + with self.subTest(mailbox=mailbox): + with self.assertRaises(ValueError): + client.append(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_append_literal_crlf(self): class AppendServer(SimpleIMAPHandler): def cmd_APPEND(self, tag, args): diff --git a/Misc/NEWS.d/next/Security/2026-11-16-19-00-00.gh-issue-143921.imap312.rst b/Misc/NEWS.d/next/Security/2026-11-16-19-00-00.gh-issue-143921.imap312.rst index 3e49ac0a60e8074..aa95dfe41df017c 100644 --- a/Misc/NEWS.d/next/Security/2026-11-16-19-00-00.gh-issue-143921.imap312.rst +++ b/Misc/NEWS.d/next/Security/2026-11-16-19-00-00.gh-issue-143921.imap312.rst @@ -1,2 +1,3 @@ Reject NUL, CR and LF in IMAP command arguments (CVE-2025-15366). Other control characters in quoted strings and literal data remain allowed. +Discard pending message literals when a command is rejected.