From ecdaa008c518c31233debf2d0bedd47099e87119 Mon Sep 17 00:00:00 2001 From: fei <204683769+feiiiiii5@users.noreply.github.com> Date: Fri, 27 Nov 2026 09:03:43 +0800 Subject: [PATCH] fix: validate proposed flow-control window before mutating --- src/h2/windows.py | 5 ++- tests/test_flow_control_window.py | 74 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/h2/windows.py b/src/h2/windows.py index 0efdd9fe6..c344a096b 100644 --- a/src/h2/windows.py +++ b/src/h2/windows.py @@ -64,12 +64,13 @@ def window_opened(self, size: int) -> None: :returns: Nothing :rtype: ``None`` """ - self.current_window_size += size + proposed = self.current_window_size + size - if self.current_window_size > LARGEST_FLOW_CONTROL_WINDOW: + if proposed > LARGEST_FLOW_CONTROL_WINDOW: msg = f"Flow control window mustn't exceed {LARGEST_FLOW_CONTROL_WINDOW}" raise FlowControlError(msg) + self.current_window_size = proposed self.max_window_size = max(self.current_window_size, self.max_window_size) def process_bytes(self, size: int) -> int | None: diff --git a/tests/test_flow_control_window.py b/tests/test_flow_control_window.py index aea53fab1..76fb7a555 100644 --- a/tests/test_flow_control_window.py +++ b/tests/test_flow_control_window.py @@ -650,6 +650,80 @@ def test_reject_local_overlarge_increase_stream_window(self) -> None: with pytest.raises(h2.exceptions.FlowControlError): c.increment_flow_control_window(increment=increment, stream_id=1) + @pytest.mark.parametrize("stream_id", [None, 1]) + def test_rejected_local_increment_leaves_window_unchanged( + self, frame_factory, stream_id, + ) -> None: + """ + A local increment that is valid on its own but would overflow the + window must be rejected without corrupting the window, so that later + increments still succeed. + """ + c = h2.connection.H2Connection() + c.initiate_connection() + c.send_headers(1, self.example_request_headers, end_stream=True) + c.clear_outbound_data_buffer() + + if stream_id is None: + window = c._inbound_flow_control_window_manager + else: + window = c.streams[stream_id]._inbound_window_manager + + original_window_size = window.current_window_size + original_max_window_size = window.max_window_size + + # Legal as a bare increment, but one byte too large when added to the + # current window. + overflow_increment = 2**31 - original_window_size + + with pytest.raises(h2.exceptions.FlowControlError): + c.increment_flow_control_window( + increment=overflow_increment, stream_id=stream_id, + ) + + assert c.data_to_send() == b"" + assert window.current_window_size == original_window_size + assert window.max_window_size == original_max_window_size + + # The window is still usable after the rejected increment. + c.increment_flow_control_window(increment=5, stream_id=stream_id) + + expected_frame = frame_factory.build_window_update_frame( + stream_id=(stream_id or 0), increment=5, + ) + assert c.data_to_send() == expected_frame.serialize() + + @pytest.mark.parametrize("stream_id", [None, 1]) + def test_local_increment_to_exact_limit_is_allowed( + self, frame_factory, stream_id, + ) -> None: + """ + An increment that brings the window to exactly the protocol maximum is + accepted and emits a single WINDOW_UPDATE. + """ + c = h2.connection.H2Connection() + c.initiate_connection() + c.send_headers(1, self.example_request_headers, end_stream=True) + c.clear_outbound_data_buffer() + + if stream_id is None: + window = c._inbound_flow_control_window_manager + else: + window = c.streams[stream_id]._inbound_window_manager + + legal_increment = 2**31 - 1 - window.current_window_size + + c.increment_flow_control_window( + increment=legal_increment, stream_id=stream_id, + ) + + expected_frame = frame_factory.build_window_update_frame( + stream_id=(stream_id or 0), increment=legal_increment, + ) + assert c.data_to_send() == expected_frame.serialize() + assert window.current_window_size == 2**31 - 1 + assert window.max_window_size == 2**31 - 1 + def test_send_update_on_closed_streams(self, frame_factory) -> None: c = h2.connection.H2Connection() c.initiate_connection()