Skip to content

Commit 22fd8d9

Browse files
sethmlarsonhroncok
authored andcommitted
pythongh-143921: Reject control characters in IMAP commands (cherry-picked from commit 6262704)
1 parent 4621d9c commit 22fd8d9

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

Lib/imaplib.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +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-
135+
_control_chars = re.compile(b'[\x00-\x1F\x7F]')
136136

137137

138138
class IMAP4:
@@ -994,6 +994,8 @@ def _command(self, name, *args):
994994
if arg is None: continue
995995
if isinstance(arg, str):
996996
arg = bytes(arg, self._encoding)
997+
if _control_chars.search(arg):
998+
raise ValueError("Control characters not allowed in commands")
997999
data = data + b' ' + arg
9981000

9991001
literal = self.literal

Lib/test/test_imaplib.py

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

513+
def test_control_characters(self):
514+
client, _ = self._setup(SimpleIMAPHandler)
515+
for c0 in support.control_characters_c0():
516+
with self.assertRaises(ValueError):
517+
client.login(f'user{c0}', 'pass')
518+
513519
def test_logout(self):
514520
client, _ = self._setup(SimpleIMAPHandler)
515521
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)