Skip to content

fix(mux-h2): accept a short read landing past the 24-octet preface - #1490

Merged
FlorentinDUBOIS merged 2 commits into
mainfrom
fix/h2-preface-short-read
Sep 24, 2026
Merged

FlorentinDUBOIS merged 2 commits into
mainfrom
fix/h2-preface-short-read

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Fixes #1487.

ConnectionH2::handle_read's short-read branch rejects any accumulated window longer than the 24-octet preface, so a valid client whose read lands mid-SETTINGS-header is disconnected.

constant.starts_with(i) asks whether i is a prefix of the constant. The requested window is CLIENT_PREFACE_SIZE = 24 + parser::FRAME_HEADER_SIZE = 33, so once the accumulated buffer passes 24 octets the test is false for every possible input — including a byte-perfect preface followed by a byte-perfect SETTINGS header.

-   let i = kawa.storage.data();
-   if !b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n".starts_with(i) {
+   let (pri, i) = (serializer::H2_PRI.as_bytes(), kawa.storage.data());
+   if !pri.starts_with(&i[..i.len().min(pri.len())]) {

The width comes from serializer::H2_PRI, which already exists in mux/ and is used by the client-position arm of readable and by health_check.rs — so no length is duplicated, and the added lines contain no literal at all.

Red, with the sizes named

The defect is invisible at chunk size 24 and at 33 — the two a hand-written test is most likely to pick — so the regression test sweeps the axis instead of fixing a size. With the fix reverted:

these chunk sizes were disconnected instead:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 14, 15, 16, 25, 26, 27, 28, 29, 30, 31, 32]
test result: FAILED. 1 passed; 1 failed

That is wider than the nine sizes the issue sampled, and the two agree exactly: every size the issue reported failing is in this set, every size it reported surviving is absent. The extra 2..=10 and 13..=16 are sizes whose partial sums also land in the 25..=32 band, which the issue never measured.

Each chunk is driven to consumption — witnessed by expect_read == Some((Zero, CLIENT_PREFACE_SIZE - delivered)) — before the next write, so two loopback writes cannot coalesce into a non-short read and silently skip the guard.

The guard still refuses a genuinely invalid preface

an_invalid_client_preface_is_still_refused_before_the_window_is_filled corrupts the first and the last octet of the magic string and sweeps the window across (corrupt_at+1)..33, covering the whole 25..=32 band. The window stays short of CLIENT_PREFACE_SIZE, so parser::preface in the H2State::ClientPreface arm is unreachable and only the early guard can refuse it — "still early" is structural here, not asserted.

That test was itself seen red under its own named mutation (clipping to pri.len() - 1):

these (corrupt_at, window) pairs survived:
[(23, 24), (23, 25), ... (23, 32)]

so dropping one octet from the comparison is caught, and the test is not vacuous.

Unit, not e2e

e2e/ cannot deterministically place a socket_read boundary in 25..=32 through a real listener, and that boundary is the entire axis. The unit sweep controls it exactly. Reused the existing test_h2_connection harness; there was no preface harness.

Verification

cargo +nightly fmt --all -- --check                             exit 0
cargo build  --locked --no-default-features --features …        exit 0
cargo clippy --all-targets --locked … -- -D warnings            exit 0, 0 diagnostics
cargo test -p sozu-lib --locked …     ok. 1094 passed; 0 failed      (1092 + the 2 added)
cargo test -p sozu     --locked …     ok. 113 passed; 0 failed
cargo test -p sozu-e2e --locked … -- --skip tests::fuzz_tests::
                                      ok. 446 passed; 0 failed
check_doc_citations.py --root .       211 citations, 504 test names, 16 pinned blocks
check_doc_citations.py --base c0f01021   326 lines unchanged, 0 exempt spans
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked   exit 0

No test deleted or ignored, no assertion loosened, no lint escape, no #[allow] added.

A note on the two-line shape

LIFECYCLE.md and doc/ carry 43 h2.rs:NNN citations below this site, so a +1-line fix would have forced a 43-citation renumber and buried a two-line change. Binding let (pri, i) = (…) keeps the edit exactly 2-for-2, which is why the citation gate reports none of the 326 cited lines changed their text with 0 exempt spans.

LIFECYCLE.md §2.1 gains a step documenting the early guard and its tolerance rule — it was incomplete rather than wrong, the guard was simply undescribed. doc/h2_mux_internals.md was checked and says nothing about the early guard, so nothing there became false.

Left alone deliberately: CLIENT_PREFACE_SIZE = 24 + parser::FRAME_HEADER_SIZE still carries its own 24 literal. Pre-existing, and out of scope for this fix.

Provenance

Found by the H2 deterministic simulator (#1485) while building its read-fragmentation axis. The simulator carves the preface out of that axis and documents why at its single call site; this is the defect that made the carve-out necessary.

`ConnectionH2::readable` arms `expect_read` with `CLIENT_PREFACE_SIZE` —
the 24-octet magic string plus a 9-octet SETTINGS frame header, 33 in all
— and its short-read branch runs an early guard so a client that is
plainly not speaking H2 is dropped without waiting for the rest of the
window.

The guard tested the WHOLE accumulated buffer against the 24-octet
constant with `starts_with`, which asks whether the buffer is a prefix OF
that constant. Once the buffer passed 24 octets the answer was false for
every possible input, a byte-perfect preface followed by a byte-perfect
SETTINGS header included, so between 25 and 32 octets the guard refused
unconditionally: `MuxResult::CloseSession`, no GOAWAY, and an `EARLY
INVALID PREFACE` line naming the client as the offender.

Nothing about a TCP segment or a TLS record makes a read land on the
24-octet boundary — a small segment, a split TLS record, or one client
`write()` the kernel delivers in two pieces all produce it — so this
closed conforming connections at a rate set by the network rather than by
anything the peer did wrong.

Compare only the part the constant can cover, deriving the width from
`serializer::H2_PRI` rather than from a second hard-coded 24 so the two
cannot drift apart. A wrong octet anywhere in the first 24 is still
refused on the same short read, which is the only moment the early guard
can act at all: `parser::preface` in the `H2State::ClientPreface` arm is
reached only once the whole window has arrived.

The regression coverage sweeps the chunk size rather than fixing one,
because the defect is invisible at exactly 24 (the buffer never grows
past the magic string) and at exactly 33 (the request is filled, so the
short-read branch is never entered). Each piece is consumed before the
next is written, so two loopback writes cannot coalesce into a non-short
read and hide the guard.

The fix is deliberately line-count neutral: `LIFECYCLE.md` and `doc/`
carry 43 `h2.rs:NNN` citations below this site, and shifting them would
have buried a two-line fix under a mass renumbering.

Fixes #1487

Signed-off-by: Florentin Dubois <[email protected]>
Two claims in the preceding commit's prose went further than the evidence.

The CHANGELOG said the sweep's wider failing set was wider because the
original report's harness "let consecutive writes coalesce". Nothing
establishes that: the report drove a fixed `socket_read` chunk size, which
cannot coalesce, and it sampled nine sizes rather than sweeping. Every size
it reported failing appears in the sweep's set and every size it reported
surviving is absent from it, so the two measurements agree — the sweep is
wider only because it also covers the intermediate sizes the report never
measured.

`LIFECYCLE.md` said the sweep covers "every split" of the window. It covers
every fixed chunk size in `1..=40`, which is not every partition.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit fa0b93a into main Sep 24, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/h2-preface-short-read branch September 24, 2026 02:54
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.

mux-h2: a short read of 25..=32 octets disconnects a valid client, because the preface guard compares against a 24-octet constant

1 participant