diff -r 25683ceaf341 Lib/httplib.py --- a/Lib/httplib.py Tue Sep 03 14:47:00 2013 +0200 +++ b/Lib/httplib.py Wed Sep 04 11:59:52 2013 +0200 @@ -211,6 +211,10 @@ # maximal amount of data to read at one time in _safe_read MAXAMOUNT = 1048576 +# maximum amount of headers accepted +_MAXHEADERS = 100 + + class HTTPMessage(mimetools.Message): def addheader(self, key, value): @@ -267,6 +271,8 @@ elif self.seekable: tell = self.fp.tell while True: + if len(hlist) > _MAXHEADERS: + raise TooManyHeaders() if tell: try: startofline = tell() @@ -1203,6 +1209,11 @@ self.args = line, self.line = line + +class TooManyHeaders(HTTPException): + def __init__(self): + HTTPException.__init__(self, "got more than %d headers" % _MAXHEADERS) + # for backwards compatibility error = HTTPException diff -r 25683ceaf341 Lib/test/test_httplib.py --- a/Lib/test/test_httplib.py Tue Sep 03 14:47:00 2013 +0200 +++ b/Lib/test/test_httplib.py Wed Sep 04 11:59:52 2013 +0200 @@ -152,6 +152,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:'