]> granicus.if.org Git - python/commitdiff
Issue #16037: HTTPMessage.readheaders() raises an HTTPException when more than
authorGeorg Brandl <georg@python.org>
Sun, 27 Oct 2013 06:34:48 +0000 (07:34 +0100)
committerGeorg Brandl <georg@python.org>
Sun, 27 Oct 2013 06:34:48 +0000 (07:34 +0100)
100 headers are read.  Adapted from patch by Jyrki Pulliainen.

Doc/library/http.client.rst
Lib/http/client.py
Lib/test/test_httplib.py
Misc/NEWS

index 6931fc6a7ffbf3a32a014ec33626f0b02799e43d..4daeeeaa9477daa52fa34975ad4b06ce982920b4 100644 (file)
@@ -169,8 +169,8 @@ The following exceptions are raised as appropriate:
    A subclass of :exc:`HTTPException`.  Raised if a server responds with a HTTP
    status code that we don't understand.
 
-The constants defined in this module are:
 
+The constants defined in this module are:
 
 .. data:: HTTP_PORT
 
index b72cf0891e5b67dff4e119654bfe53784c6b18c0..cc452e2f52e374ebd6f4e52dc72bb4515e337104 100644 (file)
@@ -214,6 +214,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
@@ -261,6 +263,8 @@ def parse_headers(fp, _class=HTTPMessage):
         if len(line) > _MAXLINE:
             raise LineTooLong("header line")
         headers.append(line)
+        if len(headers) > _MAXHEADERS:
+            raise HTTPException("got more than %d headers" % _MAXHEADERS)
         if line in (b'\r\n', b'\n', b''):
             break
     hstring = b''.join(headers).decode('iso-8859-1')
index 769ab13fca7d12b1e809ed2352cb9733e5af0f6f..f3c27c2df6aefc1e11d639c651d9fc15e419d1d5 100644 (file)
@@ -345,6 +345,15 @@ class BasicTest(TestCase):
             self.fail("Did not expect response from HEAD request")
         self.assertEqual(bytes(b), b'\x00'*5)
 
+    def test_too_many_headers(self):
+        headers = '\r\n'.join('Header%d: foo' % i
+                              for i in range(client._MAXHEADERS + 1)) + '\r\n'
+        text = ('HTTP/1.1 200 OK\r\n' + headers)
+        s = FakeSocket(text)
+        r = client.HTTPResponse(s)
+        self.assertRaisesRegex(client.HTTPException,
+                               r"got more than \d+ headers", 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:')
index 24f184743423fe06b01d6accfc8762681ea97c3e..6d01b355991fec86d00c14c75db82583ae73af6b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -81,6 +81,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #16037: HTTPMessage.readheaders() raises an HTTPException when more than
+  100 headers are read.  Adapted from patch by Jyrki Pulliainen.
+
 - Issue #16040: CVE-2013-1752: nntplib: Limit maximum line lengths to 2048 to
   prevent readline() calls from consuming too much memory.  Patch by Jyrki
   Pulliainen.