diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 69f0c7401..41eb61384 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,7 +14,7 @@ dev **Bugfixes** -- +- ``H2Connection.send_headers`` now raises ``ProtocolError`` when a server tries to open a new stream by sending a HEADERS frame. Previously the stream was created regardless of connection side. 4.4.1 (2026-08-03) ------------------ diff --git a/src/h2/connection.py b/src/h2/connection.py index e5059a21c..97ab021d8 100644 --- a/src/h2/connection.py +++ b/src/h2/connection.py @@ -792,6 +792,14 @@ def send_headers(self, "Send headers on stream ID %d", stream_id, ) + # RFC 7540 Section 5.1.1 assigns even stream IDs to server-initiated + # streams and Section 8.2 reserves them with PUSH_PROMISE. A server + # therefore responds on a peer-opened or PUSH_PROMISE-reserved stream, + # both of which are already in ``self.streams``. + if not self.config.client_side and stream_id not in self.streams: + msg = "Servers cannot open new streams by sending a HEADERS frame." + raise ProtocolError(msg) + # Check we can open the stream. if stream_id not in self.streams: max_open_streams = self.remote_settings.max_concurrent_streams diff --git a/tests/test_basic_logic.py b/tests/test_basic_logic.py index d1bc0f1eb..9970e364d 100644 --- a/tests/test_basic_logic.py +++ b/tests/test_basic_logic.py @@ -99,6 +99,19 @@ def test_sending_headers(self) -> None: b"A\x88/\x91\xd3]\x05\\\x87\xa7\x84\x87\x82" ) + def test_client_can_open_stream_by_sending_headers(self) -> None: + """ + A client may open a new stream by sending HEADERS. + """ + c = h2.connection.H2Connection() + c.initiate_connection() + c.clear_outbound_data_buffer() + + c.send_headers(1, self.example_request_headers) + + assert 1 in c.streams + assert c.data_to_send() + def test_sending_data(self) -> None: """ Single data frames are encoded correctly. @@ -1497,6 +1510,45 @@ def test_can_push_stream(self, frame_factory) -> None: assert c.data_to_send() == expected_frame.serialize() + def test_server_cannot_open_stream_by_sending_headers(self) -> None: + """ + A server must not open a new stream by sending HEADERS. + """ + c = h2.connection.H2Connection(config=self.server_config) + c.initiate_connection() + c.clear_outbound_data_buffer() + + # Request-style headers let the base code accept this call: the fresh + # stream is (incorrectly) treated as a request stream opened by us. + with pytest.raises( + h2.exceptions.ProtocolError, + match="Servers cannot open new streams by sending a HEADERS frame", + ): + c.send_headers(2, self.example_request_headers) + + assert not c.data_to_send() + + def test_server_can_send_headers_on_pushed_stream(self, frame_factory) -> None: + """ + A server may send response HEADERS on a stream it reserved by pushing. + """ + c = h2.connection.H2Connection(config=self.server_config) + c.receive_data(frame_factory.preamble()) + f = frame_factory.build_headers_frame(self.example_request_headers) + c.receive_data(f.serialize()) + + c.push_stream( + stream_id=1, + promised_stream_id=2, + request_headers=self.example_request_headers, + ) + c.clear_outbound_data_buffer() + + c.send_headers(2, self.example_response_headers) + + assert 2 in c.streams + assert c.data_to_send() + def test_cannot_push_streams_when_disabled(self, frame_factory) -> None: """ When the remote peer has disabled stream pushing, we should fail.