diff -r 2a38df26e009 Lib/httplib.py --- a/Lib/httplib.py Tue Sep 03 11:39:06 2013 -0500 +++ b/Lib/httplib.py Wed Sep 04 12:08:42 2013 +0200 @@ -214,6 +214,8 @@ # maximal line length when calling readline(). _MAXLINE = 65536 +_MAXHEADERS = 100 + class HTTPMessage(mimetools.Message): @@ -271,6 +273,8 @@ elif self.seekable: tell = self.fp.tell while True: + if len(hlist) > _MAXHEADERS: + raise TooManyHeaders() if tell: try: startofline = tell() @@ -1270,6 +1274,11 @@ HTTPException.__init__(self, "got more than %d bytes when reading %s" % (_MAXLINE, line_type)) + +class TooManyHeaders(HTTPException): + def __init__(self): + HTTPException.__init__(self, "got more than %d headers" % _MAXHEADERS) + # for backwards compatibility error = HTTPException diff -r 2a38df26e009 Lib/test/test_httplib.py --- a/Lib/test/test_httplib.py Tue Sep 03 11:39:06 2013 -0500 +++ b/Lib/test/test_httplib.py Wed Sep 04 12:08:42 2013 +0200 @@ -255,6 +255,13 @@ 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 xrange(200)) + '\r\n' + text = ('HTTP/1.1 200 OK\r\n' + headers) + s = FakeSocket(text) + r = httplib.HTTPResponse(s) + self.assertRaises(httplib.TooManyHeaders, r.begin) + def test_send_file(self): expected = 'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \ 'Accept-Encoding: identity\r\nContent-Length:'