Blame SOURCES/00208-CVE-2013-1752-imaplib.patch

925e6b
925e6b
# HG changeset patch
925e6b
# User R David Murray <rdmurray@bitdance.com>
925e6b
# Date 1388775562 18000
925e6b
# Node ID dd906f4ab9237020a7a275c2d361fa288e553481
925e6b
# Parent  69b5f692455306c98aa27ecea17e6290787ebd3f
925e6b
closes 16039: CVE-2013-1752: limit line length in imaplib readline calls.
925e6b
925e6b
diff --git a/Lib/imaplib.py b/Lib/imaplib.py
925e6b
--- a/Lib/imaplib.py
925e6b
+++ b/Lib/imaplib.py
925e6b
@@ -35,6 +35,15 @@ IMAP4_PORT = 143
925e6b
 IMAP4_SSL_PORT = 993
925e6b
 AllowedVersions = ('IMAP4REV1', 'IMAP4')        # Most recent first
925e6b
 
925e6b
+# Maximal line length when calling readline(). This is to prevent
925e6b
+# reading arbitrary length lines. RFC 3501 and 2060 (IMAP 4rev1)
925e6b
+# don't specify a line length. RFC 2683 however suggests limiting client
925e6b
+# command lines to 1000 octets and server command lines to 8000 octets.
925e6b
+# We have selected 10000 for some extra margin and since that is supposedly
925e6b
+# also what UW and Panda IMAP does.
925e6b
+_MAXLINE = 10000
925e6b
+
925e6b
+
925e6b
 #       Commands
925e6b
 
925e6b
 Commands = {
925e6b
@@ -237,7 +246,10 @@ class IMAP4:
925e6b
 
925e6b
     def readline(self):
925e6b
         """Read line from remote."""
925e6b
-        return self.file.readline()
925e6b
+        line = self.file.readline(_MAXLINE + 1)
925e6b
+        if len(line) > _MAXLINE:
925e6b
+            raise self.error("got more than %d bytes" % _MAXLINE)
925e6b
+        return line
925e6b
 
925e6b
 
925e6b
     def send(self, data):
925e6b
diff --git a/Lib/test/test_imaplib.py b/Lib/test/test_imaplib.py
925e6b
--- a/Lib/test/test_imaplib.py
925e6b
+++ b/Lib/test/test_imaplib.py
925e6b
@@ -165,6 +165,16 @@ class BaseThreadedNetworkedTests(unittes
925e6b
                               self.imap_class, *server.server_address)
925e6b
 
925e6b
 
925e6b
+    def test_linetoolong(self):
925e6b
+        class TooLongHandler(SimpleIMAPHandler):
925e6b
+            def handle(self):
925e6b
+                # Send a very long response line
925e6b
+                self.wfile.write('* OK ' + imaplib._MAXLINE*'x' + '\r\n')
925e6b
+
925e6b
+        with self.reaped_server(TooLongHandler) as server:
925e6b
+            self.assertRaises(imaplib.IMAP4.error,
925e6b
+                              self.imap_class, *server.server_address)
925e6b
+
925e6b
 class ThreadedNetworkedTests(BaseThreadedNetworkedTests):
925e6b
 
925e6b
     server_class = SocketServer.TCPServer