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