Skip to content

Commit 0e15a81

Browse files
fix
1 parent 40533a4 commit 0e15a81

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎Lib/imaplib.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
# We compile these in _mode_xxx.
133133
_Literal = br'.*{(?P<size>\d+)}$'
134134
_Untagged_status = br'\* (?P<data>\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?'
135+
_control_chars = re.compile(b'[\x00\r\n]')
135136

136137

137138

@@ -994,6 +995,8 @@ def _command(self, name, *args):
994995
if arg is None: continue
995996
if isinstance(arg, str):
996997
arg = bytes(arg, self._encoding)
998+
if _control_chars.search(arg):
999+
raise ValueError("NUL, CR and LF not allowed in commands")
9971000
data = data + b' ' + arg
9981001

9991002
literal = self.literal

‎Lib/test/test_imaplib.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,44 @@ def test_unselect(self):
537537
self.assertEqual(data[0], b'Returned to authenticated state. (Success)')
538538
self.assertEqual(client.state, 'AUTH')
539539

540+
def test_command_control_characters(self):
541+
client, _ = self._setup(SimpleIMAPHandler)
542+
with mock.patch.object(client, 'send') as send:
543+
for char in '\0\r\n':
544+
for arg in (f'a{char}b', f'a{char}b'.encode('ascii')):
545+
with self.subTest(arg=arg):
546+
with self.assertRaisesRegex(ValueError,
547+
'NUL, CR and LF not allowed in commands'):
548+
client._command('LOGIN', arg, 'password')
549+
send.assert_not_called()
550+
551+
def test_quoted_control_characters(self):
552+
client, _ = self._setup(SimpleIMAPHandler)
553+
for char in support.control_characters_c0():
554+
if char in '\0\r\n':
555+
continue
556+
with self.subTest(char=char):
557+
arg = f'"a{char}b"'
558+
with mock.patch.object(client, 'send') as send:
559+
tag = client._command('LOGIN', 'user', arg)
560+
send.assert_called_once_with(
561+
tag + b' LOGIN user ' + arg.encode('ascii') + b'\r\n')
562+
563+
def test_append_literal_crlf(self):
564+
class AppendServer(SimpleIMAPHandler):
565+
def cmd_APPEND(self, tag, args):
566+
size = int(args[-1].strip('{}'))
567+
self._send_textline('+')
568+
self.server.response = self.rfile.read(size + 2)
569+
self._send_tagged(tag, 'OK', 'APPEND completed')
570+
571+
client, server = self._setup(AppendServer)
572+
client.login('user', 'pass')
573+
message = b'Subject: test\r\n\r\nbody\tend'
574+
typ, _ = client.append('INBOX', None, None, message)
575+
self.assertEqual(typ, 'OK')
576+
self.assertEqual(server.response, message + b'\r\n')
577+
540578

541579
class NewIMAPTests(NewIMAPTestsMixin, unittest.TestCase):
542580
imap_class = imaplib.IMAP4
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reject NUL, CR and LF in IMAP command arguments (CVE-2025-15366).
2+
Other control characters in quoted strings and literal data remain allowed.

0 commit comments

Comments
 (0)