Skip to content

fix: reserve priority bytes when fragmenting HEADERS - #1353

Open
feiiiiii5 wants to merge 2 commits into
python-hyper:masterfrom
feiiiiii5:feiiiiii5/D235
Open

feiiiiii5 wants to merge 2 commits into
python-hyper:masterfrom
feiiiiii5:feiiiiii5/D235

Conversation

@feiiiiii5

Copy link
Copy Markdown

H2Connection.send_headers(..., priority_weight=16) with a header block larger than the peer's max frame size raises a bare AssertionError (and under python -O would emit an oversized HEADERS frame).

Root cause

_build_headers_frames() slices the encoded header block using the full max frame size, but a priority block adds 5 bytes (4-byte depends_on/exclusive + 1-byte weight) to the first HEADERS payload. PR #1338 documented the same issue for PUSH_PROMISE.

Fix

Added an optional reserved_bytes parameter to H2Stream.send_headers()/_build_headers_frames(); the first fragment is reduced by that amount. H2Connection.send_headers() passes 5 when priority is present. The generic parameter composes with #1338's PUSH_PROMISE reservation and preserves default behaviour.

Verification

  • New parametrized client/server test at MAX_FRAME_SIZE=16384 with a ~50 KB incompressible value. Base-red: AssertionError at _prepare_for_sending (1 failed / 1 passed).
  • With fix: 2 targeted tests passed; full suite 1686 passed, coverage 100% (1918 statements, 466 branches, 0 partial). Both new branches covered.
  • ruff check src/ clean; mypy --strict and --strict-bytes clean.

No h2spec/interop run; python -O not re-run after the fix.

Comment thread tests/test_large_headers_priority.py Outdated
from h2.events import RequestReceived
from h2.settings import SettingCodes

MAX_FRAME_SIZE = 16384

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reuse MAX_FRAME_SIZE from h2.settings.SettingCodes

Comment thread tests/test_large_headers_priority.py Outdated
payload = payload.translate(
bytes.maketrans(bytes([0, 10, 13]), bytes([120, 121, 122])),
)
payload = b"x" + payload + b"x"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need random data as payload?
do we need 50,000 bytes?

Maybe simplify to 'payload' * 100 as set the frame size accordingly to work out.

Drop the random 50 KB payload for a deterministic field that HPACK
Huffman-encodes past one frame, and derive the frame bound from the
negotiated max_outbound_frame_size instead of a local copy of the
constant.
@feiiiiii5

Copy link
Copy Markdown
Author

Both points are addressed in 6f4c0a8.

  • The test no longer hardcodes a local MAX_FRAME_SIZE; it asserts against client.max_outbound_frame_size, the value negotiated from the peer's SETTINGS.
  • Dropped random.Random(0).randbytes(50_000). The header is now "~" * 20000: HPACK Huffman codes ~ in 13 bits, so the field block is ~32 KB with no randomness.

On "simplify to 'payload' * 100 and set the frame size accordingly": SETTINGS_MAX_FRAME_SIZE has a protocol minimum of 16384 bytes (RFC 7540 §6.5.2), so a header block has to exceed ~16 KB before it can span frames at all. 'payload' * 100 Huffman-encodes well below that and would not reach the fragmentation path the regression covers, which is why the payload stays in the tens of KB.

Verified on 6f4c0a8: pytest tests/test_large_headers_priority.py → 2 passed; full pytest tests/ → 1686 passed; ruff check and ruff format --check clean. Against base 2e4d223, the same test still fails at assert all(f.body_len <= self.max_outbound_frame_size ...) in connection.py:424 with the priority frame (1 failed, 1 passed), so it still pins the regression.

@feiiiiii5

Copy link
Copy Markdown
Author

Both are addressed in 6f4c0a8:

  • The 50 KB random payload is gone. The field is now "~" * 20000, which HPACK Huffman-encodes to about 32 KB, so it still spans more than one frame deterministically.
  • The local MAX_FRAME_SIZE = 16384 copy is gone too. The test no longer needs a constant: it asserts against the negotiated client.max_outbound_frame_size and requires the block to span more than one frame. That avoids duplicating the settings default while keeping the regression honest.

tests/test_large_headers_priority.py is 2 passed on this head.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants