Skip to content

Commit b4c1ca2

Browse files
miss-islingtonkenballusarhadthedevgpshead
authored
[3.11] gh-103204: http.server - Enforce that HTTP version numbers must consist only of digits (GH-103205) (#104438)
gh-103204: `http.server` - Enforce that HTTP version numbers must consist only of digits (GH-103205) Reject HTTP requests with invalid http/x.y version numbers: x or y being non-digits or too-long. --------- (cherry picked from commit cf720ac) Co-authored-by: Ben Kallus <[email protected]> Co-authored-by: Oleg Iarygin <[email protected]> Co-authored-by: Gregory P. Smith <[email protected]>
1 parent 7055088 commit b4c1ca2

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

Lib/http/server.py

+4
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ def parse_request(self):
300300
# - Leading zeros MUST be ignored by recipients.
301301
if len(version_number) != 2:
302302
raise ValueError
303+
if any(not component.isdigit() for component in version_number):
304+
raise ValueError("non digit in http version")
305+
if any(len(component) > 10 for component in version_number):
306+
raise ValueError("unreasonable length http version")
303307
version_number = int(version_number[0]), int(version_number[1])
304308
except (ValueError, IndexError):
305309
self.send_error(

Lib/test/test_httpservers.py

+21
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,27 @@ def test_version_digits(self):
164164
res = self.con.getresponse()
165165
self.assertEqual(res.status, HTTPStatus.BAD_REQUEST)
166166

167+
def test_version_signs_and_underscores(self):
168+
self.con._http_vsn_str = 'HTTP/-9_9_9.+9_9_9'
169+
self.con.putrequest('GET', '/')
170+
self.con.endheaders()
171+
res = self.con.getresponse()
172+
self.assertEqual(res.status, HTTPStatus.BAD_REQUEST)
173+
174+
def test_major_version_number_too_long(self):
175+
self.con._http_vsn_str = 'HTTP/909876543210.0'
176+
self.con.putrequest('GET', '/')
177+
self.con.endheaders()
178+
res = self.con.getresponse()
179+
self.assertEqual(res.status, HTTPStatus.BAD_REQUEST)
180+
181+
def test_minor_version_number_too_long(self):
182+
self.con._http_vsn_str = 'HTTP/1.909876543210'
183+
self.con.putrequest('GET', '/')
184+
self.con.endheaders()
185+
res = self.con.getresponse()
186+
self.assertEqual(res.status, HTTPStatus.BAD_REQUEST)
187+
167188
def test_version_none_get(self):
168189
self.con._http_vsn_str = ''
169190
self.con.putrequest('GET', '/')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixes :mod:`http.server` accepting HTTP requests with HTTP version numbers
2+
preceded by '+', or '-', or with digit-separating '_' characters. The length
3+
of the version numbers is also constrained.

0 commit comments

Comments
 (0)