fix: reject conflicting content-length headers by Harshal96 · Pull Request #1317 · python-hyper/h2 · GitHub
Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.rst
12 changes: 10 additions & 2 deletions src/h2/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -1364,15 +1364,23 @@ def _initialize_content_length(self, headers: Iterable[Header]) -> None:
self._expected_content_length = 0
return

content_length = None

for n, v in headers:
if n == b"content-length":
try:
self._expected_content_length = int(v, 10)
parsed_content_length = int(v, 10)
except ValueError as err:
msg = f"Invalid content-length header: {v!r}"
raise ProtocolError(msg) from err

return
if content_length is None:
content_length = parsed_content_length
elif parsed_content_length != content_length:
msg = f"Conflicting content-length headers: {content_length} and {parsed_content_length}"
raise ProtocolError(msg)

self._expected_content_length = content_length

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be simplified here: if content_length is None, then just assign it anyway, as _expected_content_length is going to be also None.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


def _track_content_length(self, length: int, end_stream: bool) -> None:
"""
Expand Down
82 changes: 80 additions & 2 deletions tests/test_invalid_content_lengths.py