Skip to content

Commit

Permalink
Reject Content-Length longer than 4300 digits
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien00859 committed Jan 11, 2025
1 parent 31e626c commit 40a14d8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions h11/_headers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import re
try:
from sys import get_int_max_str_digits
except ImportError:
def get_int_max_str_digits():
return 4300 # CPython default
from typing import AnyStr, cast, List, overload, Sequence, Tuple, TYPE_CHECKING, Union

from ._abnf import field_name, field_value
Expand Down Expand Up @@ -173,6 +178,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) > get_int_max_str_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" * 4301)])

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

0 comments on commit 40a14d8

Please sign in to comment.