Skip to content

feat: add max_buffered_frame_len to limit buffered non-DATA frames - #387

Open
jeremyjpj0916 wants to merge 1 commit into
hyperium:masterfrom
jeremyjpj0916:max-buffered-frame-len
Open

jeremyjpj0916 wants to merge 1 commit into
hyperium:masterfrom
jeremyjpj0916:max-buffered-frame-len

Conversation

@jeremyjpj0916

Copy link
Copy Markdown

Non-DATA frames are buffered in full before they're decoded, and unknown frame types are buffered in full before they're skipped. Nothing checks the declared payload length. When a frame hasn't fully arrived, Frame::decode returns Incomplete(2 + len), FrameDecoder stores it as expected, and FrameStream::poll_next keeps reading into its BufList until that many bytes are available.

So a peer can open a request stream, send a HEADERS frame header declaring a 2^62 - 1 byte payload, and keep sending bytes. accept() / resolve_request() never return, and memory grows until the process is killed. QUIC flow control doesn't stop this, because each read grants the peer more credit. The idle timeout doesn't stop it either, because the peer is active. The peer's control stream (SETTINGS, GOAWAY) has the same problem.

Changes

  • Add max_buffered_frame_len(u64) to server::Builder and client::Builder.
  • Frame::decode_bounded(buf, max) checks the declared length right after the type and length varints are read. If the length is over the limit, it returns FrameError::ExceedsMaxBufferedLen before taking the payload. Frame::decode calls it with u64::MAX.
  • FrameDecoder carries the limit and does not set expected for a refused frame, so FrameStream stops buffering toward it. The error maps to a connection error of type H3_EXCESSIVE_LOAD, per RFC 9114 §10.5.
  • The limit applies to request streams (server and client), control and push streams, and WebTransport bidirectional streams. FrameStream::split keeps it on both halves.
  • The check compares the u64 varint before converting to usize. A length that doesn't fit in a usize is refused for every frame type, so it can't be truncated on 32-bit targets.
  • DATA frames are streamed, not buffered, so the limit doesn't apply to them.
  • The default is no limit, so existing behavior doesn't change. I'm happy to set a default instead (for example, tied to max_field_section_size) if you'd prefer.

Tests

  • frame.rs unit tests:
    • Oversized HEADERS, SETTINGS and unknown frames are refused, with nothing consumed and expected left unset.
    • A frame exactly at the limit is accepted, and one byte over is refused.
    • A DATA frame larger than the limit still decodes.
    • The default decoder is unbounded.
    • The refusal shows up through FrameStream::poll_next.
    • On 32-bit only: a length above usize::MAX is refused.
  • Connection tests over quinn:
    • A peer declaring an oversized HEADERS or unknown frame on a request stream, or an oversized SETTINGS frame on the control stream, gets the connection closed with H3_EXCESSIVE_LOAD. This is checked on both server and client.
    • A request body much larger than the limit still goes through.
  • I checked that the connection tests fail on their timeout when the check is disabled.
  • The duvet citation for §10.5 is added, and its todo and snapshot are updated.

I ran cargo test with each CI feature combination, plus cargo fmt, cargo clippy (no new warnings), the MSRV 1.74.1 checks, and duvet report. The i686 library builds, but I couldn't run the 32-bit tests locally.

Non-DATA frames are buffered completely before they are decoded, and
unknown frame types are buffered completely before they are skipped.
The declared payload length was never checked, so a peer could declare
a frame of up to 2^62-1 bytes and keep sending payload. QUIC flow
control does not limit this, since reading from the stream grants the
peer more credit.

Add `max_buffered_frame_len` to the server and client builders. A
non-DATA frame declaring a longer payload is refused as soon as its
length is read, before the payload is buffered, with a connection error
of type H3_EXCESSIVE_LOAD. The limit applies to request streams, control
and push streams, and WebTransport bidirectional streams. DATA frames
are streamed and are not affected.

The declared length is compared as a u64 before converting it to usize,
and a length that does not fit in a usize is refused for every frame
type, so it cannot be truncated on 32-bit targets.

The default is no limit, so existing behavior is unchanged.

This branch has not been deployed

No deployments
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.

1 participant