]> granicus.if.org Git - python/commitdiff
- Issue #16037: HTTPMessage.readheaders() raises an HTTPException when more
authorBarry Warsaw <barry@python.org>
Sun, 29 Sep 2013 17:59:06 +0000 (13:59 -0400)
committerBarry Warsaw <barry@python.org>
Sun, 29 Sep 2013 17:59:06 +0000 (13:59 -0400)
  than 100 headers are read.  Adapted from patch by Jyrki Pulliainen.

Lib/httplib.py
Lib/test/test_httplib.py
Misc/NEWS

index 680e8750413d9f22ff1aba12e2394834622d37ec..8c2eab6dfefbfeb6889e35701fd39b1cbd013746 100644 (file)
@@ -211,6 +211,10 @@ responses = {
 # 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 @@ class HTTPMessage(mimetools.Message):
         elif self.seekable:
             tell = self.fp.tell
         while True:
+            if len(hlist) > _MAXHEADERS:
+                raise HTTPException("got more than %d headers" % _MAXHEADERS)
             if tell:
                 try:
                     startofline = tell()
@@ -1203,6 +1209,7 @@ class BadStatusLine(HTTPException):
         self.args = line,
         self.line = line
 
+
 # for backwards compatibility
 error = HTTPException
 
index 730e8852a1d0b883481cdf7b870f39f54d4e2dc6..b5f8a7954376ace24fc373e19ab027695901abf1 100644 (file)
@@ -152,6 +152,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 xrange(200)) + '\r\n'
+        text = ('HTTP/1.1 200 OK\r\n' + headers)
+        s = FakeSocket(text)
+        r = httplib.HTTPResponse(s)
+        self.assertRaises(httplib.HTTPException, 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:'
index 8a0db6e1ca78f774e7e65ebb360394121a5537ad..0f31f1e29940f7c0df21f1ce70e2afdbd11ce720 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,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 #16038: CVE-2013-1752: ftplib: Limit amount of data read by
   limiting the call to readline().  Original patch by Michał
   Jastrzębski and Giampaolo Rodola.