cpython: 8a6def3add5b (original) (raw)
Mercurial > cpython
changeset 85714:8a6def3add5b 2.6
#16042: CVE-2013-1752: Limit amount of data read by limiting the call to readline(). The SSLFakeFile.readline() method needs to support limiting readline() as well. It's not a full emulation of readline()'s signature, but this class is only used by smtplib's code, so it doesn't have to be. Modified version of original patch by Christian Heimes. [#16042]
Andrew Kuchling amk@amk.ca | |
---|---|
date | Sun, 15 Sep 2013 13:11:47 -0400 |
parents | 07ee48ce4513 |
children | a9f147749b68 e5c4eb6b8e05 |
files | Lib/smtplib.py Lib/test/test_smtplib.py Misc/NEWS |
diffstat | 3 files changed, 42 insertions(+), 5 deletions(-)[+] [-] Lib/smtplib.py 13 Lib/test/test_smtplib.py 29 Misc/NEWS 5 |
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) @@ -170,10 +171,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: break[](#l1.25) str += chr[](#l1.26)
@@ -334,11 +339,13 @@ class SMTP: if self.file is None: self.file = self.sock.makefile('rb') while 1:
line = self.file.readline()[](#l1.31)
line = self.file.readline(_MAXLINE + 1)[](#l1.32) if line == '':[](#l1.33) self.close()[](#l1.34) raise SMTPServerDisconnected("Connection unexpectedly closed")[](#l1.35)
if self.debuglevel > 0: print>>stderr, 'reply:', repr(line)[](#l1.36)
if self.debuglevel > 0: print >>stderr, 'reply:', repr(line)[](#l1.37)
if len(line) > _MAXLINE:[](#l1.38)
raise SMTPResponseException(500, "Line too long.")[](#l1.39) resp.append(line[4:].strip())[](#l1.40) code=line[:3][](#l1.41) # Check that the error code is syntactically correct.[](#l1.42)
--- a/Lib/test/test_smtplib.py +++ b/Lib/test/test_smtplib.py @@ -273,6 +273,32 @@ class BadHELOServerTests(TestCase): HOST, self.port, 'localhost', 3) +class TooLongLineTests(TestCase):
- def setUp(self):
self.old_stdout = sys.stdout[](#l2.11)
self.output = StringIO.StringIO()[](#l2.12)
sys.stdout = self.output[](#l2.13)
self.evt = threading.Event()[](#l2.15)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)[](#l2.16)
self.sock.settimeout(15)[](#l2.17)
self.port = test_support.bind_port(self.sock)[](#l2.18)
servargs = (self.evt, self.respdata, self.sock)[](#l2.19)
threading.Thread(target=server, args=servargs).start()[](#l2.20)
self.evt.wait()[](#l2.21)
self.evt.clear()[](#l2.22)
- def testLineTooLong(self):
self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP,[](#l2.29)
HOST, self.port, 'localhost', 3)[](#l2.30)
+ + sim_users = {'Mr.A@somewhere.com':'John A', 'Ms.B@somewhere.com':'Sally B', 'Mrs.C@somewhereesle.com':'Ruth C', @@ -482,7 +508,8 @@ class SMTPSimTests(TestCase): def test_main(verbose=None): test_support.run_unittest(GeneralTests, DebuggingServerTests, NonConnectingTests,
BadHELOServerTests, SMTPSimTests)[](#l2.40)
BadHELOServerTests, SMTPSimTests,[](#l2.41)
TooLongLineTests)[](#l2.42)
if name == 'main': test_main()
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -16,13 +16,16 @@ Library
- Issue #16248: Disable code execution from the user's home directory by tkinter when the -E flag is passed to Python. Patch by Zachary Ware. +- Issue #16042: CVE-2013-1752: smtplib: Limit amount of data read by
- limiting the call to readline(). Original patch by Christian Heimes. + Extension Modules -----------------