Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-44022: Fix http client infinite line reading (DoS) after a http 100 #25916

Merged
merged 7 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Lib/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,12 @@ def begin(self):
if status != CONTINUE:
break
# skip the header from the 100 response
header_total_size = 0
gpshead marked this conversation as resolved.
Show resolved Hide resolved
while True:
skip = self.fp.readline(_MAXLINE + 1)
if len(skip) > _MAXLINE:
line_length = len(skip)
header_total_size += line_length
if line_length > _MAXLINE or header_total_size > _MAXLINE:
raise LineTooLong("header line")
skip = skip.strip()
if not skip:
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_httplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,14 @@ def test_overflowing_header_line(self):
resp = client.HTTPResponse(FakeSocket(body))
self.assertRaises(client.LineTooLong, resp.begin)

def test_overflowing_total_header_size_after_100(self):
body = (
'HTTP/1.1 100 OK\r\n'
'r\n' * 32768
)
resp = client.HTTPResponse(FakeSocket(body))
self.assertRaises(client.LineTooLong, resp.begin)

def test_overflowing_chunked_line(self):
body = (
'HTTP/1.1 200 OK\r\n'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added total header size limit after receving http 100 to httplib client for avoiding malicious server packet causing client hangs.