Skip to content

Enforce HTTP/2 MAX_CONCURRENT_STREAMS with REFUSED_STREAM - #40

Open
ulfjack wants to merge 2 commits into
mainfrom
ulfjack/sec-hardening-3
Open

Enforce HTTP/2 MAX_CONCURRENT_STREAMS with REFUSED_STREAM#40
ulfjack wants to merge 2 commits into
mainfrom
ulfjack/sec-hardening-3

Conversation

@ulfjack

@ulfjack ulfjack commented Aug 10, 2026

Copy link
Copy Markdown
Owner

We advertise SETTINGS_MAX_CONCURRENT_STREAMS=100 but never enforced it, so
a peer that ignores the advertised limit could open unbounded streams and
grow the streams map (and, transitively, heldRequests) without limit.

Refuse a HEADERS frame that would exceed the limit with RST_STREAM
(REFUSED_STREAM, retryable) instead of opening the stream (RFC 9113 §5.1.2).
The streams map holds exactly the open/half-closed streams that count toward
the limit, and heldRequests entries are a subset of open streams, so both are
now bounded. HPACK is decoded first so the connection-global dynamic table
stays in sync whether or not the stream is refused.

Co-Authored-By: Claude Opus 4.8 noreply@anthropic.com

ulfjack and others added 2 commits August 10, 2026 17:16
For locally-served chunked requests the only size guard compared
maxDecodedBodyBytes against ChunkedBodyScanner.decodedByteCount(), which counts
only chunk DATA bytes. Chunk-extension (SIZE_EXT), trailer, and unrecognized
size-line bytes are consumed uncounted while every raw byte is still forwarded
to LocalHttpRequestStage's unbounded bodyBuffer, so an endless chunk-extension
("1;AAAA...") or trailer buffers to OutOfMemoryError regardless of the
configured ceiling — one unauthenticated connection crashes the JVM.

Have ChunkedBodyScanner track rawByteCount() (framing + data) alongside
decodedByteCount(), and enforce a total raw-byte ceiling in
HttpServerStage.readBody next to the decoded one: 2x maxDecodedBodyBytes + 8 KiB
(finite, proportional so it does not false-positive on legitimate many-chunk
bodies; off when the policy is unlimited). Per-element caps alone don't suffice
— many tiny data chunks each with a capped extension still aggregate past the
decoded ceiling; a raw bound covers all framing variants. Also give the SIZE
state a terminal else so a non-hex size-line byte errors (400) instead of being
silently consumed.

Tests: ChunkedBodyScannerTest.invalidCharInSizeLine_setsError and
rawByteCount_countsFramingAndData;
ChunkedBodyIntegrationTest.chunkedFramingBytesBoundedBy413 (20 KB extension
under an 8-byte policy -> 413).

Verification (background; SKIP_PRECOMMIT=1, inline coverage exceeds the 2-min
foreground limit):
  * bazel test //...            18/18 (--flaky_test_attempts=3)
  * coverage-gate               92.43% (7837/8479) >= 90%
  * bazel run //:format.check   clean

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
We advertise SETTINGS_MAX_CONCURRENT_STREAMS=100 but never enforced it, so
a peer that ignores the advertised limit could open unbounded streams and
grow the streams map (and, transitively, heldRequests) without limit.

Refuse a HEADERS frame that would exceed the limit with RST_STREAM
(REFUSED_STREAM, retryable) instead of opening the stream (RFC 9113 §5.1.2).
The streams map holds exactly the open/half-closed streams that count toward
the limit, and heldRequests entries are a subset of open streams, so both are
now bounded. HPACK is decoded first so the connection-global dynamic table
stays in sync whether or not the stream is refused.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@ulfjack
ulfjack force-pushed the ulfjack/sec-hardening-2 branch from 1e0f663 to e48d13d Compare August 10, 2026 17:16
@ulfjack
ulfjack force-pushed the ulfjack/sec-hardening-3 branch from 51d3caf to 412a3e5 Compare August 10, 2026 17:16
Base automatically changed from ulfjack/sec-hardening-2 to main August 10, 2026 17:18
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