Skip to content

fix(proxy-protocol): forward the header a RelayHeader cluster never sent - #1348

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/relay-header-forwarding
Sep 19, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/relay-header-forwarding

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

A RelayHeader cluster exists so the backend receives the client's own PROXY-v2 header verbatim. It
was receiving nothing at all, and the write loop that should have sent it could not terminate.

The defect

RelayProxyProtocol::readable consumed every byte it had just read — the header included — before
back_writable had forwarded any of it. One line, two consequences.

The header was never forwarded. This state re-serializes nothing: back_writable forwards the
buffered prefix rather than rebuilding a header from addresses, so consuming that prefix destroyed
the only copy. The comment at the parse site asserted the opposite — "The header bytes themselves are
still forwarded verbatim onto the backend by back_writable" — and is corrected.

back_writable could not terminate. With the buffer emptied it looped on socket.write(&[]),
which a connected non-blocking socket answers Ok(0): cursor_header never advanced toward
header_size, so that exit was unreachable and the loop's only other exit was its Err arm. The loop
sits inside a single back_writable call — MAX_LOOP_ITERATIONS (lib/src/tcp.rs:1403) bounds the
outer dispatch loop in ready_inner, not this inner one.

Reached by a cluster configured with both expect_proxy and send_proxy, which yields
ProxyProtocolConfig::RelayHeader and builds TcpStateMachine::RelayProxyProtocol.

Provenance

Not a recent regression, and not merely pre-existing: 51aedab9 (2018-10-18) introduced it, and
it has shipped in every release since 0.10.0.

The BufferQueue that commit replaced kept the parse cursor and the output queue as two separate
things, so readable ran consume_parsed_data(sz) and slice_output(sz) — the header was
retired as parsed input while those same bytes stayed queued for output, and back_writable drained
them with next_output_data() / consume_output_data(sz). Flattening the buffer to a single cursor
collapsed both calls into one consume(sz) and silently dropped the output half.

This restores that behaviour on the single-cursor buffer: readable consumes nothing, back_writable
consumes exactly as it forwards.

Pipelined payload

Bytes the client pipelined into the same read stay in place for the pipe phase. into_pipe hands the
same Checkout to Pipe::new, whose arm_inherited_buffer_writes arms the backend WRITABLE
readiness exactly when it is non-empty, so the tail is flushed rather than dropped.

Three adjacent defects, fixed because this makes them reachable

  1. back_writable's Err arm reset both readiness words for every error kind. Dead while the
    buffer was always empty — the loop only ever called write(&[]) and got Ok(0) — but a relay that
    really writes can be backpressured, and a v2 header is 16 + len with len: u16. WouldBlock now
    clears only the stale WRITABLE event and keeps the interest, as send.rs does, so the next epoll
    edge resumes a half-written header instead of the session dying at timeout with a truncated header
    on the backend. Interrupted leaves both words untouched so ready_inner retries under its own
    bound — removing WRITABLE there would be a lost wakeup, since the socket did not change state.
  2. into_pipe restored readiness with a bare event assignment, overwriting the arm Pipe::new
    had just set — the exact pattern build_pipe_from_preread's comment forbids, and the last of the
    three pipe-entry paths still using it. Now calls restore_readiness_events.
  3. readable had no zero-length-read guard. tcp_socket_read returns (0, Continue) as soon as
    the slice it is handed is empty, which is what space() yields on a full buffer, so a client that
    declared a large len and stalled had ready_inner re-enter until MAX_LOOP_ITERATIONS. Now
    drops the READABLE event, as expect.rs does.

Why it survived eight years

The only existing test, e2e's try_tcp_sni_relay_proxy_forwards_header_verbatim, uses an SNI listener,
so upgrade_sni_preread routes it straight to build_pipe_from_preread without consuming the
header and never builds a RelayProxyProtocol. That path was always correct.
RelayProxyProtocol's own forwarding had no coverage whatsoever.

Tests — eight, each seen red under a documented mutation

back_writable_returns_instead_of_spinning, back_writable_forwards_the_header_verbatim,
back_writable_leaves_pipelined_payload_for_the_pipe_phase,
a_zero_length_write_yields_instead_of_spinning,
back_writable_parks_the_session_on_a_backpressured_backend, readable_yields_on_a_zero_length_read,
back_writable_keeps_its_readiness_when_a_signal_interrupts_the_write — plus
into_pipe_rearms_the_backend_write_for_pipelined_payload in lib/src/tcp.rs, because the upgrade
needs a TcpListener whose constructor and fields are private to that module.

