Skip to content

Reject unrepresentable body lengths in Frame.serialize() - #176

Open
AryanRG wants to merge 1 commit into
python-hyper:masterfrom
AryanRG:fix/frame-serialize-length-bound
Open

AryanRG wants to merge 1 commit into
python-hyper:masterfrom
AryanRG:fix/frame-serialize-length-bound

Conversation

@AryanRG

@AryanRG AryanRG commented Oct 17, 2026

Copy link
Copy Markdown

Frame.serialize() currently emits a body of 16,777,216 bytes with a zero-length header because the length is truncated to 24 bits. Reject bodies above FRAME_MAX_ALLOWED_LEN with InvalidFrameError before building the header, while preserving the valid 16,777,215-byte boundary. The check includes padding and other serialized body overhead.

Seven regression cases cover DATA and CONTINUATION maximum-size round trips, rejection at maximum plus one and plus two, and overflow caused by the PADDED length byte. Includes a changelog entry naming Frame.serialize().

ExtensionFrame.serialize() is a separate override and remains outside this patch; #169 addresses its stale body length. Peer-negotiated lower frame limits remain the caller's responsibility, and the check runs after body allocation. This is separate from the SETTINGS flag handling in #175 and header parsing tests in #173.

Validation on macOS ARM64 / CPython 3.14.4: 155 tests passed, including the pinned HTTP/2 fixture collection, with 100% statement and branch coverage. Configured source Ruff, strict mypy, HTML documentation, and whitespace checks passed. Five of the seven regression cases failed against the original runtime source. Other platforms and Python versions were not run locally.

AI assistance was used for the investigation, implementation, tests, validation, and PR text.

Comment thread src/hyperframe/frame.py

if self.body_len > FRAME_MAX_ALLOWED_LEN:
msg = f"Frame body exceeds maximum allowed length: {self.body_len} > {FRAME_MAX_ALLOWED_LEN}"
raise InvalidFrameError(msg)

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.

Raising here already, will put us in a tough spot over at https://github.com/python-hyper/h2/blob/2e4d22303af8710642f2f493c103860855710f02/src/h2/connection.py#L423-L424

At runtime, this sits on the hot path, so ideally, we only check /assert / validate the frame length only once. Do you have any suggestions how to unify this? Maybe pass in the currently negotiated max frame size from the connection into, e.g., Frame.serialize(max_size: int = FRAME_MAX_ALLOWED_LEN) ?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants