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 1/2] 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. From 93e85a8ca89db49a666a8515d038c16808525f64 Mon Sep 17 00:00:00 2001 From: feiiiiii5 <204683769+feiiiiii5@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:00:04 +0800 Subject: [PATCH 2/2] fix: clarify RFC basis and add changelog for server HEADERS guard Reword the comment to cite RFC 7540 sections 5.1.1 and 8.2 instead of overstating the RFC. Drop the stream id from the error message so it applies to all stream IDs, and add a CHANGELOG entry. --- CHANGELOG.rst | 2 +- src/h2/connection.py | 9 +++++---- tests/test_basic_logic.py | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) 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 1fa98f11c..97ab021d8 100644 --- a/src/h2/connection.py +++ b/src/h2/connection.py @@ -792,11 +792,12 @@ 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``. + # 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 = f"Server cannot open stream {stream_id} by sending headers." + msg = "Servers cannot open new streams by sending a HEADERS frame." raise ProtocolError(msg) # Check we can open the stream. diff --git a/tests/test_basic_logic.py b/tests/test_basic_logic.py index d1dc20683..9970e364d 100644 --- a/tests/test_basic_logic.py +++ b/tests/test_basic_logic.py @@ -1522,7 +1522,7 @@ def test_server_cannot_open_stream_by_sending_headers(self) -> None: # 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", + match="Servers cannot open new streams by sending a HEADERS frame", ): c.send_headers(2, self.example_request_headers)