f63228
f63228
# HG changeset patch
f63228
# User Berker Peksag <berker.peksag@gmail.com>
f63228
# Date 1407212157 -10800
f63228
# Node ID 5e310c6a8520603bca8bc4b40eaf4f074db47c0d
f63228
# Parent  46c7a724b487295257423a69478392cb01ce74e6
f63228
Issue #16037: HTTPMessage.readheaders() raises an HTTPException when more
f63228
than 100 headers are read.
f63228
f63228
Patch by Jyrki Pulliainen and Daniel Eriksson.
f63228
f63228
diff --git a/Lib/httplib.py b/Lib/httplib.py
f63228
--- a/Lib/httplib.py
f63228
+++ b/Lib/httplib.py
f63228
@@ -215,6 +215,10 @@ MAXAMOUNT = 1048576
f63228
 # maximal line length when calling readline().
f63228
 _MAXLINE = 65536
f63228
 
f63228
+# maximum amount of headers accepted
f63228
+_MAXHEADERS = 100
f63228
+
f63228
+
f63228
 class HTTPMessage(mimetools.Message):
f63228
 
f63228
     def addheader(self, key, value):
f63228
@@ -271,6 +275,8 @@ class HTTPMessage(mimetools.Message):
f63228
         elif self.seekable:
f63228
             tell = self.fp.tell
f63228
         while True:
f63228
+            if len(hlist) > _MAXHEADERS:
f63228
+                raise HTTPException("got more than %d headers" % _MAXHEADERS)
f63228
             if tell:
f63228
                 try:
f63228
                     startofline = tell()
f63228
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
f63228
--- a/Lib/test/test_httplib.py
f63228
+++ b/Lib/test/test_httplib.py
f63228
@@ -262,6 +262,13 @@ class BasicTest(TestCase):
f63228
         if resp.read() != "":
f63228
             self.fail("Did not expect response from HEAD request")
f63228
 
f63228
+    def test_too_many_headers(self):
f63228
+        headers = '\r\n'.join('Header%d: foo' % i for i in xrange(200)) + '\r\n'
f63228
+        text = ('HTTP/1.1 200 OK\r\n' + headers)
f63228
+        s = FakeSocket(text)
f63228
+        r = httplib.HTTPResponse(s)
f63228
+        self.assertRaises(httplib.HTTPException, r.begin)
f63228
+
f63228
     def test_send_file(self):
f63228
         expected = 'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \
f63228
                    'Accept-Encoding: identity\r\nContent-Length:'