Skip to content

Commit 6262704

Browse files
authored
gh-143921: Reject control characters in IMAP commands
1 parent 27a7160 commit 6262704

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

134134

135135
class IMAP4:
@@ -1105,6 +1105,8 @@ def _command(self, name, *args):
11051105
if arg is None: continue
11061106
if isinstance(arg, str):
11071107
arg = bytes(arg, self._encoding)
1108+
if _control_chars.search(arg):
1109+
raise ValueError("Control characters not allowed in commands")
11081110
data = data + b' ' + arg
11091111

11101112
literal = self.literal

Lib/test/test_imaplib.py

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

660+
def test_control_characters(self):
661+
client, _ = self._setup(SimpleIMAPHandler)
662+
for c0 in support.control_characters_c0():
663+
with self.assertRaises(ValueError):
664+
client.login(f'user{c0}', 'pass')
665+
660666
# property tests
661667

662668
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)