Skip to content

Commit 41cc78a

Browse files
authored
gh-150743: Limit trailer lines and interim responses read by http.client (GH-150741)
http.client read chunked-response trailer lines and skipped interim (1xx) responses in unbounded loops, so a server streaming either forever would hang the client even with a socket timeout set (data keeps arriving, so the timeout never fires). Trailer lines are now limited to max_response_headers (100 by default) and interim responses to 100; HTTPException is raised past either limit. Follow-up to gh-88188 for CVE-2021-3737, which bounded header lines within an interim response but not these two sibling loops. --- This issue was reported to us via [GHSA-w4q2-g22w-6fr4](https://github.com/python/cpython/security/advisories/GHSA-w4q2-g22w-6fr4) and was determined not to be high enough severity to handle privately.
1 parent 54524ab commit 41cc78a

4 files changed

Lines changed: 123 additions & 1 deletion

File tree

Doc/library/http.client.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ HTTPConnection Objects
433433

434434
The maximum number of allowed response headers to help prevent denial-of-service
435435
attacks. By default, the maximum number of allowed headers is set to 100.
436+
The same limit applies to the trailer section of a chunked response.
436437

437438
.. versionadded:: 3.15
438439

Lib/http/client.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@
111111
_MAXLINE = 65536
112112
_MAXHEADERS = 100
113113

114+
# maximal number of interim (1xx) responses tolerated before the final
115+
# response. Real servers send at most a few; without a bound, a server
116+
# streaming "100 Continue" responses would hang getresponse() forever.
117+
# A socket timeout cannot detect that as data keeps arriving within every
118+
# timeout window.
119+
_MAXINTERIMRESPONSES = 100
120+
114121
# Data larger than this will be read in chunks, to prevent extreme
115122
# overallocation.
116123
_MIN_READ_BUF_SIZE = 1 << 20
@@ -293,6 +300,7 @@ def __init__(self, sock, debuglevel=0, method=None, url=None):
293300
self.chunk_left = _UNKNOWN # bytes left to read in current chunk
294301
self.length = _UNKNOWN # number of bytes left in response
295302
self.will_close = _UNKNOWN # conn will close at end of response
303+
self._max_headers = None # configured header count limit
296304

297305
def _read_status(self):
298306
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
@@ -332,8 +340,13 @@ def begin(self, *, _max_headers=None):
332340
# we've already started reading the response
333341
return
334342

343+
# Trailers of a chunked response are read by read() long after
344+
# begin() returns, so remember the configured header count limit
345+
# for _read_and_discard_trailer() to enforce.
346+
self._max_headers = _max_headers
347+
335348
# read until we get a non-100 response
336-
while True:
349+
for _ in range(_MAXINTERIMRESPONSES):
337350
version, status, reason = self._read_status()
338351
if status != CONTINUE:
339352
break
@@ -342,6 +355,9 @@ def begin(self, *, _max_headers=None):
342355
if self.debuglevel > 0:
343356
print("headers:", skipped_headers)
344357
del skipped_headers
358+
else:
359+
raise HTTPException(
360+
f"got more than {_MAXINTERIMRESPONSES} interim responses")
345361

346362
self.code = self.status = status
347363
self.reason = reason.strip()
@@ -561,6 +577,10 @@ def _read_next_chunk_size(self):
561577
def _read_and_discard_trailer(self):
562578
# read and discard trailer up to the CRLF terminator
563579
### note: we shouldn't have any trailers!
580+
max_trailers = self._max_headers
581+
if max_trailers is None:
582+
max_trailers = _MAXHEADERS
583+
trailers_read = 0
564584
while True:
565585
line = self.fp.readline(_MAXLINE + 1)
566586
if len(line) > _MAXLINE:
@@ -571,6 +591,13 @@ def _read_and_discard_trailer(self):
571591
break
572592
if line in (b'\r\n', b'\n', b''):
573593
break
594+
# Bound the trailer count just as response headers are bounded.
595+
# A server streaming trailer lines forever would otherwise hang
596+
# the client; a socket timeout cannot detect that as data keeps
597+
# arriving within every timeout window.
598+
trailers_read += 1
599+
if trailers_read > max_trailers:
600+
raise HTTPException(f"got more than {max_trailers} trailers")
574601

575602
def _get_chunk_left(self):
576603
# return self.chunk_left, reading a new chunk if necessary.

Lib/test/test_httplib.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,35 @@ def test_max_connection_headers(self):
478478
response = conn.getresponse()
479479
response.read()
480480

481+
def test_max_connection_trailers(self):
482+
# max_response_headers also limits trailer lines of a chunked
483+
# response, which are read and discarded by read().
484+
max_trailers = client._MAXHEADERS + 20
485+
trailer_lines = "".join(
486+
f"X-Trailer{i}: {i}\r\n" for i in range(max_trailers - 1)
487+
)
488+
body = chunked_start + last_chunk + trailer_lines + chunked_end
489+
490+
with self.subTest(max_response_headers=None):
491+
conn = client.HTTPConnection("example.com")
492+
conn.sock = FakeSocket(body)
493+
conn.request("GET", "/")
494+
response = conn.getresponse()
495+
with self.assertRaisesRegex(
496+
client.HTTPException,
497+
f"got more than {client._MAXHEADERS} trailers",
498+
):
499+
response.read()
500+
501+
with self.subTest(max_response_headers=max_trailers):
502+
conn = client.HTTPConnection(
503+
"example.com", max_response_headers=max_trailers
504+
)
505+
conn.sock = FakeSocket(body)
506+
conn.request("GET", "/")
507+
response = conn.getresponse()
508+
self.assertEqual(response.read(), chunked_expected)
509+
481510
class HttpMethodTests(TestCase):
482511
def test_invalid_method_names(self):
483512
methods = (
@@ -1378,6 +1407,35 @@ def test_overflowing_header_limit_after_100(self):
13781407
self.assertIn('got more than ', str(cm.exception))
13791408
self.assertIn('headers', str(cm.exception))
13801409

1410+
def test_too_many_interim_responses(self):
1411+
# A server streaming "100 Continue" responses forever must not
1412+
# hang getresponse().
1413+
body = (
1414+
'HTTP/1.1 100 Continue\r\n\r\n'
1415+
* (client._MAXINTERIMRESPONSES + 1)
1416+
)
1417+
resp = client.HTTPResponse(FakeSocket(body))
1418+
with self.assertRaises(client.HTTPException) as cm:
1419+
resp.begin()
1420+
self.assertIn('got more than ', str(cm.exception))
1421+
self.assertIn('interim responses', str(cm.exception))
1422+
1423+
def test_multiple_interim_responses(self):
1424+
# A reasonable number of interim responses before the final
1425+
# response is skipped as before.
1426+
body = (
1427+
'HTTP/1.1 100 Continue\r\n\r\n' * 3 +
1428+
'HTTP/1.1 200 OK\r\n'
1429+
'Content-Length: 5\r\n'
1430+
'\r\n'
1431+
'hello'
1432+
)
1433+
resp = client.HTTPResponse(FakeSocket(body), method="GET")
1434+
resp.begin()
1435+
self.assertEqual(resp.status, 200)
1436+
self.assertEqual(resp.read(), b'hello')
1437+
resp.close()
1438+
13811439
def test_overflowing_chunked_line(self):
13821440
body = (
13831441
'HTTP/1.1 200 OK\r\n'
@@ -1449,6 +1507,35 @@ def test_chunked_trailers(self):
14491507
self.assertEqual(sock.file.read(), b"") #we read to the end
14501508
resp.close()
14511509

1510+
def test_chunked_too_many_trailers(self):
1511+
"""A response streaming endless trailer lines must raise, not hang"""
1512+
too_many_trailers = "".join(
1513+
f"X-Trailer{i}: {i}\r\n" for i in range(client._MAXHEADERS + 1)
1514+
)
1515+
# An unbounded read() reaches the trailers via the final 0 chunk.
1516+
sock = FakeSocket(
1517+
chunked_start + last_chunk + too_many_trailers + chunked_end)
1518+
resp = client.HTTPResponse(sock, method="GET")
1519+
resp.begin()
1520+
with self.assertRaisesRegex(
1521+
client.HTTPException,
1522+
f"got more than {client._MAXHEADERS} trailers",
1523+
):
1524+
resp.read()
1525+
resp.close()
1526+
1527+
# A bounded read(amt) larger than the body hits the same limit.
1528+
sock = FakeSocket(
1529+
chunked_start + last_chunk + too_many_trailers + chunked_end)
1530+
resp = client.HTTPResponse(sock, method="GET")
1531+
resp.begin()
1532+
with self.assertRaisesRegex(
1533+
client.HTTPException,
1534+
f"got more than {client._MAXHEADERS} trailers",
1535+
):
1536+
resp.read(len(chunked_expected) + 1)
1537+
resp.close()
1538+
14521539
def test_chunked_sync(self):
14531540
"""Check that we don't read past the end of the chunked-encoding stream"""
14541541
expected = chunked_expected
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:mod:`http.client` now limits the number of chunked-response trailer lines
2+
it will read to :attr:`~http.client.HTTPConnection.max_response_headers`
3+
(100 by default), and the number of interim (1xx) responses it will skip
4+
to 100. A malicious or broken server could previously stream trailer
5+
lines or ``100 Continue`` responses forever, hanging the client even when
6+
a socket timeout was in use.
7+
Reported by ``@YLChen-007`` via GHSA-w4q2-g22w-6fr4.

0 commit comments

Comments
 (0)