Skip to content

Commit 3b1b4dd

Browse files
sethmlarsonhroncok
authored andcommitted
pythongh-143921: Reject control characters in IMAP commands (cherry-picked from commit 6262704)
1 parent b6b7f6f commit 3b1b4dd

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

Lib/imaplib.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
# We compile these in _mode_xxx.
129129
_Literal = br'.*{(?P<size>\d+)}$'
130130
_Untagged_status = br'\* (?P<data>\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?'
131-
131+
_control_chars = re.compile(b'[\x00-\x1F\x7F]')
132132

133133

134134
class IMAP4:
@@ -958,6 +958,8 @@ def _command(self, name, *args):
958958
if arg is None: continue
959959
if isinstance(arg, str):
960960
arg = bytes(arg, self._encoding)
961+
if _control_chars.search(arg):
962+
raise ValueError("Control characters not allowed in commands")
961963
data = data + b' ' + arg
962964

963965
literal = self.literal

Lib/test/test_imaplib.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,12 @@ def test_login(self):
462462
self.assertEqual(data[0], b'LOGIN completed')
463463
self.assertEqual(client.state, 'AUTH')
464464

465+
def test_control_characters(self):
466+
client, _ = self._setup(SimpleIMAPHandler)
467+
for c0 in support.control_characters_c0():
468+
with self.assertRaises(ValueError):
469+
client.login(f'user{c0}', 'pass')
470+
465471
def test_logout(self):
466472
client, _ = self._setup(SimpleIMAPHandler)
467473
typ, data = client.login('user', 'pass')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reject control characters in IMAP commands.

0 commit comments

Comments
 (0)