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
1 change: 1 addition & 0 deletions src/h2/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,7 @@ def remotely_pushed(self, pushed_headers: Iterable[Header]) -> tuple[list[Frame]
StreamInputs.RECV_PUSH_PROMISE,
)
self._authority = authority_from_headers(pushed_headers)
self.request_method = extract_method_header(pushed_headers)

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.

We should validate that the pushed request method is "safe" as defined in
https://datatracker.ietf.org/doc/html/rfc9113#section-8.4
and
https://www.rfc-editor.org/info/rfc9110/#section-9.2.1

Promised requests MUST be safe (see Section 9.2.1 of [HTTP])

Of the request methods defined by this specification, the GET, HEAD, OPTIONS, and TRACE methods are defined to be safe.

return [], events

def receive_headers(self,
Expand Down
60 changes: 60 additions & 0 deletions tests/test_head_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,63 @@ def test_reject_non_zero_content_and_body(self, frame_factory, headers) -> None:
c.receive_data(headers.serialize())
with pytest.raises(h2.exceptions.InvalidBodyLengthError):
c.receive_data(data.serialize())


class TestPushedHeadRequest:
"""Regression test for pushed HEAD responses carrying content-length (#1330)."""

pushed_response_headers = [
(b":status", b"200"),
(b"content-length", b"5"),
]

@staticmethod
def _receive_pushed_response(frame_factory, method: bytes) -> list[h2.events.Event]:
c = h2.connection.H2Connection()
c.initiate_connection()
c.send_headers(
1,
[
(b":method", b"GET"),
(b":scheme", b"https"),
(b":authority", b"example.com"),
(b":path", b"/"),
],
end_stream=False,
)

pushed_request_headers = [
(b":method", method),
(b":scheme", b"https"),
(b":authority", b"example.com"),
(b":path", b"/"),
]
push_promise = frame_factory.build_push_promise_frame(
stream_id=1,
promised_stream_id=2,
headers=pushed_request_headers,
flags=["END_HEADERS"],
)
response = frame_factory.build_headers_frame(
TestPushedHeadRequest.pushed_response_headers,
stream_id=2,
)
data = frame_factory.build_data_frame(
data=b"",
flags=["END_STREAM"],
stream_id=2,
)
return c.receive_data(
push_promise.serialize() + response.serialize() + data.serialize(),
)

def test_pushed_head_response_with_content_length_has_no_body(self, frame_factory) -> None:
events = self._receive_pushed_response(frame_factory, b"HEAD")
data_events = [e for e in events if isinstance(e, h2.events.DataReceived)]
assert len(data_events) == 1
assert data_events[0].stream_id == 2
assert data_events[0].data == b""

def test_pushed_get_response_with_content_length_rejects_empty_body(self, frame_factory) -> None:
with pytest.raises(h2.exceptions.InvalidBodyLengthError):
self._receive_pushed_response(frame_factory, b"GET")