[Python-checkins] cpython (3.5): Issue #26586: Handle excessive header fields in http.server, by Xiang Zhang

martin.panter python-checkins at python.org
Sat Apr 2 22:02:19 EDT 2016


https://hg.python.org/cpython/rev/f5247195238f
changeset:   100836:f5247195238f
branch:      3.5
parent:      100834:f41d3321007f
user:        Martin Panter <vadmium+py at gmail.com>
date:        Sun Apr 03 00:45:46 2016 +0000
summary:
  Issue #26586: Handle excessive header fields in http.server, by Xiang Zhang

files:
  Lib/http/server.py           |  7 +++++++
  Lib/test/test_httpservers.py |  7 +++++++
  Misc/NEWS                    |  4 ++++
  3 files changed, 18 insertions(+), 0 deletions(-)


diff --git a/Lib/http/server.py b/Lib/http/server.py
--- a/Lib/http/server.py
+++ b/Lib/http/server.py
@@ -337,6 +337,13 @@
                 HTTPStatus.BAD_REQUEST,
                 "Line too long")
             return False
+        except http.client.HTTPException as err:
+            self.send_error(
+                HTTPStatus.REQUEST_HEADER_FIELDS_TOO_LARGE,
+                "Too many headers",
+                str(err)
+            )
+            return False
 
         conntype = self.headers.get('Connection', "")
         if conntype.lower() == 'close':
diff --git a/Lib/test/test_httpservers.py b/Lib/test/test_httpservers.py
--- a/Lib/test/test_httpservers.py
+++ b/Lib/test/test_httpservers.py
@@ -858,6 +858,13 @@
         self.assertFalse(self.handler.get_called)
         self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1')
 
+    def test_too_many_headers(self):
+        result = self.send_typical_request(
+            b'GET / HTTP/1.1\r\n' + b'X-Foo: bar\r\n' * 101 + b'\r\n')
+        self.assertEqual(result[0], b'HTTP/1.1 431 Too many headers\r\n')
+        self.assertFalse(self.handler.get_called)
+        self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1')
+
     def test_close_connection(self):
         # handle_one_request() should be repeatedly called until
         # it sets close_connection
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -99,6 +99,10 @@
 Library
 -------
 
+- Issue #26586: In http.server, respond with "413 Request header fields too
+  large" if there are too many header fields to parse, rather than killing
+  the connection and raising an unhandled exception.  Patch by Xiang Zhang.
+
 - Issue #22854: Change BufferedReader.writable() and
   BufferedWriter.readable() to always return False.
 

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


More information about the Python-checkins mailing list