gh-143921: Reject control characters in IMAP commands

This commit is contained in:
Seth Michael Larson 2026-01-20 14:45:42 -06:00 committed by GitHub
parent 27a7160b8b
commit 6262704b13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 1 deletions

View 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

View 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):

View File

@ -0,0 +1 @@
Reject control characters in IMAP commands.