Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit acc0319

Browse files
committed
Issue #26586: Handle excessive header fields in http.server, by Xiang Zhang
1 parent af83639 commit acc0319

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

Lib/http/server.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,13 @@ def parse_request(self):
337337
HTTPStatus.BAD_REQUEST,
338338
"Line too long")
339339
return False
340+
except http.client.HTTPException as err:
341+
self.send_error(
342+
HTTPStatus.REQUEST_HEADER_FIELDS_TOO_LARGE,
343+
"Too many headers",
344+
str(err)
345+
)
346+
return False
340347

341348
conntype = self.headers.get('Connection', "")
342349
if conntype.lower() == 'close':

Lib/test/test_httpservers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,13 @@ def test_header_length(self):
858858
self.assertFalse(self.handler.get_called)
859859
self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1')
860860

861+
def test_too_many_headers(self):
862+
result = self.send_typical_request(
863+
b'GET / HTTP/1.1\r\n' + b'X-Foo: bar\r\n' * 101 + b'\r\n')
864+
self.assertEqual(result[0], b'HTTP/1.1 431 Too many headers\r\n')
865+
self.assertFalse(self.handler.get_called)
866+
self.assertEqual(self.handler.requestline, 'GET / HTTP/1.1')
867+
861868
def test_close_connection(self):
862869
# handle_one_request() should be repeatedly called until
863870
# it sets close_connection

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ Core and Builtins
9999
Library
100100
-------
101101

102+
- Issue #26586: In http.server, respond with "413 Request header fields too
103+
large" if there are too many header fields to parse, rather than killing
104+
the connection and raising an unhandled exception. Patch by Xiang Zhang.
105+
102106
- Issue #22854: Change BufferedReader.writable() and
103107
BufferedWriter.readable() to always return False.
104108

0 commit comments

Comments
 (0)