Skip to content

Commit dafc1e7

Browse files
committed
Reject Content-Length longer 1 billion TB
1 parent 31e626c commit dafc1e7

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

h11/_headers.py

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
except ImportError:
1313
from typing_extensions import Literal # type: ignore
1414

15+
CONTENT_LENGTH_MAX_DIGITS = 20 # allow up to 100_000_000TB
16+
1517

1618
# Facts
1719
# -----
@@ -173,6 +175,8 @@ def normalize_and_validate(
173175
raise LocalProtocolError("conflicting Content-Length headers")
174176
value = lengths.pop()
175177
validate(_content_length_re, value, "bad Content-Length")
178+
if len(value) > CONTENT_LENGTH_MAX_DIGITS:
179+
raise LocalProtocolError("bad Content-Length")
176180
if seen_content_length is None:
177181
seen_content_length = value
178182
new_headers.append((raw_name, name, value))

h11/tests/test_headers.py

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def test_normalize_and_validate() -> None:
7474
)
7575
with pytest.raises(LocalProtocolError):
7676
normalize_and_validate([("Content-Length", "1 , 1,2")])
77+
with pytest.raises(LocalProtocolError):
78+
normalize_and_validate([("Content-Length", "1" * 21)]) # 1 billion TB
7779

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

0 commit comments

Comments
 (0)