]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-143921: Reject control characters in IMAP commands
authorSeth Michael Larson <seth@python.org>
Tue, 20 Jan 2026 20:45:42 +0000 (14:45 -0600)
committerGitHub <noreply@github.com>
Tue, 20 Jan 2026 20:45:42 +0000 (20:45 +0000)
Lib/imaplib.py
Lib/test/test_imaplib.py
Misc/NEWS.d/next/Security/2026-01-16-11-41-06.gh-issue-143921.AeCOor.rst [new file with mode: 0644]

index 22a0afcd981519e5dbc78a27f88f20d86c700dd5..cb3edceae0d9f1904650d432703e145ed590fea8 100644 (file)
@@ -129,7 +129,7 @@ Untagged_status = re.compile(
 # 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-\x1F\x7F]')
 
 
 class IMAP4:
@@ -1105,6 +1105,8 @@ class IMAP4:
             if arg is None: continue
             if isinstance(arg, str):
                 arg = bytes(arg, self._encoding)
+            if _control_chars.search(arg):
+                raise ValueError("Control characters not allowed in commands")
             data = data + b' ' + arg
 
         literal = self.literal
index 430fa71fa29f59c23d410f268450d77149dc1764..cb5454b40eccf90e7f5a2eeeda2f0c1ffe9343e3 100644 (file)
@@ -657,6 +657,12 @@ class NewIMAPTestsMixin:
         self.assertEqual(data[0], b'Returned to authenticated state. (Success)')
         self.assertEqual(client.state, 'AUTH')
 
+    def test_control_characters(self):
+        client, _ = self._setup(SimpleIMAPHandler)
+        for c0 in support.control_characters_c0():
+            with self.assertRaises(ValueError):
+                client.login(f'user{c0}', 'pass')
+
     # property tests
 
     def test_file_property_should_not_be_accessed(self):
diff --git a/Misc/NEWS.d/next/Security/2026-01-16-11-41-06.gh-issue-143921.AeCOor.rst b/Misc/NEWS.d/next/Security/2026-01-16-11-41-06.gh-issue-143921.AeCOor.rst
new file mode 100644 (file)
index 0000000..4e13fe9
--- /dev/null
@@ -0,0 +1 @@
+Reject control characters in IMAP commands.