Skip to content

Commit 13c5575

Browse files
miss-islingtonserhiy-storchakaclaude
authored
[3.13] gh-88574: Do not swallow the line after a terminating literal in imaplib (GH-153317) (GH-153326)
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 e7f42be commit 13c5575

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

142142

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

145151
r"""IMAP4 client class.
@@ -1154,6 +1160,11 @@ def _get_response(self):
11541160

11551161
resp = self._get_line()
11561162

1163+
# Skip spurious blank lines between responses (some servers send one
1164+
# after a literal that ends a response).
1165+
while resp == b'':
1166+
resp = self._get_line()
1167+
11571168
# Command completion response?
11581169

11591170
if self._match(self.tagre, resp):
@@ -1191,6 +1202,7 @@ def _get_response(self):
11911202

11921203
# Is there a literal to come?
11931204

1205+
depth = 0 # open parenthesis nesting so far
11941206
while self._match(self.Literal, dat):
11951207

11961208
# Read literal direct from connection.
@@ -1204,13 +1216,15 @@ def _get_response(self):
12041216
# Store response with literal as tuple
12051217

12061218
self._append_untagged(typ, (dat, data))
1219+
depth = _paren_depth(dat, depth)
12071220

12081221
# Read trailer - possibly containing another literal
12091222

12101223
dat = self._get_line()
12111224

1212-
# Skip a blank line that some servers send after a literal.
1213-
if dat == b'':
1225+
# Skip spurious blank lines after a literal, but only inside an
1226+
# unclosed parenthesis (at top level they end the response).
1227+
while dat == b'' and depth > 0:
12141228
dat = self._get_line()
12151229

12161230
self._append_untagged(typ, dat)

Lib/test/test_imaplib.py

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

908+
def test_literal_terminating_response(self):
909+
# A literal ending a response (a LIST mailbox name sent as a literal)
910+
# has an empty trailer that must not be swallowed. Conforming case:
911+
# no spurious blank lines.
912+
names = [b'My (box)"', b'Another', b'Third']
913+
class Handler(SimpleIMAPHandler):
914+
def cmd_LIST(self, tag, args):
915+
for name in names:
916+
self._send(b'* LIST (\\HasNoChildren) "/" {%d}\r\n'
917+
% len(name))
918+
self._send(name)
919+
self._send(b'\r\n') # ends the response, no blank
920+
self._send_tagged(tag, 'OK', 'LIST completed')
921+
client, _ = self._setup(Handler)
922+
client.login('user', 'pass')
923+
typ, data = client.list()
924+
self.assertEqual(typ, 'OK')
925+
self.assertEqual(data, [
926+
(b'(\\HasNoChildren) "/" {9}', b'My (box)"'), b'',
927+
(b'(\\HasNoChildren) "/" {7}', b'Another'), b'',
928+
(b'(\\HasNoChildren) "/" {5}', b'Third'), b'',
929+
])
930+
931+
def test_spurious_blank_lines_between_responses(self):
932+
# A spurious blank line after each terminating literal falls between the
933+
# untagged responses and must be skipped, even several in a row.
934+
names = [b'My (box)"', b'Another', b'Third']
935+
class Handler(SimpleIMAPHandler):
936+
def cmd_LIST(self, tag, args):
937+
for name in names:
938+
self._send(b'* LIST (\\HasNoChildren) "/" {%d}\r\n'
939+
% len(name))
940+
self._send(name)
941+
self._send(b'\r\n\r\n') # ends the response, then a blank
942+
self._send_tagged(tag, 'OK', 'LIST completed')
943+
client, _ = self._setup(Handler)
944+
client.login('user', 'pass')
945+
typ, data = client.list()
946+
self.assertEqual(typ, 'OK')
947+
self.assertEqual(data, [
948+
(b'(\\HasNoChildren) "/" {9}', b'My (box)"'), b'',
949+
(b'(\\HasNoChildren) "/" {7}', b'Another'), b'',
950+
(b'(\\HasNoChildren) "/" {5}', b'Third'), b'',
951+
])
952+
908953
def test_unselect(self):
909954
client, server = self._setup(SimpleIMAPHandler)
910955
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)