]> granicus.if.org Git - python/commitdiff
Issue #26586: Handle excessive header fields in http.server, by Xiang Zhang
authorMartin Panter <vadmium+py@gmail.com>
Sun, 3 Apr 2016 00:45:46 +0000 (00:45 +0000)
committerMartin Panter <vadmium+py@gmail.com>
Sun, 3 Apr 2016 00:45:46 +0000 (00:45 +0000)
Lib/http/server.py
Lib/test/test_httpservers.py
Misc/NEWS

index e1b71abf37420665f5c36a2df9c1f1e160616aa0..fac4d9db39ed37f5ff2027f1909c9ea0ed42fdc2 100644 (file)
@@ -337,6 +337,13 @@ class BaseHTTPRequestHandler(socketserver.StreamRequestHandler):
                 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':
index d4c8eabd42205b04d005d0d5fe6759a55044dd2b..b71fa5e851eb5ab84ac184db87f1c050c15b2a87 100644 (file)
@@ -858,6 +858,13 @@ class BaseHTTPRequestHandlerTestCase(unittest.TestCase):
         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
index e2e37a4064ad62beee6a2d258d4a6c848e899fff..9682b409d1415bdc030eb14976cdb7889dce2d0d 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -99,6 +99,10 @@ Core and Builtins
 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.