Blame SOURCES/00207-CVE-2013-1752-smtplib.patch

f63228
f63228
# HG changeset patch
f63228
# User Benjamin Peterson <benjamin@python.org>
f63228
# Date 1417827918 18000
f63228
# Node ID 923aac88a3cc76a95d5a04d9d3ece245147a8064
f63228
# Parent  339f877cca115c1901f5dd93d7bc066031d2a669
f63228
smtplib: limit amount read from the network (closes #16042)
f63228
f63228
diff --git a/Lib/smtplib.py b/Lib/smtplib.py
f63228
--- a/Lib/smtplib.py
f63228
+++ b/Lib/smtplib.py
f63228
@@ -57,6 +57,7 @@ from sys import stderr
f63228
 SMTP_PORT = 25
f63228
 SMTP_SSL_PORT = 465
f63228
 CRLF = "\r\n"
f63228
+_MAXLINE = 8192 # more than 8 times larger than RFC 821, 4.5.3
f63228
 
f63228
 OLDSTYLE_AUTH = re.compile(r"auth=(.*)", re.I)
f63228
 
f63228
@@ -179,10 +180,14 @@ else:
f63228
         def __init__(self, sslobj):
f63228
             self.sslobj = sslobj
f63228
 
f63228
-        def readline(self):
f63228
+        def readline(self, size=-1):
f63228
+            if size < 0:
f63228
+                size = None
f63228
             str = ""
f63228
             chr = None
f63228
             while chr != "\n":
f63228
+                if size is not None and len(str) >= size:
f63228
+                    break
f63228
                 chr = self.sslobj.read(1)
f63228
                 if not chr:
f63228
                     break
f63228
@@ -353,7 +358,7 @@ class SMTP:
f63228
             self.file = self.sock.makefile('rb')
f63228
         while 1:
f63228
             try:
f63228
-                line = self.file.readline()
f63228
+                line = self.file.readline(_MAXLINE + 1)
f63228
             except socket.error as e:
f63228
                 self.close()
f63228
                 raise SMTPServerDisconnected("Connection unexpectedly closed: "
f63228
@@ -363,6 +368,8 @@ class SMTP:
f63228
                 raise SMTPServerDisconnected("Connection unexpectedly closed")
f63228
             if self.debuglevel > 0:
f63228
                 print>>stderr, 'reply:', repr(line)
f63228
+            if len(line) > _MAXLINE:
f63228
+                raise SMTPResponseException(500, "Line too long.")
f63228
             resp.append(line[4:].strip())
f63228
             code = line[:3]
f63228
             # Check that the error code is syntactically correct.
f63228
diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py
f63228
--- a/Lib/test/test_smtplib.py
f63228
+++ b/Lib/test/test_smtplib.py
f63228
@@ -292,6 +292,33 @@ class BadHELOServerTests(unittest.TestCa
f63228
                             HOST, self.port, 'localhost', 3)
f63228
 
f63228
 
f63228
+@unittest.skipUnless(threading, 'Threading required for this test.')
f63228
+class TooLongLineTests(unittest.TestCase):
f63228
+    respdata = '250 OK' + ('.' * smtplib._MAXLINE * 2) + '\n'
f63228
+
f63228
+    def setUp(self):
f63228
+        self.old_stdout = sys.stdout
f63228
+        self.output = StringIO.StringIO()
f63228
+        sys.stdout = self.output
f63228
+
f63228
+        self.evt = threading.Event()
f63228
+        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
f63228
+        self.sock.settimeout(15)
f63228
+        self.port = test_support.bind_port(self.sock)
f63228
+        servargs = (self.evt, self.respdata, self.sock)
f63228
+        threading.Thread(target=server, args=servargs).start()
f63228
+        self.evt.wait()
f63228
+        self.evt.clear()
f63228
+
f63228
+    def tearDown(self):
f63228
+        self.evt.wait()
f63228
+        sys.stdout = self.old_stdout
f63228
+
f63228
+    def testLineTooLong(self):
f63228
+        self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP,
f63228
+                          HOST, self.port, 'localhost', 3)
f63228
+
f63228
+
f63228
 sim_users = {'Mr.A@somewhere.com':'John A',
f63228
              'Ms.B@somewhere.com':'Sally B',
f63228
              'Mrs.C@somewhereesle.com':'Ruth C',
f63228
@@ -526,7 +553,8 @@ class SMTPSimTests(unittest.TestCase):
f63228
 def test_main(verbose=None):
f63228
     test_support.run_unittest(GeneralTests, DebuggingServerTests,
f63228
                               NonConnectingTests,
f63228
-                              BadHELOServerTests, SMTPSimTests)
f63228
+                              BadHELOServerTests, SMTPSimTests,
f63228
+                              TooLongLineTests)
f63228
 
f63228
 if __name__ == '__main__':
f63228
     test_main()