Skip to content

Commit cb508c6

Browse files
sethmlarsonhroncok
authored andcommitted
Downstream only: Reject control characters in IMAP commands
1 parent 7fe9933 commit cb508c6

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
@@ -131,7 +131,7 @@
131131
# We compile these in _mode_xxx.
132132
_Literal = br'.*{(?P<size>\d+)}$'
133133
_Untagged_status = br'\* (?P<data>\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?'
134-
134+
_control_chars = re.compile(b'[\x00-\x1F\x7F]')
135135

136136

137137
class IMAP4:
@@ -1108,6 +1108,8 @@ def _command(self, name, *args):
11081108
if arg is None: continue
11091109
if isinstance(arg, str):
11101110
arg = bytes(arg, self._encoding)
1111+
if _control_chars.search(arg):
1112+
raise ValueError("Control characters not allowed in commands")
11111113
data = data + b' ' + arg
11121114

11131115
literal = self.literal

Lib/test/test_imaplib.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,12 @@ def test_unselect(self):
663663
self.assertEqual(data[0], b'Returned to authenticated state. (Success)')
664664
self.assertEqual(client.state, 'AUTH')
665665

666+
def test_control_characters(self):
667+
client, _ = self._setup(SimpleIMAPHandler)
668+
for c0 in support.control_characters_c0():
669+
with self.assertRaises(ValueError):
670+
client.login(f'user{c0}', 'pass')
671+
666672
# property tests
667673

668674
def test_file_property_should_not_be_accessed(self):
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)