Repository navigation
fix(mux-h2): accept a short read landing past the 24-octet preface - #1490
Merged
Merged
Conversation
`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
requested review from
Wonshtrum and
llenotre
as code owners
September 24, 2026 01:25
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 whetheriis a prefix of the constant. The requested window isCLIENT_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.The width comes from
serializer::H2_PRI, which already exists inmux/and is used by the client-position arm ofreadableand byhealth_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:
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..=10and13..=16are 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_filledcorrupts 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 ofCLIENT_PREFACE_SIZE, soparser::prefacein theH2State::ClientPrefacearm 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):so dropping one octet from the comparison is caught, and the test is not vacuous.
Unit, not e2e
e2e/cannot deterministically place asocket_readboundary in 25..=32 through a real listener, and that boundary is the entire axis. The unit sweep controls it exactly. Reused the existingtest_h2_connectionharness; there was no preface harness.Verification
No test deleted or ignored, no assertion loosened, no lint escape, no
#[allow]added.A note on the two-line shape
LIFECYCLE.mdanddoc/carry 43h2.rs:NNNcitations below this site, so a +1-line fix would have forced a 43-citation renumber and buried a two-line change. Bindinglet (pri, i) = (…)keeps the edit exactly 2-for-2, which is why the citation gate reportsnone of the 326 cited lines changed their textwith 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.mdwas checked and says nothing about the early guard, so nothing there became false.Left alone deliberately:
CLIENT_PREFACE_SIZE = 24 + parser::FRAME_HEADER_SIZEstill carries its own24literal. 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.