cpython: 923aac88a3cc (original) (raw)
Mercurial > cpython
changeset 93747:923aac88a3cc 2.7
smtplib: limit amount read from the network (closes #16042) [#16042]
Benjamin Peterson benjamin@python.org | |
---|---|
date | Fri, 05 Dec 2014 20:05:18 -0500 |
parents | 339f877cca11 |
children | d50096708b2d |
files | Lib/smtplib.py Lib/test/test_smtplib.py Misc/NEWS |
diffstat | 3 files changed, 41 insertions(+), 3 deletions(-)[+] [-] Lib/smtplib.py 11 Lib/test/test_smtplib.py 30 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Lib/smtplib.py +++ b/Lib/smtplib.py @@ -57,6 +57,7 @@ from sys import stderr SMTP_PORT = 25 SMTP_SSL_PORT = 465 CRLF = "\r\n" +_MAXLINE = 8192 # more than 8 times larger than RFC 821, 4.5.3 OLDSTYLE_AUTH = re.compile(r"auth=(.*)", re.I) @@ -179,10 +180,14 @@ else: def init(self, sslobj): self.sslobj = sslobj
def readline(self):[](#l1.15)
def readline(self, size=-1):[](#l1.16)
if size < 0:[](#l1.17)
size = None[](#l1.18) str = ""[](#l1.19) chr = None[](#l1.20) while chr != "\n":[](#l1.21)
if size is not None and len(str) >= size:[](#l1.22)
break[](#l1.23) chr = self.sslobj.read(1)[](#l1.24) if not chr:[](#l1.25) break[](#l1.26)
@@ -353,7 +358,7 @@ class SMTP: self.file = self.sock.makefile('rb') while 1: try:
line = self.file.readline()[](#l1.31)
line = self.file.readline(_MAXLINE + 1)[](#l1.32) except socket.error as e:[](#l1.33) self.close()[](#l1.34) raise SMTPServerDisconnected("Connection unexpectedly closed: "[](#l1.35)
@@ -363,6 +368,8 @@ class SMTP: raise SMTPServerDisconnected("Connection unexpectedly closed") if self.debuglevel > 0: print>>stderr, 'reply:', repr(line)
if len(line) > _MAXLINE:[](#l1.40)
raise SMTPResponseException(500, "Line too long.")[](#l1.41) resp.append(line[4:].strip())[](#l1.42) code = line[:3][](#l1.43) # Check that the error code is syntactically correct.[](#l1.44)
--- a/Lib/test/test_smtplib.py +++ b/Lib/test/test_smtplib.py @@ -292,6 +292,33 @@ class BadHELOServerTests(unittest.TestCa HOST, self.port, 'localhost', 3) +@unittest.skipUnless(threading, 'Threading required for this test.') +class TooLongLineTests(unittest.TestCase):
- def setUp(self):
self.old_stdout = sys.stdout[](#l2.12)
self.output = StringIO.StringIO()[](#l2.13)
sys.stdout = self.output[](#l2.14)
self.evt = threading.Event()[](#l2.16)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)[](#l2.17)
self.sock.settimeout(15)[](#l2.18)
self.port = test_support.bind_port(self.sock)[](#l2.19)
servargs = (self.evt, self.respdata, self.sock)[](#l2.20)
threading.Thread(target=server, args=servargs).start()[](#l2.21)
self.evt.wait()[](#l2.22)
self.evt.clear()[](#l2.23)
- def testLineTooLong(self):
self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP,[](#l2.30)
HOST, self.port, 'localhost', 3)[](#l2.31)
+ + sim_users = {'Mr.A@somewhere.com':'John A', 'Ms.B@somewhere.com':'Sally B', 'Mrs.C@somewhereesle.com':'Ruth C', @@ -526,7 +553,8 @@ class SMTPSimTests(unittest.TestCase): def test_main(verbose=None): test_support.run_unittest(GeneralTests, DebuggingServerTests, NonConnectingTests,
BadHELOServerTests, SMTPSimTests)[](#l2.41)
BadHELOServerTests, SMTPSimTests,[](#l2.42)
TooLongLineTests)[](#l2.43)
if name == 'main': test_main()
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 2.7.9? Library ------- +- Issue #16042: CVE-2013-1752: smtplib: Limit amount of data read by limiting