Skip to content

Commit 217472d

Browse files
miss-islingtonserhiy-storchakaclaude
authored
[3.15] gh-88574: Do not swallow the line after a terminating literal in imaplib (GH-153317) (GH-153324)
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. (cherry picked from commit 6b81784) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 10ea39e commit 217472d

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
@@ -137,6 +137,12 @@
137137
_quoted = re.compile(br'"(?:[^"\\]|\\.)*+"')
138138

139139

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

142148
r"""IMAP4 client class.
@@ -1284,6 +1290,11 @@ def _get_response(self, start_timeout=False):
12841290
else:
12851291
resp = self._get_line()
12861292

1293+
# Skip spurious blank lines between responses (some servers send one
1294+
# after a literal that ends a response).
1295+
while resp == b'':
1296+
resp = self._get_line()
1297+
12871298
# Command completion response?
12881299

12891300
if self._match(self.tagre, resp):
@@ -1321,6 +1332,7 @@ def _get_response(self, start_timeout=False):
13211332

13221333
# Is there a literal to come?
13231334

1335+
depth = 0 # open parenthesis nesting so far
13241336
while self._match(self.Literal, dat):
13251337

13261338
# Read literal direct from connection.
@@ -1334,13 +1346,15 @@ def _get_response(self, start_timeout=False):
13341346
# Store response with literal as tuple
13351347

13361348
self._append_untagged(typ, (dat, data))
1349+
depth = _paren_depth(dat, depth)
13371350

13381351
# Read trailer - possibly containing another literal
13391352

13401353
dat = self._get_line()
13411354

1342-
# Skip a blank line that some servers send after a literal.
1343-
if dat == b'':
1355+
# Skip spurious blank lines after a literal, but only inside an
1356+
# unclosed parenthesis (at top level they end the response).
1357+
while dat == b'' and depth > 0:
13441358
dat = self._get_line()
13451359

13461360
self._append_untagged(typ, dat)

Lib/test/test_imaplib.py

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

991+
def test_literal_terminating_response(self):
992+
# A literal ending a response (a LIST mailbox name sent as a literal)
993+
# has an empty trailer that must not be swallowed. Conforming case:
994+
# no spurious blank lines.
995+
names = [b'My (box)"', b'Another', b'Third']
996+
class Handler(SimpleIMAPHandler):
997+
def cmd_LIST(self, tag, args):
998+
for name in names:
999+
self._send(b'* LIST (\\HasNoChildren) "/" {%d}\r\n'
1000+
% len(name))
1001+
self._send(name)
1002+
self._send(b'\r\n') # ends the response, no blank
1003+
self._send_tagged(tag, 'OK', 'LIST completed')
1004+
client, _ = self._setup(Handler)
1005+
client.login('user', 'pass')
1006+
typ, data = client.list()
1007+
self.assertEqual(typ, 'OK')
1008+
self.assertEqual(data, [
1009+
(b'(\\HasNoChildren) "/" {9}', b'My (box)"'), b'',
1010+
(b'(\\HasNoChildren) "/" {7}', b'Another'), b'',
1011+
(b'(\\HasNoChildren) "/" {5}', b'Third'), b'',
1012+
])
1013+
1014+
def test_spurious_blank_lines_between_responses(self):
1015+
# A spurious blank line after each terminating literal falls between the
1016+
# untagged responses and must be skipped, even several in a row.
1017+
names = [b'My (box)"', b'Another', b'Third']
1018+
class Handler(SimpleIMAPHandler):
1019+
def cmd_LIST(self, tag, args):
1020+
for name in names:
1021+
self._send(b'* LIST (\\HasNoChildren) "/" {%d}\r\n'
1022+
% len(name))
1023+
self._send(name)
1024+
self._send(b'\r\n\r\n') # ends the response, then a blank
1025+
self._send_tagged(tag, 'OK', 'LIST completed')
1026+
client, _ = self._setup(Handler)
1027+
client.login('user', 'pass')
1028+
typ, data = client.list()
1029+
self.assertEqual(typ, 'OK')
1030+
self.assertEqual(data, [
1031+
(b'(\\HasNoChildren) "/" {9}', b'My (box)"'), b'',
1032+
(b'(\\HasNoChildren) "/" {7}', b'Another'), b'',
1033+
(b'(\\HasNoChildren) "/" {5}', b'Third'), b'',
1034+
])
1035+
9911036
def test_unselect(self):
9921037
client, server = self._setup(SimpleIMAPHandler)
9931038
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)