Skip to content

Commit 5398a0b

Browse files
miss-islingtonmetsw24-maxdanilablagorodniy37encukou
authored
[3.14] gh-150751: validate http.client Content-Length and chunk-size (GH-150752) (GH-159020)
* gh-150751: validate http.client Content-Length and chunk-size (GH-150752) RFC 9112 defines Content-Length as 1*DIGIT and chunk-size as 1*HEXDIG, but int() also accepts a sign, underscores, surrounding whitespace and an 0x prefix, so malformed framing values were parsed instead of rejected. (cherry picked from commit 2eb2af8) Co-authored-by: metsw24-max <metsw24@gmail.com> * gh-150751: Ignore optional whitespace around Content-Length in http.client (GH-159156) gh-150752 started validating the Content-Length value against the RFC 9112 grammar (1*DIGIT), but the compat32 header parser keeps trailing SP/HTAB in the field value, so "Content-Length: 5 " is now treated as a missing header. HTTPResponse then assumes that the connection will close and read() blocks until the server closes it, which never happens on a keep-alive connection. RFC 9112, section 5.1 says that optional whitespace around a field value is excluded by parsers. Strip SP/HTAB before the grammar check; values such as "+5", "5_0" and "5 0" are still rejected. --------- Co-authored-by: metsw24-max <metsw24@gmail.com> Co-authored-by: blago1 <74470460+danilablagorodniy37@users.noreply.github.com> Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent c9bf268 commit 5398a0b

4 files changed

Lines changed: 67 additions & 8 deletions

File tree

‎Lib/http/client.py‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@
166166
# to prevent http header injection.
167167
_contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]')
168168

169+
# RFC 9112: Content-Length = 1*DIGIT and chunk-size = 1*HEXDIG.
170+
# int() is more permissive, so we match against the grammar before calling it.
171+
_is_legal_content_length = re.compile(r'[0-9]+').fullmatch
172+
_is_legal_chunk_size = re.compile(rb'[0-9a-fA-F]+').fullmatch
173+
169174
# We always set the Content-Length header for these methods because some
170175
# servers will otherwise respond with a 411
171176
_METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'}
@@ -384,14 +389,12 @@ def begin(self):
384389
# NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
385390
self.length = None
386391
length = self.headers.get("content-length")
387-
if length and not self.chunked:
388-
try:
389-
self.length = int(length)
390-
except ValueError:
391-
self.length = None
392-
else:
393-
if self.length < 0: # ignore nonsensical negative lengths
394-
self.length = None
392+
if length is not None:
393+
# RFC 9112, section 5.1: optional whitespace (SP / HTAB) around
394+
# the field value is not part of the value.
395+
length = length.strip(' \t')
396+
if length and not self.chunked and _is_legal_content_length(length):
397+
self.length = int(length)
395398
else:
396399
self.length = None
397400

@@ -558,7 +561,10 @@ def _read_next_chunk_size(self):
558561
i = line.find(b";")
559562
if i >= 0:
560563
line = line[:i] # strip chunk-extensions
564+
line = line.rstrip()
561565
try:
566+
if not _is_legal_chunk_size(line):
567+
raise ValueError("invalid chunk size")
562568
return int(line, 16)
563569
except ValueError:
564570
# close the connection as protocol synchronisation is

‎Lib/test/test_httplib.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,49 @@ def test_negative_content_length(self):
12781278
self.assertEqual(resp.read(), b'Hello\r\n')
12791279
self.assertTrue(resp.isclosed())
12801280

1281+
def test_malformed_content_length(self):
1282+
# RFC 9112: Content-Length = 1*DIGIT. Values that int() accepts but
1283+
# the grammar forbids must not be used to frame the body.
1284+
for value in ('+5', '5_0', '5 0'):
1285+
with self.subTest(value=value):
1286+
sock = FakeSocket(
1287+
'HTTP/1.1 200 OK\r\nContent-Length: %s\r\n\r\nHello\r\n' % value)
1288+
resp = client.HTTPResponse(sock, method="GET")
1289+
resp.begin()
1290+
self.assertIsNone(resp.length)
1291+
self.assertEqual(resp.read(), b'Hello\r\n')
1292+
resp.close()
1293+
1294+
def test_content_length_with_whitespace(self):
1295+
# RFC 9112, section 5.1: optional whitespace (SP / HTAB) around
1296+
# the field value is not part of the value, so it must not
1297+
# prevent the Content-Length from being used to frame the body.
1298+
for value in ('7 ', '7\t', '7 \t ', ' 7 '):
1299+
with self.subTest(value=value):
1300+
sock = FakeSocket(
1301+
'HTTP/1.1 200 OK\r\nContent-Length: %s\r\n\r\n'
1302+
'Hello\r\nextra' % value)
1303+
resp = client.HTTPResponse(sock, method="GET")
1304+
resp.begin()
1305+
self.assertEqual(resp.length, 7)
1306+
self.assertEqual(resp.read(), b'Hello\r\n')
1307+
self.assertTrue(resp.isclosed())
1308+
resp.close()
1309+
1310+
def test_malformed_chunk_size(self):
1311+
# RFC 9112: chunk-size = 1*HEXDIG. Reject sizes that int(_, 16) accepts
1312+
# but the grammar forbids (a sign, an "0x" prefix, underscores or
1313+
# leading whitespace).
1314+
start = 'HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n'
1315+
for size in ('-5', '+5', '0x5', '1_f', ' 5'):
1316+
with self.subTest(size=size):
1317+
sock = FakeSocket(start + '%s\r\nHELLO\r\n0\r\n\r\n' % size)
1318+
resp = client.HTTPResponse(sock, method="GET")
1319+
resp.begin()
1320+
self.assertRaises(client.IncompleteRead, resp.read)
1321+
self.assertTrue(resp.isclosed())
1322+
resp.close()
1323+
12811324
def test_incomplete_read(self):
12821325
sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n')
12831326
resp = client.HTTPResponse(sock, method="GET")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:mod:`http.client` now validates the ``Content-Length`` header and the
2+
chunked ``chunk-size`` against the RFC 9112 grammar (``1*DIGIT`` and
3+
``1*HEXDIG``) before parsing them, rejecting values such as ``+5``, ``5_0``
4+
or a ``0x``-prefixed or negative chunk size that :func:`int` would otherwise
5+
accept. This avoids framing a response differently from a strict peer.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:mod:`http.client` now ignores optional whitespace around the
2+
``Content-Length`` header value when validating it, as required by
3+
:rfc:`9112`. Previously a value with trailing whitespace (for example
4+
``Content-Length: 5`` followed by a space) was treated as missing and the
5+
response body was read until the connection was closed.

0 commit comments

Comments
 (0)