Skip to content

Commit fd9262c

Browse files
committed
Issue python#16039: CVE-2013-1752: Change use of readline in imaplib module to limit
line length. Patch by Emil Lind.
1 parent 0840b41 commit fd9262c

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Lib/imaplib.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
IMAP4_SSL_PORT = 993
4343
AllowedVersions = ('IMAP4REV1', 'IMAP4') # Most recent first
4444

45+
# Maximal line length when calling readline(). This is to prevent
46+
# reading arbitrary length lines. RFC 3501 and 2060 (IMAP 4rev1)
47+
# don't specify a line length. RFC 2683 however suggests limiting client
48+
# command lines to 1000 octets and server command lines to 8000 octets.
49+
# We have selected 10000 for some extra margin and since that is supposedly
50+
# also what UW and Panda IMAP does.
51+
_MAXLINE = 10000
52+
53+
4554
# Commands
4655

4756
Commands = {
@@ -263,7 +272,10 @@ def read(self, size):
263272

264273
def readline(self):
265274
"""Read line from remote."""
266-
return self.file.readline()
275+
line = self.file.readline(_MAXLINE + 1)
276+
if len(line) > _MAXLINE:
277+
raise self.error("got more than %d bytes" % _MAXLINE)
278+
return line
267279

268280

269281
def send(self, data):

Lib/test/test_imaplib.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,17 @@ def cmd_AUTHENTICATE(self, tag, args):
309309
self.assertEqual(ret, "OK")
310310

311311

312+
def test_linetoolong(self):
313+
class TooLongHandler(SimpleIMAPHandler):
314+
def handle(self):
315+
# Send a very long response line
316+
self.wfile.write(b'* OK ' + imaplib._MAXLINE*b'x' + b'\r\n')
317+
318+
with self.reaped_server(TooLongHandler) as server:
319+
self.assertRaises(imaplib.IMAP4.error,
320+
self.imap_class, *server.server_address)
321+
322+
312323
class ThreadedNetworkedTests(BaseThreadedNetworkedTests):
313324

314325
server_class = socketserver.TCPServer

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.6?
1010
Library
1111
-------
1212

13+
- Issue #16039: CVE-2013-1752: Change use of readline in imaplib module to limit
14+
line length. Patch by Emil Lind.
15+
1316
- Issue #22421: Fix a regression that caused the pydoc server to be bound to
1417
all interfaces instead of only localhost.
1518

0 commit comments

Comments
 (0)