Blame SOURCES/00206-CVE-2013-1752-poplib.patch

ae2451
ae2451
# HG changeset patch
ae2451
# User Benjamin Peterson <benjamin@python.org>
ae2451
# Date 1417827758 18000
ae2451
# Node ID 339f877cca115c1901f5dd93d7bc066031d2a669
ae2451
# Parent  54af094087953f4997a4ead63e949d845c4b4412
ae2451
in poplib, limit maximum line length that we read from the network (closes #16041)
ae2451
ae2451
Patch from Berker Peksag.
ae2451
ae2451
diff --git a/Lib/poplib.py b/Lib/poplib.py
ae2451
--- a/Lib/poplib.py
ae2451
+++ b/Lib/poplib.py
ae2451
@@ -32,6 +32,12 @@ CR = '\r'
ae2451
 LF = '\n'
ae2451
 CRLF = CR+LF
ae2451
 
ae2451
+# maximal line length when calling readline(). This is to prevent
ae2451
+# reading arbitrary length lines. RFC 1939 limits POP3 line length to
ae2451
+# 512 characters, including CRLF. We have selected 2048 just to be on
ae2451
+# the safe side.
ae2451
+_MAXLINE = 2048
ae2451
+
ae2451
 
ae2451
 class POP3:
ae2451
 
ae2451
@@ -103,7 +109,9 @@ class POP3:
ae2451
     # Raise error_proto('-ERR EOF') if the connection is closed.
ae2451
 
ae2451
     def _getline(self):
ae2451
-        line = self.file.readline()
ae2451
+        line = self.file.readline(_MAXLINE + 1)
ae2451
+        if len(line) > _MAXLINE:
ae2451
+            raise error_proto('line too long')
ae2451
         if self._debugging > 1: print '*get*', repr(line)
ae2451
         if not line: raise error_proto('-ERR EOF')
ae2451
         octets = len(line)
ae2451
@@ -365,6 +373,8 @@ else:
ae2451
             match = renewline.match(self.buffer)
ae2451
             while not match:
ae2451
                 self._fillBuffer()
ae2451
+                if len(self.buffer) > _MAXLINE:
ae2451
+                    raise error_proto('line too long')
ae2451
                 match = renewline.match(self.buffer)
ae2451
             line = match.group(0)
ae2451
             self.buffer = renewline.sub('' ,self.buffer, 1)
ae2451
diff --git a/Lib/test/test_poplib.py b/Lib/test/test_poplib.py
ae2451
--- a/Lib/test/test_poplib.py
ae2451
+++ b/Lib/test/test_poplib.py
ae2451
@@ -198,6 +198,10 @@ class TestPOP3Class(TestCase):
ae2451
                     113)
ae2451
         self.assertEqual(self.client.retr('foo'), expected)
ae2451
 
ae2451
+    def test_too_long_lines(self):
ae2451
+        self.assertRaises(poplib.error_proto, self.client._shortcmd,
ae2451
+                          'echo +%s' % ((poplib._MAXLINE + 10) * 'a'))
ae2451
+
ae2451
     def test_dele(self):
ae2451
         self.assertOK(self.client.dele('foo'))
ae2451