From dddf9464b5266babe2b9bbe48f81c2fbbf2b87f5 Mon Sep 17 00:00:00 2001 From: fei <204683769+feiiiiii5@users.noreply.github.com> Date: Mon, 23 Nov 2026 14:12:03 +0800 Subject: [PATCH] fix: reject server-initiated streams in send_headers --- src/h2/connection.py | 7 ++++++ tests/test_basic_logic.py | 52 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/h2/connection.py b/src/h2/connection.py index e5059a21c..1fa98f11c 100644 --- a/src/h2/connection.py +++ b/src/h2/connection.py @@ -792,6 +792,13 @@ def send_headers(self, "Send headers on stream ID %d", stream_id, ) + # Only clients may open a stream by sending HEADERS. A server must + # respond on a stream the peer opened, or on one it reserved with + # PUSH_PROMISE: both already exist in ``self.streams``. + if not self.config.client_side and stream_id not in self.streams: + msg = f"Server cannot open stream {stream_id} by sending headers." + 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..d1dc20683 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="Server cannot open stream 2 by sending headers", + ): + 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.