diff --git i/Lib/http/client.py w/Lib/http/client.py index 5466d06..bac1eb6 100644 --- i/Lib/http/client.py +++ w/Lib/http/client.py @@ -206,6 +206,8 @@ MAXAMOUNT = 1048576 # maximal line length when calling readline(). _MAXLINE = 65536 +_MAXHEADERS = 100 + class HTTPMessage(email.message.Message): # XXX The only usage of this method is in @@ -253,6 +255,8 @@ def parse_headers(fp, _class=HTTPMessage): if len(line) > _MAXLINE: raise LineTooLong("header line") headers.append(line) + if len(headers) > _MAXHEADERS: + raise TooMuchHeaders() if line in (b'\r\n', b'\n', b''): break hstring = b''.join(headers).decode('iso-8859-1') @@ -1192,5 +1196,10 @@ class LineTooLong(HTTPException): HTTPException.__init__(self, "got more than %d bytes when reading %s" % (_MAXLINE, line_type)) + +class TooMuchHeaders(HTTPException): + def __init__(self): + HTTPException.__init__(self, "got more than %d headers" % _MAXHEADERS) + # for backwards compatibility error = HTTPException diff --git i/Lib/test/test_httplib.py w/Lib/test/test_httplib.py index 420302c..99370fb 100644 --- i/Lib/test/test_httplib.py +++ w/Lib/test/test_httplib.py @@ -272,6 +272,13 @@ class BasicTest(TestCase): if resp.read(): self.fail("Did not expect response from HEAD request") + def test_too_many_headers(self): + headers = '\r\n'.join('Header%d: foo' % i for i in range(200)) + '\r\n' + text = ('HTTP/1.1 200 OK\r\n' + headers) + s = FakeSocket(text) + r = client.HTTPResponse(s) + self.assertRaises(client.TooMuchHeaders, r.begin) + def test_send_file(self): expected = (b'GET /foo HTTP/1.1\r\nHost: example.com\r\n' b'Accept-Encoding: identity\r\nContent-Length:')