Skip to content

Commit ae2cfac

Browse files
sethmlarsonhroncok
authored andcommitted
Reject control characters in IMAP commands
1 parent 9f4263e commit ae2cfac

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
@@ -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:
@@ -1000,6 +1000,8 @@ def _command(self, name, *args):
10001000
if arg is None: continue
10011001
if isinstance(arg, str):
10021002
arg = bytes(arg, self._encoding)
1003+
if _control_chars.search(arg):
1004+
raise ValueError("Control characters not allowed in commands")
10031005
data = data + b' ' + arg
10041006

10051007
literal = self.literal

Lib/test/test_imaplib.py

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

518+
def test_control_characters(self):
519+
client, _ = self._setup(SimpleIMAPHandler)
520+
for c0 in support.control_characters_c0():
521+
with self.assertRaises(ValueError):
522+
client.login(f'user{c0}', 'pass')
523+
518524
def test_logout(self):
519525
client, _ = self._setup(SimpleIMAPHandler)
520526
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)