Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 317f64f

Browse files
committed
#21815: violate IMAP RFC to be compatible with, e.g., gmail
and others, including imaplib's own behavior. I'm applying this only to 3.6 because there's a potential backward compatibility concern: if there are servers that include ] characters in the 'text' portion of their imap responses, this code change could introduce a new bug. Patch by Lita Cho, reviewed by Jessica McKellar, Berker Peksag, Maciej Szulik, silentghost, and me (I fleshed out the comments with the additional info/concerns.)
1 parent 01759d5 commit 317f64f

4 files changed

Lines changed: 73 additions & 1 deletion

File tree

Doc/library/imaplib.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,17 @@ An :class:`IMAP4` instance has the following methods:
500500
M.store(num, '+FLAGS', '\\Deleted')
501501
M.expunge()
502502

503+
.. note::
504+
505+
Creating flags containing ']' (for example: "[test]") violates
506+
:rfc:`3501` (the IMAP protocol). However, imaplib has historically
507+
allowed creation of such tags, and popular IMAP servers, such as Gmail,
508+
accept and produce such flags. There are non-Python programs which also
509+
create such tags. Although it is an RFC violation and IMAP clients and
510+
servers are supposed to be strict, imaplib nontheless continues to allow
511+
such tags to be created for backward compatibility reasons, and as of
512+
python 3.5.2/3.6.0, handles them if they are sent from the server, since
513+
this improves real-world compatibility.
503514

504515
.. method:: IMAP4.subscribe(mailbox)
505516

Lib/imaplib.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,15 @@
111111
# Literal is no longer used; kept for backward compatibility.
112112
Literal = re.compile(br'.*{(?P<size>\d+)}$', re.ASCII)
113113
MapCRLF = re.compile(br'\r\n|\r|\n')
114-
Response_code = re.compile(br'\[(?P<type>[A-Z-]+)( (?P<data>[^\]]*))?\]')
114+
# We no longer exclude the ']' character from the data portion of the response
115+
# code, even though it violates the RFC. Popular IMAP servers such as Gmail
116+
# allow flags with ']', and there are programs (including imaplib!) that can
117+
# produce them. The problem with this is if the 'text' portion of the response
118+
# includes a ']' we'll parse the response wrong (which is the point of the RFC
119+
# restriction). However, that seems less likely to be a problem in practice
120+
# than being unable to correctly parse flags that include ']' chars, which
121+
# was reported as a real-world problem in issue #21815.
122+
Response_code = re.compile(br'\[(?P<type>[A-Z-]+)( (?P<data>.*))?\]')
115123
Untagged_response = re.compile(br'\* (?P<type>[A-Z-]+)( (?P<data>.*))?')
116124
# Untagged_status is no longer used; kept for backward compatibility
117125
Untagged_status = re.compile(

Lib/test/test_imaplib.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,55 @@ def test_connect(self):
242242
client = self.imap_class(*server.server_address)
243243
client.shutdown()
244244

245+
@reap_threads
246+
def test_bracket_flags(self):
247+
248+
# This violates RFC 3501, which disallows ']' characters in tag names,
249+
# but imaplib has allowed producing such tags forever, other programs
250+
# also produce them (eg: OtherInbox's Organizer app as of 20140716),
251+
# and Gmail, for example, accepts them and produces them. So we
252+
# support them. See issue #21815.
253+
254+
class BracketFlagHandler(SimpleIMAPHandler):
255+
256+
def handle(self):
257+
self.flags = ['Answered', 'Flagged', 'Deleted', 'Seen', 'Draft']
258+
super().handle()
259+
260+
def cmd_AUTHENTICATE(self, tag, args):
261+
self._send_textline('+')
262+
self.server.response = yield
263+
self._send_tagged(tag, 'OK', 'FAKEAUTH successful')
264+
265+
def cmd_SELECT(self, tag, args):
266+
flag_msg = ' \\'.join(self.flags)
267+
self._send_line(('* FLAGS (%s)' % flag_msg).encode('ascii'))
268+
self._send_line(b'* 2 EXISTS')
269+
self._send_line(b'* 0 RECENT')
270+
msg = ('* OK [PERMANENTFLAGS %s \\*)] Flags permitted.'
271+
% flag_msg)
272+
self._send_line(msg.encode('ascii'))
273+
self._send_tagged(tag, 'OK', '[READ-WRITE] SELECT completed.')
274+
275+
def cmd_STORE(self, tag, args):
276+
new_flags = args[2].strip('(').strip(')').split()
277+
self.flags.extend(new_flags)
278+
flags_msg = '(FLAGS (%s))' % ' \\'.join(self.flags)
279+
msg = '* %s FETCH %s' % (args[0], flags_msg)
280+
self._send_line(msg.encode('ascii'))
281+
self._send_tagged(tag, 'OK', 'STORE completed.')
282+
283+
with self.reaped_pair(BracketFlagHandler) as (server, client):
284+
code, data = client.authenticate('MYAUTH', lambda x: b'fake')
285+
self.assertEqual(code, 'OK')
286+
self.assertEqual(server.response, b'ZmFrZQ==\r\n')
287+
client.select('test')
288+
typ, [data] = client.store(b'1', "+FLAGS", "[test]")
289+
self.assertIn(b'[test]', data)
290+
client.select('test')
291+
typ, [data] = client.response('PERMANENTFLAGS')
292+
self.assertIn(b'[test]', data)
293+
245294
@reap_threads
246295
def test_issue5949(self):
247296

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ Core and Builtins
128128
Library
129129
-------
130130

131+
- Issue #21815: Accept ] characters in the data portion of imap responses,
132+
in order to handle the flags with square brackets accepted and produced
133+
by servers such as gmail.
134+
131135
- Issue #25447: fileinput now uses sys.stdin as-is if it does not have a
132136
buffer attribute (restores backward compatibility).
133137

0 commit comments

Comments
 (0)