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
7 changes: 7 additions & 0 deletions src/h2/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions tests/test_basic_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down