The EINTR test drives a real socketpair filled to zero free space with MSG_DONTWAIT, then signals
the thread with SIGUSR1 installed without SA_RESTART, so the parked send returns Interrupted
rather than a partial write. That arm is defensive — production backend sockets come from
mio::net::TcpStream::connect and are always non-blocking, so their send answers EAGAIN — which
is why exercising it needs a socketpair at all.

The test harness itself was corrected twice during review: it gated on a quantity the code under test
mutates, so a consumption defect stalled the harness and reddened every test in the module without
reaching the code under test
. It now gates on metrics.bin (bytes read off the socket), and the
pre-back_writable stages report failure as a value with their own message rather than surfacing as
the wedge assertion.

cargo +nightly fmt --all -- --check                                              exit 0
cargo +1.93.1 clippy --workspace --all-targets --all-features --locked -D warn   exit 0
cargo +1.93.1 test -p sozu-lib --locked --all-features --test-threads=1
    834 passed, 0 failed

`RelayProxyProtocol::readable` consumed every byte it had just read — the
PROXY-v2 header included — before `back_writable` had forwarded any of it.
One line, two defects.

A `RelayHeader` cluster exists so the backend receives the client's own
header verbatim, and it was receiving nothing: this state re-serializes
nothing, `back_writable` forwards the buffered prefix rather than rebuilding
a header from `addresses`, so consuming that prefix destroyed the only copy.
The comment at the parse site asserted the opposite and is corrected.

`back_writable` could not then terminate. With the buffer emptied it looped
on `socket.write(&[])`, which a connected non-blocking socket answers
`Ok(0)`: `cursor_header` never advanced toward `header_size`, so that exit
was unreachable and the loop's only other exit was its `Err` arm. The loop
sits inside one `back_writable` call, and `MAX_LOOP_ITERATIONS` bounds the
outer dispatch loop in `ready_inner`, not this inner one.

`51aedab9` (2018-10-18) introduced it and it has shipped since 0.10.0. The
`BufferQueue` it replaced kept the parse cursor and the output queue
separate, so `readable` ran `consume_parsed_data` and `slice_output`: the
header was retired as parsed input while those same bytes stayed queued for
output, and `back_writable` drained them with `next_output_data` /
`consume_output_data`. Flattening to a single cursor collapsed both into one
`consume(sz)` and dropped the output half. This restores that behaviour on
the single-cursor buffer: `readable` consumes nothing, `back_writable`
consumes exactly as it forwards.

Bytes the client pipelined into the same read stay in place for the pipe
phase — `into_pipe` hands the same `Checkout` to `Pipe::new`, whose
`arm_inherited_buffer_writes` arms the backend WRITABLE readiness when it is
non-empty. A zero-length write now drops that event and yields, and the
write is clamped to the unsent header tail, so a future read path that
consumed the header again would cost one wasted syscall rather than a
non-terminating loop.

Three adjacent defects the forwarding fix makes reachable are fixed with it.
`back_writable`'s `Err` arm reset both readiness words for every error kind,
which was dead while the buffer was always empty; `WouldBlock` now clears
only the stale WRITABLE event and keeps the interest, as `send.rs` does, and
`Interrupted` leaves both untouched so `ready_inner` retries under its own
bound. `into_pipe` restored readiness with a bare event assignment that
overwrote the arm `Pipe::new` had just set — the pattern
`build_pipe_from_preread` forbids, and the last pipe-entry path still using
it — and now calls `restore_readiness_events`. `readable` had no
zero-length-read guard, so a client that declared a large length and stalled
left READABLE set and had `ready_inner` re-enter until `MAX_LOOP_ITERATIONS`.

The SNI-preread `RelayHeader` path was never affected: `upgrade_sni_preread`
routes it to `build_pipe_from_preread` without consuming the header and
never builds a `RelayProxyProtocol`, which is why the existing e2e test
covered that path and not this one. `RelayProxyProtocol`'s own forwarding had
no coverage at all; it now has eight tests, each seen red under a documented
mutation.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit b7d41b9 into main Sep 19, 2026
44 of 45 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/relay-header-forwarding branch September 19, 2026 07:02
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