Skip to content

Commit 6b81784

Browse files
gh-88574: Do not swallow the line after a terminating literal in imaplib (GH-153317)
GH-152751 skipped a spurious blank line after a literal unconditionally, corrupting a response that ends with a literal (such as a mailbox name returned by LIST): its empty trailer was mistaken for the blank and the following line was swallowed. The blank is now skipped only inside an unclosed parenthesis. After a literal that ends the response it instead arrives before the next response and is skipped there. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 754b9f2 commit 6b81784

3 files changed

Lines changed: 64 additions & 3 deletions

File tree

Lib/imaplib.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@
138138
_quoted = re.compile(br'"(?:[^"\\]|\\.)*+"')
139139

140140

141+
def _paren_depth(data, depth=0):
142+
# Net parenthesis nesting of data, ignoring parentheses in quoted strings.
143+
data = _quoted.sub(b'', data)
144+
return depth + data.count(b'(') - data.count(b')')
145+
146+
141147
class IMAP4:
142148

143149
r"""IMAP4 client class.
@@ -1345,6 +1351,11 @@ def _get_response(self, start_timeout=False):
13451351
else:
13461352
resp = self._get_line()
13471353

1354+
# Skip spurious blank lines between responses (some servers send one
1355+
# after a literal that ends a response).
1356+
while resp == b'':
1357+
resp = self._get_line()
1358+
13481359
# Command completion response?
13491360

13501361
if self._match(self.tagre, resp):
@@ -1382,6 +1393,7 @@ def _get_response(self, start_timeout=False):
13821393

13831394
# Is there a literal to come?
13841395

1396+
depth = 0 # open parenthesis nesting so far
13851397
while self._match(self.Literal, dat):
13861398

13871399
# Read literal direct from connection.
@@ -1395,13 +1407,15 @@ def _get_response(self, start_timeout=False):
13951407
# Store response with literal as tuple
13961408

13971409
self._append_untagged(typ, (dat, data))
1410+
depth = _paren_depth(dat, depth)
13981411

13991412
# Read trailer - possibly containing another literal
14001413

14011414
dat = self._get_line()
14021415

1403-
# Skip a blank line that some servers send after a literal.
1404-
if dat == b'':
1416+
# Skip spurious blank lines after a literal, but only inside an
1417+
# unclosed parenthesis (at top level they end the response).
1418+
while dat == b'' and depth > 0:
14051419
dat = self._get_line()
14061420

14071421
self._append_untagged(typ, dat)

Lib/test/test_imaplib.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,51 @@ def cmd_FETCH(self, tag, args):
10411041
self.assertEqual(data, [(b'1 (BODY[HEADER] {13}', b'Subject: test'),
10421042
b')'])
10431043

1044+
def test_literal_terminating_response(self):
1045+
# A literal ending a response (a LIST mailbox name sent as a literal)
1046+
# has an empty trailer that must not be swallowed. Conforming case:
1047+
# no spurious blank lines.
1048+
names = [b'My (box)"', b'Another', b'Third']
1049+
class Handler(SimpleIMAPHandler):
1050+
def cmd_LIST(self, tag, args):
1051+
for name in names:
1052+
self._send(b'* LIST (\\HasNoChildren) "/" {%d}\r\n'
1053+
% len(name))
1054+
self._send(name)
1055+
self._send(b'\r\n') # ends the response, no blank
1056+
self._send_tagged(tag, 'OK', 'LIST completed')
1057+
client, _ = self._setup(Handler)
1058+
client.login('user', 'pass')
1059+
typ, data = client.list()
1060+
self.assertEqual(typ, 'OK')
1061+
self.assertEqual(data, [
1062+
(b'(\\HasNoChildren) "/" {9}', b'My (box)"'), b'',
1063+
(b'(\\HasNoChildren) "/" {7}', b'Another'), b'',
1064+
(b'(\\HasNoChildren) "/" {5}', b'Third'), b'',
1065+
])
1066+
1067+
def test_spurious_blank_lines_between_responses(self):
1068+
# A spurious blank line after each terminating literal falls between the
1069+
# untagged responses and must be skipped, even several in a row.
1070+
names = [b'My (box)"', b'Another', b'Third']
1071+
class Handler(SimpleIMAPHandler):
1072+
def cmd_LIST(self, tag, args):
1073+
for name in names:
1074+
self._send(b'* LIST (\\HasNoChildren) "/" {%d}\r\n'
1075+
% len(name))
1076+
self._send(name)
1077+
self._send(b'\r\n\r\n') # ends the response, then a blank
1078+
self._send_tagged(tag, 'OK', 'LIST completed')
1079+
client, _ = self._setup(Handler)
1080+
client.login('user', 'pass')
1081+
typ, data = client.list()
1082+
self.assertEqual(typ, 'OK')
1083+
self.assertEqual(data, [
1084+
(b'(\\HasNoChildren) "/" {9}', b'My (box)"'), b'',
1085+
(b'(\\HasNoChildren) "/" {7}', b'Another'), b'',
1086+
(b'(\\HasNoChildren) "/" {5}', b'Third'), b'',
1087+
])
1088+
10441089
def test_unselect(self):
10451090
client, server = self._setup(SimpleIMAPHandler)
10461091
client.login('user', 'pass')
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
:mod:`imaplib` no longer fails when a server sends a spurious blank line
2-
after the counted data of a literal. Such a blank line is now skipped.
2+
after the counted data of a literal, including after a literal that
3+
terminates a response (such as a mailbox name returned by ``LIST``).
4+
Such blank lines are now skipped without swallowing the following line.

0 commit comments

Comments
 (0)