Skip to content
Merged
Show file tree
Hide file tree
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
Expand Up @@ -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
# -----
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions h11/tests/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")]) == [
Expand Down
Loading