Skip to content

Commit 5b9e3d0

Browse files
miss-islingtonmetsw24-maxdanilablagorodniy37encukou
authored
[3.15] gh-150751: validate http.client Content-Length and chunk-size (GH-150752) (GH-159021)
* 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 c208053 commit 5b9e3d0

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'}
@@ -393,14 +398,12 @@ def begin(self, *, _max_headers=None):
393398
# NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked"
394399
self.length = None
395400
length = self.headers.get("content-length")
396-
if length and not self.chunked:
397-
try:
398-
self.length = int(length)
399-
except ValueError:
400-
self.length = None
401-
else:
402-
if self.length < 0: # ignore nonsensical negative lengths
403-
self.length = None
401+
if length is not None:
402+
# RFC 9112, section 5.1: optional whitespace (SP / HTAB) around
403+
# the field value is not part of the value.
404+
length = length.strip(' \t')
405+
if length and not self.chunked and _is_legal_content_length(length):
406+
self.length = int(length)
404407
else:
405408
self.length = None
406409

@@ -567,7 +570,10 @@ def _read_next_chunk_size(self):
567570
i = line.find(b";")
568571
if i >= 0:
569572
line = line[:i] # strip chunk-extensions
573+
line = line.rstrip()
570574
try:
575+
if not _is_legal_chunk_size(line):
576+
raise ValueError("invalid chunk size")
571577
return int(line, 16)
572578
except ValueError:
573579
# 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
@@ -1353,6 +1353,49 @@ def test_negative_content_length(self):
13531353
self.assertEqual(resp.read(), b'Hello\r\n')
13541354
self.assertTrue(resp.isclosed())
13551355

1356+
def test_malformed_content_length(self):
1357+
# RFC 9112: Content-Length = 1*DIGIT. Values that int() accepts but
1358+
# the grammar forbids must not be used to frame the body.
1359+
for value in ('+5', '5_0', '5 0'):
1360+
with self.subTest(value=value):
1361+
sock = FakeSocket(
1362+
'HTTP/1.1 200 OK\r\nContent-Length: %s\r\n\r\nHello\r\n' % value)
1363+
resp = client.HTTPResponse(sock, method="GET")
1364+
resp.begin()
1365+
self.assertIsNone(resp.length)
1366+
self.assertEqual(resp.read(), b'Hello\r\n')
1367+
resp.close()
1368+
1369+
def test_content_length_with_whitespace(self):
1370+
# RFC 9112, section 5.1: optional whitespace (SP / HTAB) around
1371+
# the field value is not part of the value, so it must not
1372+
# prevent the Content-Length from being used to frame the body.
1373+
for value in ('7 ', '7\t', '7 \t ', ' 7 '):
1374+
with self.subTest(value=value):
1375+
sock = FakeSocket(
1376+
'HTTP/1.1 200 OK\r\nContent-Length: %s\r\n\r\n'
1377+
'Hello\r\nextra' % value)
1378+
resp = client.HTTPResponse(sock, method="GET")
1379+
resp.begin()
1380+
self.assertEqual(resp.length, 7)
1381+
self.assertEqual(resp.read(), b'Hello\r\n')
1382+
self.assertTrue(resp.isclosed())
1383+
resp.close()
1384+
1385+
def test_malformed_chunk_size(self):
1386+
# RFC 9112: chunk-size = 1*HEXDIG. Reject sizes that int(_, 16) accepts
1387+
# but the grammar forbids (a sign, an "0x" prefix, underscores or
1388+
# leading whitespace).
1389+
start = 'HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n'
1390+
for size in ('-5', '+5', '0x5', '1_f', ' 5'):
1391+
with self.subTest(size=size):
1392+
sock = FakeSocket(start + '%s\r\nHELLO\r\n0\r\n\r\n' % size)
1393+
resp = client.HTTPResponse(sock, method="GET")
1394+
resp.begin()
1395+
self.assertRaises(client.IncompleteRead, resp.read)
1396+
self.assertTrue(resp.isclosed())
1397+
resp.close()
1398+
13561399
def test_incomplete_read(self):
13571400
sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n')
13581401
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)