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

Reject Content-Length longer than 1 billion TB #181

Merged
Merged
Changes from all 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
4 changes: 4 additions & 0 deletions h11/_headers.py
Original file line number Diff line number Diff line change
@@ -12,6 +12,8 @@
except ImportError:
from typing_extensions import Literal # type: ignore

CONTENT_LENGTH_MAX_DIGITS = 20 # allow up to 1 billion TB - 1


# Facts
# -----
@@ -173,6 +175,8 @@ def normalize_and_validate(
raise LocalProtocolError("conflicting Content-Length headers")
value = lengths.pop()
validate(_content_length_re, value, "bad Content-Length")
if len(value) > CONTENT_LENGTH_MAX_DIGITS:
raise LocalProtocolError("bad Content-Length")
if seen_content_length is None:
seen_content_length = value
new_headers.append((raw_name, name, value))
2 changes: 2 additions & 0 deletions h11/tests/test_headers.py
Original file line number Diff line number Diff line change
@@ -74,6 +74,8 @@ def test_normalize_and_validate() -> None:
)
with pytest.raises(LocalProtocolError):
normalize_and_validate([("Content-Length", "1 , 1,2")])
with pytest.raises(LocalProtocolError):
normalize_and_validate([("Content-Length", "1" * 21)]) # 1 billion TB

# transfer-encoding
assert normalize_and_validate([("Transfer-Encoding", "chunked")]) == [