Skip to content

Commit 40a14d8

Browse files
committed
Reject Content-Length longer than 4300 digits
1 parent 31e626c commit 40a14d8

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

Diff for: h11/_headers.py

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import re
2+
try:
3+
from sys import get_int_max_str_digits
4+
except ImportError:
5+
def get_int_max_str_digits():
6+
return 4300 # CPython default
27
from typing import AnyStr, cast, List, overload, Sequence, Tuple, TYPE_CHECKING, Union
38

49
from ._abnf import field_name, field_value
@@ -173,6 +178,8 @@ def normalize_and_validate(
173178
raise LocalProtocolError("conflicting Content-Length headers")
174179
value = lengths.pop()
175180
validate(_content_length_re, value, "bad Content-Length")
181+
if len(value) > get_int_max_str_digits():
182+
raise LocalProtocolError("bad Content-Length")
176183
if seen_content_length is None:
177184
seen_content_length = value
178185
new_headers.append((raw_name, name, value))

Diff for: 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" * 4301)])
7779

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

0 commit comments

Comments
 (0)