diff --git a/Lib/imaplib.py b/Lib/imaplib.py index e337fe6471069f..23503f0cbf009d 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]') @@ -972,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, @@ -994,11 +998,11 @@ 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 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 4429a90050a14d..de6cd237a1e9a3 100644 --- a/Lib/test/test_imaplib.py +++ b/Lib/test/test_imaplib.py @@ -537,6 +537,67 @@ 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_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): + 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 00000000000000..aa95dfe41df017 --- /dev/null +++ b/Misc/NEWS.d/next/Security/2026-11-16-19-00-00.gh-issue-143921.imap312.rst @@ -0,0 +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.