Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Lib/imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
# We compile these in _mode_xxx.
_Literal = br'.*{(?P<size>\d+)}$'
_Untagged_status = br'\* (?P<data>\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?'
_control_chars = re.compile(b'[\x00\r\n]')



Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down
61 changes: 61 additions & 0 deletions Lib/test/test_imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Loading