fix(relay): detect CRLFCRLF across chunks + cap drain buffer#427
Merged
Conversation
bootstrap drain loop's `chunk.windows(4).any(...)` only inspected latest read. CRLFCRLF straddling two reads = miss + spin to 5s deadline. no chunk-count cap either. now: accumulate into bounded buffer (8 KiB, matches PROXY_REQUEST_LINE_BUFFER for symmetric header budget), search whole buffer each iteration. seed w/ trailing 3 bytes of already_read so request-line/chunk boundary also covered. bail w/ tracing::warn + InvalidData err on cap overflow → connection closes promptly. tests: cross-chunk detection + cap-overflow bail. rejected: rolling 4-byte tail. simpler in bytes but harder to reason about correctness around edge cases (short reads, seeded state). 8 KiB buffer cost trivial vs clarity gain. Closes #238
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.
Why
bootstrap drain loop in
handle_bootstrap_request_after_linereads 1 KB chunks looking for HTTP end-of-headers\r\n\r\n. butchunk.windows(4).any(...)only scans latest chunk. marker straddling two reads = miss → loop spins untilBOOTSTRAP_IO_TIMEOUT(5s). also no cap on chunk count: peer can stream forever w/ no marker.low severity (path is rate-limited by
MAX_CONCURRENT_BOOTSTRAP_CONNECTIONSsemaphore + 5s deadline), but a robustness gap worth closing.Fix
accumulate every read into bounded
Vec+ search whole buffer each iteration → cross-chunk marker found. cap =BOOTSTRAP_DRAIN_BUFFER_CAP = 8 KiB, matchesPROXY_REQUEST_LINE_BUFFERso relay applies one symmetric header budget. exceed cap →tracing::warnone line +Err(InvalidData)→dispatch_connectioncloses connection. seed accumulator w/ trailing 3 bytes ofalready_readso marker straddling request-line/chunk boundary is also caught.rejected: rolling 4-byte tail across iterations. simpler in bytes but harder to reason about (short reads, seeded state, off-by-one risk). 8 KiB cost trivial vs clarity gain.
Verify
cargo fmt --checkcleancargo clippy -p willow-relay --all-targets -- -D warningscleancargo test -p willow-relay12/12 pass (10 existing + 2 new)proxy_bootstrap_detects_crlf_crlf_split_across_chunks— sends\r\n\rthen pause then\n; asserts response in <2s (would hang to 5s deadline w/o fix)proxy_bootstrap_closes_when_drain_buffer_cap_exceeded— streams 16 KiB no marker; asserts EOF in <2s w/ no200 OKCloses #238
Generated by Claude Code