[Python-checkins] cpython (3.2): Issue #16042: CVE-2013-1752: smtplib: Limit amount of data read by

georg.brandl python-checkins at python.org
Tue Sep 30 14:47:04 CEST 2014


https://hg.python.org/cpython/rev/0f362676460d
changeset:   92662:0f362676460d
branch:      3.2
user:        Georg Brandl <georg at python.org>
date:        Tue Sep 30 14:18:02 2014 +0200
summary:
  Issue #16042: CVE-2013-1752: smtplib: Limit amount of data read by
limiting the call to readline().  Original patch by Christian Heimes.

files:
  Lib/smtplib.py           |   5 +++-
  Lib/test/mock_socket.py  |   9 ++++++-
  Lib/test/test_smtplib.py |  30 +++++++++++++++++++++++++++-
  Misc/NEWS                |   3 ++
  4 files changed, 43 insertions(+), 4 deletions(-)


diff --git a/Lib/smtplib.py b/Lib/smtplib.py
--- a/Lib/smtplib.py
+++ b/Lib/smtplib.py
@@ -62,6 +62,7 @@
 SMTP_SSL_PORT = 465
 CRLF = "\r\n"
 bCRLF = b"\r\n"
+_MAXLINE = 8192 # more than 8 times larger than RFC 821, 4.5.3
 
 OLDSTYLE_AUTH = re.compile(r"auth=(.*)", re.I)
 
@@ -363,7 +364,7 @@
             self.file = self.sock.makefile('rb')
         while 1:
             try:
-                line = self.file.readline()
+                line = self.file.readline(_MAXLINE + 1)
             except socket.error as e:
                 self.close()
                 raise SMTPServerDisconnected("Connection unexpectedly closed: "
@@ -373,6 +374,8 @@
                 raise SMTPServerDisconnected("Connection unexpectedly closed")
             if self.debuglevel > 0:
                 print('reply:', repr(line), file=stderr)
+            if len(line) > _MAXLINE:
+                raise SMTPResponseException(500, "Line too long.")
             resp.append(line[4:].strip(b' \t\r\n'))
             code = line[:3]
             # Check that the error code is syntactically correct.
diff --git a/Lib/test/mock_socket.py b/Lib/test/mock_socket.py
--- a/Lib/test/mock_socket.py
+++ b/Lib/test/mock_socket.py
@@ -21,8 +21,13 @@
     """
     def __init__(self, lines):
         self.lines = lines
-    def readline(self):
-        return self.lines.pop(0) + b'\r\n'
+    def readline(self, limit=-1):
+        result = self.lines.pop(0) + b'\r\n'
+        if limit >= 0:
+            # Re-insert the line, removing the \r\n we added.
+            self.lines.insert(0, result[limit:-2])
+            result = result[:limit]
+        return result
     def close(self):
         pass
 
diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py
--- a/Lib/test/test_smtplib.py
+++ b/Lib/test/test_smtplib.py
@@ -537,6 +537,33 @@
                             HOST, self.port, 'localhost', 3)
 
 
+ at unittest.skipUnless(threading, 'Threading required for this test.')
+class TooLongLineTests(unittest.TestCase):
+    respdata = b'250 OK' + (b'.' * smtplib._MAXLINE * 2) + b'\n'
+
+    def setUp(self):
+        self.old_stdout = sys.stdout
+        self.output = io.StringIO()
+        sys.stdout = self.output
+
+        self.evt = threading.Event()
+        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        self.sock.settimeout(15)
+        self.port = support.bind_port(self.sock)
+        servargs = (self.evt, self.respdata, self.sock)
+        threading.Thread(target=server, args=servargs).start()
+        self.evt.wait()
+        self.evt.clear()
+
+    def tearDown(self):
+        self.evt.wait()
+        sys.stdout = self.old_stdout
+
+    def testLineTooLong(self):
+        self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP,
+                          HOST, self.port, 'localhost', 3)
+
+
 sim_users = {'Mr.A at somewhere.com':'John A',
              'Ms.B at xn--fo-fka.com':'Sally B',
              'Mrs.C at somewhereesle.com':'Ruth C',
@@ -826,7 +853,8 @@
 def test_main(verbose=None):
     support.run_unittest(GeneralTests, DebuggingServerTests,
                               NonConnectingTests,
-                              BadHELOServerTests, SMTPSimTests)
+                              BadHELOServerTests, SMTPSimTests,
+                              TooLongLineTests)
 
 if __name__ == '__main__':
     test_main()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@
 Library
 -------
 
+- Issue #16042: CVE-2013-1752: smtplib: Limit amount of data read by
+  limiting the call to readline().  Original patch by Christian Heimes.
+
 - Issue #16038: CVE-2013-1752: ftplib: Limit amount of data read by
   limiting the call to readline().  Original patch by Michał
   Jastrzębski and Giampaolo Rodola.

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list