Skip to content

Commit ff1bbba

Browse files
committed
Merged revisions 87373,87381 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r87373 | senthil.kumaran | 2010-12-18 17:55:23 +0100 (sam., 18 déc. 2010) | 3 lines Fix Issue6791 - Limit the HTTP header readline with _MAXLENGTH. Patch by Antoine Pitrou ........ r87381 | antoine.pitrou | 2010-12-18 18:59:18 +0100 (sam., 18 déc. 2010) | 3 lines NEWS entry for r87373 ........
1 parent a2eb94b commit ff1bbba

5 files changed

Lines changed: 68 additions & 7 deletions

File tree

Lib/http/client.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@
203203
# maximal amount of data to read at one time in _safe_read
204204
MAXAMOUNT = 1048576
205205

206+
# maximal line length when calling readline().
207+
_MAXLINE = 65536
208+
206209
class HTTPMessage(email.message.Message):
207210
# XXX The only usage of this method is in
208211
# http.server.CGIHTTPRequestHandler. Maybe move the code there so
@@ -245,7 +248,9 @@ def parse_headers(fp, _class=HTTPMessage):
245248
"""
246249
headers = []
247250
while True:
248-
line = fp.readline()
251+
line = fp.readline(_MAXLINE + 1)
252+
if len(line) > _MAXLINE:
253+
raise LineTooLong("header line")
249254
headers.append(line)
250255
if line in (b'\r\n', b'\n', b''):
251256
break
@@ -349,7 +354,10 @@ def begin(self):
349354
break
350355
# skip the header from the 100 response
351356
while True:
352-
skip = self.fp.readline().strip()
357+
skip = self.fp.readline(_MAXLINE + 1)
358+
if len(skip) > _MAXLINE:
359+
raise LineTooLong("header line")
360+
skip = skip.strip()
353361
if not skip:
354362
break
355363
if self.debuglevel > 0:
@@ -525,7 +533,9 @@ def _read_chunked(self, amt):
525533
value = []
526534
while True:
527535
if chunk_left is None:
528-
line = self.fp.readline()
536+
line = self.fp.readline(_MAXLINE + 1)
537+
if len(line) > _MAXLINE:
538+
raise LineTooLong("chunk size")
529539
i = line.find(b";")
530540
if i >= 0:
531541
line = line[:i] # strip chunk-extensions
@@ -560,7 +570,9 @@ def _read_chunked(self, amt):
560570
# read and discard trailer up to the CRLF terminator
561571
### note: we shouldn't have any trailers!
562572
while True:
563-
line = self.fp.readline()
573+
line = self.fp.readline(_MAXLINE + 1)
574+
if len(line) > _MAXLINE:
575+
raise LineTooLong("trailer line")
564576
if not line:
565577
# a vanishingly small number of sites EOF without
566578
# sending the trailer
@@ -703,7 +715,9 @@ def _tunnel(self):
703715
raise socket.error("Tunnel connection failed: %d %s" % (code,
704716
message.strip()))
705717
while True:
706-
line = response.fp.readline()
718+
line = response.fp.readline(_MAXLINE + 1)
719+
if len(line) > _MAXLINE:
720+
raise LineTooLong("header line")
707721
if line == b'\r\n':
708722
break
709723

@@ -1133,6 +1147,11 @@ def __init__(self, line):
11331147
self.args = line,
11341148
self.line = line
11351149

1150+
class LineTooLong(HTTPException):
1151+
def __init__(self, line_type):
1152+
HTTPException.__init__(self, "got more than %d bytes when reading %s"
1153+
% (_MAXLINE, line_type))
1154+
11361155
# for backwards compatibility
11371156
error = HTTPException
11381157

Lib/http/server.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,12 @@ def parse_request(self):
314314
self.command, self.path, self.request_version = command, path, version
315315

316316
# Examine the headers and look for a Connection directive.
317-
self.headers = http.client.parse_headers(self.rfile,
318-
_class=self.MessageClass)
317+
try:
318+
self.headers = http.client.parse_headers(self.rfile,
319+
_class=self.MessageClass)
320+
except http.client.LineTooLong:
321+
self.send_error(400, "Line too long")
322+
return False
319323

320324
conntype = self.headers.get('Connection', "")
321325
if conntype.lower() == 'close':

Lib/test/test_httplib.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,34 @@ def test_epipe(self):
303303
self.assertEqual("Basic realm=\"example\"",
304304
resp.getheader("www-authenticate"))
305305

306+
# Test lines overflowing the max line size (_MAXLINE in http.client)
307+
308+
def test_overflowing_status_line(self):
309+
self.skipTest("disabled for HTTP 0.9 support")
310+
body = "HTTP/1.1 200 Ok" + "k" * 65536 + "\r\n"
311+
resp = client.HTTPResponse(FakeSocket(body))
312+
self.assertRaises((client.LineTooLong, client.BadStatusLine), resp.begin)
313+
314+
def test_overflowing_header_line(self):
315+
body = (
316+
'HTTP/1.1 200 OK\r\n'
317+
'X-Foo: bar' + 'r' * 65536 + '\r\n\r\n'
318+
)
319+
resp = client.HTTPResponse(FakeSocket(body))
320+
self.assertRaises(client.LineTooLong, resp.begin)
321+
322+
def test_overflowing_chunked_line(self):
323+
body = (
324+
'HTTP/1.1 200 OK\r\n'
325+
'Transfer-Encoding: chunked\r\n\r\n'
326+
+ '0' * 65536 + 'a\r\n'
327+
'hello world\r\n'
328+
'0\r\n'
329+
)
330+
resp = client.HTTPResponse(FakeSocket(body))
331+
resp.begin()
332+
self.assertRaises(client.LineTooLong, resp.read)
333+
306334
class OfflineTest(TestCase):
307335
def test_responses(self):
308336
self.assertEqual(client.responses[client.NOT_FOUND], "Not Found")

Lib/test/test_httpservers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ def test_request_length(self):
144144
self.assertEqual(result[0], b'HTTP/1.1 414 Request-URI Too Long\r\n')
145145
self.assertFalse(self.handler.get_called)
146146

147+
def test_header_length(self):
148+
# Issue #6791: same for headers
149+
result = self.send_typical_request(
150+
b'GET / HTTP/1.1\r\nX-Foo: bar' + b'r' * 65537 + b'\r\n\r\n')
151+
self.assertEqual(result[0], b'HTTP/1.1 400 Line too long\r\n')
152+
self.assertFalse(self.handler.get_called)
153+
147154

148155
class BaseHTTPServerTestCase(BaseTestCase):
149156
class request_handler(NoLogRequestHandler, BaseHTTPRequestHandler):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Core and Builtins
2424
Library
2525
-------
2626

27+
- Issue #6791: Limit header line length (to 65535 bytes) in http.client
28+
and http.server, to avoid denial of services from the other party.
29+
2730
- Issue #10404: Use ctl-button-1 on OSX for the context menu in Idle.
2831

2932
- Issue #4188: Avoid creating dummy thread objects when logging operations

0 commit comments

Comments
 (0)