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