Skip to content

Commit 477136f

Browse files
serhiy-storchakaclaude
authored andcommitted
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. (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 670b4ac commit 477136f

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
@@ -879,6 +879,51 @@ def cmd_FETCH(self, tag, args):
879879
self.assertEqual(data, [(b'1 (BODY[HEADER] {13}', b'Subject: test'),
880880
b')'])
881881

882+
def test_literal_terminating_response(self):
883+
# A literal ending a response (a LIST mailbox name sent as a literal)
884+
# has an empty trailer that must not be swallowed. Conforming case:
885+
# no spurious blank lines.
886+
names = [b'My (box)"', b'Another', b'Third']
887+
class Handler(SimpleIMAPHandler):
888+
def cmd_LIST(self, tag, args):
889+
for name in names:
890+
self._send(b'* LIST (\\HasNoChildren) "/" {%d}\r\n'
891+
% len(name))
892+
self._send(name)
893+
self._send(b'\r\n') # ends the response, no blank
894+
self._send_tagged(tag, 'OK', 'LIST completed')
895+
client, _ = self._setup(Handler)
896+
client.login('user', 'pass')
897+
typ, data = client.list()
898+
self.assertEqual(typ, 'OK')
899+
self.assertEqual(data, [
900+
(b'(\\HasNoChildren) "/" {9}', b'My (box)"'), b'',
901+
(b'(\\HasNoChildren) "/" {7}', b'Another'), b'',
902+
(b'(\\HasNoChildren) "/" {5}', b'Third'), b'',
903+
])
904+
905+
def test_spurious_blank_lines_between_responses(self):
906+
# A spurious blank line after each terminating literal falls between the
907+
# untagged responses and must be skipped, even several in a row.
908+
names = [b'My (box)"', b'Another', b'Third']
909+
class Handler(SimpleIMAPHandler):
910+
def cmd_LIST(self, tag, args):
911+
for name in names:
912+
self._send(b'* LIST (\\HasNoChildren) "/" {%d}\r\n'
913+
% len(name))
914+
self._send(name)
915+
self._send(b'\r\n\r\n') # ends the response, then a blank
916+
self._send_tagged(tag, 'OK', 'LIST completed')
917+
client, _ = self._setup(Handler)
918+
client.login('user', 'pass')
919+
typ, data = client.list()
920+
self.assertEqual(typ, 'OK')
921+
self.assertEqual(data, [
922+
(b'(\\HasNoChildren) "/" {9}', b'My (box)"'), b'',
923+
(b'(\\HasNoChildren) "/" {7}', b'Another'), b'',
924+
(b'(\\HasNoChildren) "/" {5}', b'Third'), b'',
925+
])
926+
882927
def test_unselect(self):
883928
client, server = self._setup(SimpleIMAPHandler)
884929
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)