[Python-checkins] cpython (2.7): Issue #16037: HTTPMessage.readheaders() raises an HTTPException when more

berker.peksag python-checkins at python.org
Tue Aug 5 06:15:03 CEST 2014


http://hg.python.org/cpython/rev/5e310c6a8520
changeset:   92002:5e310c6a8520
branch:      2.7
parent:      91998:46c7a724b487
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Tue Aug 05 07:15:57 2014 +0300
summary:
  Issue #16037: HTTPMessage.readheaders() raises an HTTPException when more
than 100 headers are read.

Patch by Jyrki Pulliainen and Daniel Eriksson.

files:
  Lib/httplib.py           |  6 ++++++
  Lib/test/test_httplib.py |  7 +++++++
  Misc/NEWS                |  3 +++
  3 files changed, 16 insertions(+), 0 deletions(-)


diff --git a/Lib/httplib.py b/Lib/httplib.py
--- a/Lib/httplib.py
+++ b/Lib/httplib.py
@@ -215,6 +215,10 @@
 # maximal line length when calling readline().
 _MAXLINE = 65536
 
+# maximum amount of headers accepted
+_MAXHEADERS = 100
+
+
 class HTTPMessage(mimetools.Message):
 
     def addheader(self, key, value):
@@ -271,6 +275,8 @@
         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()
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -262,6 +262,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.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:'
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -19,6 +19,9 @@
 Library
 -------
 
+- Issue #16037: HTTPMessage.readheaders() raises an HTTPException when more
+  than 100 headers are read. Patch by Jyrki Pulliainen and Daniel Eriksson.
+
 - Issue #21580: Now Tkinter correctly handles binary "data" and "maskdata"
   configure options of tkinter.PhotoImage.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list