Skip to content

Fix parsing limits #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
10 changes: 10 additions & 0 deletions h11/_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ def next_event(self) -> Union[Event, Type[NEED_DATA], Type[PAUSED]]:
if self.their_state is ERROR:
raise RemoteProtocolError("Can't receive data when peer state is ERROR")
try:
receive_buffer_len_before = len(self._receive_buffer)
event = self._extract_next_receive_event()
if event not in [NEED_DATA, PAUSED]:
self._process_event(self.their_role, cast(Event, event))
Expand All @@ -492,6 +493,15 @@ def next_event(self) -> Union[Event, Type[NEED_DATA], Type[PAUSED]]:
# We're still trying to complete some event, but that's
# never going to happen because no more data is coming
raise RemoteProtocolError("peer unexpectedly closed connection")

if isinstance(event, (Request, InformationalResponse, Response)):
read_from_buffer = receive_buffer_len_before - len(self._receive_buffer)
if read_from_buffer > self._max_incomplete_event_size + 1:
# 431 is "Request header fields too large" which is pretty
# much the only situation where we can get here
raise RemoteProtocolError(
"Receive buffer too long", error_status_hint=431
)
return event
except BaseException as exc:
self._process_error(self.their_role)
Expand Down
16 changes: 16 additions & 0 deletions h11/tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,22 @@ def test_max_incomplete_event_size_countermeasure() -> None:
c.next_event()


@pytest.mark.parametrize("chunking", [False, True])
def test_max_incomplete_event_size_enforced_regardless_of_chunking(chunking) -> None:
buffer = b"GET / HTTP/1.0\r\nBig: " + b"a" * 4000 + b"\r\n\r\n"
c = Connection(SERVER, max_incomplete_event_size=(len(buffer) - 2))

if chunking:
chunks = [buffer[:-1], buffer[-1:]]
else:
chunks = [buffer]

with pytest.raises(RemoteProtocolError):
for chunk in chunks:
c.receive_data(chunk)
get_all_events(c)


def test_reuse_simple() -> None:
p = ConnectionPair()
p.send(
Expand Down