|
|
21eb11 |
|
|
|
21eb11 |
# HG changeset patch
|
|
|
21eb11 |
# User Benjamin Peterson <benjamin@python.org>
|
|
|
21eb11 |
# Date 1417827758 18000
|
|
|
21eb11 |
# Node ID 339f877cca115c1901f5dd93d7bc066031d2a669
|
|
|
21eb11 |
# Parent 54af094087953f4997a4ead63e949d845c4b4412
|
|
|
21eb11 |
in poplib, limit maximum line length that we read from the network (closes #16041)
|
|
|
21eb11 |
|
|
|
21eb11 |
Patch from Berker Peksag.
|
|
|
21eb11 |
|
|
|
21eb11 |
diff --git a/Lib/poplib.py b/Lib/poplib.py
|
|
|
21eb11 |
--- a/Lib/poplib.py
|
|
|
21eb11 |
+++ b/Lib/poplib.py
|
|
|
21eb11 |
@@ -32,6 +32,12 @@ CR = '\r'
|
|
|
21eb11 |
LF = '\n'
|
|
|
21eb11 |
CRLF = CR+LF
|
|
|
21eb11 |
|
|
|
21eb11 |
+# maximal line length when calling readline(). This is to prevent
|
|
|
21eb11 |
+# reading arbitrary length lines. RFC 1939 limits POP3 line length to
|
|
|
21eb11 |
+# 512 characters, including CRLF. We have selected 2048 just to be on
|
|
|
21eb11 |
+# the safe side.
|
|
|
21eb11 |
+_MAXLINE = 2048
|
|
|
21eb11 |
+
|
|
|
21eb11 |
|
|
|
21eb11 |
class POP3:
|
|
|
21eb11 |
|
|
|
21eb11 |
@@ -103,7 +109,9 @@ class POP3:
|
|
|
21eb11 |
# Raise error_proto('-ERR EOF') if the connection is closed.
|
|
|
21eb11 |
|
|
|
21eb11 |
def _getline(self):
|
|
|
21eb11 |
- line = self.file.readline()
|
|
|
21eb11 |
+ line = self.file.readline(_MAXLINE + 1)
|
|
|
21eb11 |
+ if len(line) > _MAXLINE:
|
|
|
21eb11 |
+ raise error_proto('line too long')
|
|
|
21eb11 |
if self._debugging > 1: print '*get*', repr(line)
|
|
|
21eb11 |
if not line: raise error_proto('-ERR EOF')
|
|
|
21eb11 |
octets = len(line)
|
|
|
21eb11 |
@@ -365,6 +373,8 @@ else:
|
|
|
21eb11 |
match = renewline.match(self.buffer)
|
|
|
21eb11 |
while not match:
|
|
|
21eb11 |
self._fillBuffer()
|
|
|
21eb11 |
+ if len(self.buffer) > _MAXLINE:
|
|
|
21eb11 |
+ raise error_proto('line too long')
|
|
|
21eb11 |
match = renewline.match(self.buffer)
|
|
|
21eb11 |
line = match.group(0)
|
|
|
21eb11 |
self.buffer = renewline.sub('' ,self.buffer, 1)
|
|
|
21eb11 |
diff --git a/Lib/test/test_poplib.py b/Lib/test/test_poplib.py
|
|
|
21eb11 |
--- a/Lib/test/test_poplib.py
|
|
|
21eb11 |
+++ b/Lib/test/test_poplib.py
|
|
|
21eb11 |
@@ -198,6 +198,10 @@ class TestPOP3Class(TestCase):
|
|
|
21eb11 |
113)
|
|
|
21eb11 |
self.assertEqual(self.client.retr('foo'), expected)
|
|
|
21eb11 |
|
|
|
21eb11 |
+ def test_too_long_lines(self):
|
|
|
21eb11 |
+ self.assertRaises(poplib.error_proto, self.client._shortcmd,
|
|
|
21eb11 |
+ 'echo +%s' % ((poplib._MAXLINE + 10) * 'a'))
|
|
|
21eb11 |
+
|
|
|
21eb11 |
def test_dele(self):
|
|
|
21eb11 |
self.assertOK(self.client.dele('foo'))
|
|
|
21eb11 |
|
|
|
21eb11 |
|
|
|
21eb11 |
|
|
|
21eb11 |
# HG changeset patch
|
|
|
21eb11 |
# User Benjamin Peterson <benjamin@python.org>
|
|
|
21eb11 |
# Date 1417827918 18000
|
|
|
21eb11 |
# Node ID 923aac88a3cc76a95d5a04d9d3ece245147a8064
|
|
|
21eb11 |
# Parent 339f877cca115c1901f5dd93d7bc066031d2a669
|
|
|
21eb11 |
smtplib: limit amount read from the network (closes #16042)
|
|
|
21eb11 |
|
|
|
21eb11 |
diff --git a/Lib/smtplib.py b/Lib/smtplib.py
|
|
|
21eb11 |
--- a/Lib/smtplib.py
|
|
|
21eb11 |
+++ b/Lib/smtplib.py
|
|
|
21eb11 |
@@ -57,6 +57,7 @@ from sys import stderr
|
|
|
21eb11 |
SMTP_PORT = 25
|
|
|
21eb11 |
SMTP_SSL_PORT = 465
|
|
|
21eb11 |
CRLF = "\r\n"
|
|
|
21eb11 |
+_MAXLINE = 8192 # more than 8 times larger than RFC 821, 4.5.3
|
|
|
21eb11 |
|
|
|
21eb11 |
OLDSTYLE_AUTH = re.compile(r"auth=(.*)", re.I)
|
|
|
21eb11 |
|
|
|
21eb11 |
@@ -179,10 +180,14 @@ else:
|
|
|
21eb11 |
def __init__(self, sslobj):
|
|
|
21eb11 |
self.sslobj = sslobj
|
|
|
21eb11 |
|
|
|
21eb11 |
- def readline(self):
|
|
|
21eb11 |
+ def readline(self, size=-1):
|
|
|
21eb11 |
+ if size < 0:
|
|
|
21eb11 |
+ size = None
|
|
|
21eb11 |
str = ""
|
|
|
21eb11 |
chr = None
|
|
|
21eb11 |
while chr != "\n":
|
|
|
21eb11 |
+ if size is not None and len(str) >= size:
|
|
|
21eb11 |
+ break
|
|
|
21eb11 |
chr = self.sslobj.read(1)
|
|
|
21eb11 |
if not chr:
|
|
|
21eb11 |
break
|
|
|
21eb11 |
@@ -353,7 +358,7 @@ class SMTP:
|
|
|
21eb11 |
self.file = self.sock.makefile('rb')
|
|
|
21eb11 |
while 1:
|
|
|
21eb11 |
try:
|
|
|
21eb11 |
- line = self.file.readline()
|
|
|
21eb11 |
+ line = self.file.readline(_MAXLINE + 1)
|
|
|
21eb11 |
except socket.error as e:
|
|
|
21eb11 |
self.close()
|
|
|
21eb11 |
raise SMTPServerDisconnected("Connection unexpectedly closed: "
|
|
|
21eb11 |
@@ -363,6 +368,8 @@ class SMTP:
|
|
|
21eb11 |
raise SMTPServerDisconnected("Connection unexpectedly closed")
|
|
|
21eb11 |
if self.debuglevel > 0:
|
|
|
21eb11 |
print>>stderr, 'reply:', repr(line)
|
|
|
21eb11 |
+ if len(line) > _MAXLINE:
|
|
|
21eb11 |
+ raise SMTPResponseException(500, "Line too long.")
|
|
|
21eb11 |
resp.append(line[4:].strip())
|
|
|
21eb11 |
code = line[:3]
|
|
|
21eb11 |
# Check that the error code is syntactically correct.
|
|
|
21eb11 |
diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py
|
|
|
21eb11 |
--- a/Lib/test/test_smtplib.py
|
|
|
21eb11 |
+++ b/Lib/test/test_smtplib.py
|
|
|
21eb11 |
@@ -292,6 +292,33 @@ class BadHELOServerTests(unittest.TestCa
|
|
|
21eb11 |
HOST, self.port, 'localhost', 3)
|
|
|
21eb11 |
|
|
|
21eb11 |
|
|
|
21eb11 |
+@unittest.skipUnless(threading, 'Threading required for this test.')
|
|
|
21eb11 |
+class TooLongLineTests(unittest.TestCase):
|
|
|
21eb11 |
+ respdata = '250 OK' + ('.' * smtplib._MAXLINE * 2) + '\n'
|
|
|
21eb11 |
+
|
|
|
21eb11 |
+ def setUp(self):
|
|
|
21eb11 |
+ self.old_stdout = sys.stdout
|
|
|
21eb11 |
+ self.output = StringIO.StringIO()
|
|
|
21eb11 |
+ sys.stdout = self.output
|
|
|
21eb11 |
+
|
|
|
21eb11 |
+ self.evt = threading.Event()
|
|
|
21eb11 |
+ self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
21eb11 |
+ self.sock.settimeout(15)
|
|
|
21eb11 |
+ self.port = test_support.bind_port(self.sock)
|
|
|
21eb11 |
+ servargs = (self.evt, self.respdata, self.sock)
|
|
|
21eb11 |
+ threading.Thread(target=server, args=servargs).start()
|
|
|
21eb11 |
+ self.evt.wait()
|
|
|
21eb11 |
+ self.evt.clear()
|
|
|
21eb11 |
+
|
|
|
21eb11 |
+ def tearDown(self):
|
|
|
21eb11 |
+ self.evt.wait()
|
|
|
21eb11 |
+ sys.stdout = self.old_stdout
|
|
|
21eb11 |
+
|
|
|
21eb11 |
+ def testLineTooLong(self):
|
|
|
21eb11 |
+ self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP,
|
|
|
21eb11 |
+ HOST, self.port, 'localhost', 3)
|
|
|
21eb11 |
+
|
|
|
21eb11 |
+
|
|
|
21eb11 |
sim_users = {'Mr.A@somewhere.com':'John A',
|
|
|
21eb11 |
'Ms.B@somewhere.com':'Sally B',
|
|
|
21eb11 |
'Mrs.C@somewhereesle.com':'Ruth C',
|
|
|
21eb11 |
@@ -526,7 +553,8 @@ class SMTPSimTests(unittest.TestCase):
|
|
|
21eb11 |
def test_main(verbose=None):
|
|
|
21eb11 |
test_support.run_unittest(GeneralTests, DebuggingServerTests,
|
|
|
21eb11 |
NonConnectingTests,
|
|
|
21eb11 |
- BadHELOServerTests, SMTPSimTests)
|
|
|
21eb11 |
+ BadHELOServerTests, SMTPSimTests,
|
|
|
21eb11 |
+ TooLongLineTests)
|
|
|
21eb11 |
|
|
|
21eb11 |
if __name__ == '__main__':
|
|
|
21eb11 |
test_main()
|
|
|
21eb11 |
|
|
|
21eb11 |
diff --git a/Lib/httplib.py b/Lib/httplib.py
|
|
|
21eb11 |
--- a/Lib/httplib.py
|
|
|
21eb11 |
+++ b/Lib/httplib.py
|
|
|
21eb11 |
@@ -211,6 +211,10 @@ responses = {
|
|
|
21eb11 |
# maximal line length when calling readline().
|
|
|
21eb11 |
_MAXLINE = 65536
|
|
|
21eb11 |
|
|
|
21eb11 |
+# maximum amount of headers accepted
|
|
|
21eb11 |
+_MAXHEADERS = 100
|
|
|
21eb11 |
+
|
|
|
21eb11 |
+
|
|
|
21eb11 |
class HTTPMessage(mimetools.Message):
|
|
|
21eb11 |
|
|
|
21eb11 |
def addheader(self, key, value):
|
|
|
21eb11 |
@@ -267,6 +271,8 @@ class HTTPMessage(mimetools.Message):
|
|
|
21eb11 |
elif self.seekable:
|
|
|
21eb11 |
tell = self.fp.tell
|
|
|
21eb11 |
while True:
|
|
|
21eb11 |
+ if len(hlist) > _MAXHEADERS:
|
|
|
21eb11 |
+ raise HTTPException("got more than %d headers" % _MAXHEADERS)
|
|
|
21eb11 |
if tell:
|
|
|
21eb11 |
try:
|
|
|
21eb11 |
startofline = tell()
|
|
|
21eb11 |
|
|
|
21eb11 |
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
|
|
|
21eb11 |
--- a/Lib/test/test_httplib.py
|
|
|
21eb11 |
+++ b/Lib/test/test_httplib.py
|
|
|
21eb11 |
@@ -152,6 +152,13 @@ class BasicTest(TestCase):
|
|
|
21eb11 |
if resp.read() != "":
|
|
|
21eb11 |
self.fail("Did not expect response from HEAD request")
|
|
|
21eb11 |
|
|
|
21eb11 |
+ def test_too_many_headers(self):
|
|
|
21eb11 |
+ headers = '\r\n'.join('Header%d: foo' % i for i in xrange(200)) + '\r\n'
|
|
|
21eb11 |
+ text = ('HTTP/1.1 200 OK\r\n' + headers)
|
|
|
21eb11 |
+ s = FakeSocket(text)
|
|
|
21eb11 |
+ r = httplib.HTTPResponse(s)
|
|
|
21eb11 |
+ self.assertRaises(httplib.HTTPException, r.begin)
|
|
|
21eb11 |
+
|
|
|
21eb11 |
def test_send_file(self):
|
|
|
21eb11 |
expected = 'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \
|
|
|
21eb11 |
'Accept-Encoding: identity\r\nContent-Length:'
|