8000 Issue #16042: CVE-2013-1752: smtplib: Limit amount of data read by · python/cpython@210ee47 · GitHub
[go: up one dir, main page]

Skip to content

Commit 210ee47

Browse files
committed
Issue #16042: CVE-2013-1752: smtplib: Limit amount of data read by
limiting the call to readline(). Original patch by Christian Heimes.
1 parent 70088f1 commit 210ee47

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

Lib/smtplib.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
SMTP_SSL_PORT = 465
6363
CRLF = "\r\n"
6464
bCRLF = b"\r\n"
65+
_MAXLINE = 8192 # more than 8 times larger than RFC 821, 4.5.3
6566

6667
OLDSTYLE_AUTH = re.compile(r"auth=(.*)", re.I)
6768

@@ -363,7 +364,7 @@ def getreply(self):
363364
self.file = self.sock.makefile('rb')
364365
while 1:
365366
try:
366-
line = self.file.readline()
367+
line = self.file.readline(_MAXLINE + 1)
367368
except socket.error as e:
368369
self.close()
369370
raise SMTPServerDisconnected("Connection unexpectedly closed: "
@@ -373,6 +374,8 @@ def getreply(self):
373374
raise SMTPServerDisconnected("Connection unexpectedly closed")
374375
if self.debuglevel > 0:
375376
print('reply:', repr(line), file=stderr)
377+
if len(line) > _MAXLINE:
378+
raise SMTPResponseException(500, "Line too long.")
376379
resp.append(line[4:].strip(b' \t\r\n'))
377380
code = line[:3]
378381
# Check that the error code is syntactically correct.

Lib/test/mock_socket.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ class MockFile:
2121
"""
2222
def __init__(self, lines):
2323
self.lines = lines
24-
def readline(self):
25-
return self.lines.pop(0) + b'\r\n'
24+
def readline(self, limit=-1):
25+
result = self.lines.pop(0) + b'\r\n'
26+
if limit >= 0:
27+
# Re-insert the line, removing the \r\n we added.
28+
self.lines.insert(0, result[limit:-2])
29+
result = result[:limit]
30+
return result
2631
def close(self):
2732
pass
2833

Lib/test/test_smtplib.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,33 @@ def testFailingHELO(self):
537537
HOST, self.port, 'localhost', 3)
538538

539539

540+
@unittest.skipUnless(threading, 'Threading required for this test.')
541+
class TooLongLineTests(unittest.TestCase):
542+
respdata = b'250 OK' + (b'.' * smtplib._MAXLINE * 2) + b'\n'
543+
544+
def setUp(self):
545+
self.old_stdout = sys.stdout
546+
self.output = io.StringIO()
547+
sys.stdout = self.output
548+
549+
self.evt = threading.Event()
550+
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
551+
self.sock.settimeout(15)
552+
self.port = support.bind_port(self.sock)
553+
servargs = (self.evt, self.respdata, self.sock)
554+
threading.Thread(target=server, args=servargs).start()
555+
self.evt.wait()
556+
self.evt.clear()
557+
558+
def tearDown(self):
559+
self.evt.wait()
560+
sys.stdout = self.old_stdout
561+
562+
def testLineTooLong(self):
563+
self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP,
564+
HOST, self.port, 'localhost', 3)
565+
566+
540567
sim_users = {'Mr.A@somewhere.com':'John A',
541568
'Ms.B@xn--fo-fka.com':'Sally B',
542569
'Mrs.C@somewhereesle.com':'Ruth C',
@@ -826,7 +853,8 @@ def found_terminator(self):
826853
def test_main(verbose=None):
827854
support.run_unittest(GeneralTests, DebuggingServerTests,
828855
NonConnectingTests,
829-
BadHELOServerTests, SMTPSimTests)
856+
BadHELOServerTests, SMTPSimTests,
857+
TooLongLineTests)
830858

831859
if __name__ == '__main__':
832860
test_main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.6?
1010
Library
1111
-------
1212

13+
- Issue #16042: CVE-2013-1752: smtplib: Limit amount of data read by
14+
limiting the call to readline(). Original patch by Christian Heimes.
15+
1316
- Issue #16038: CVE-2013-1752: ftplib: Limit amount of data read by
1417
limiting the call to readline(). Original patch by Michał
1518
Jastrzębski and Giampaolo Rodola.

0 commit comments

Comments
 (0)
0