Skip to content

h2: a backend request encoded but not sent is re-linked to a fresh backend with stale HEADERS frames #1632

Closed

Description

@FlorentinDUBOIS

Found by the adversarial review of #1630 (#1627). Adjacent to #1627 and #1631, and not fixed by either.

Mechanism

A request relayed to an H2 backend is encoded by that backend connection's write pass: kawa.prepare pops the request's StatusLine/Header/Flags blocks out of stream.front.blocks and leaves HEADERS/CONTINUATION frames in stream.front.out. Those frames carry the stream id of that backend connection and field blocks HPACK-encoded against that connection's encoder.

The request counts as sent only once Kawa::consume moves a byte (kawa.consumed). When the first write of the request takes 0 bytes, the stream is parked (expect_write) with front.out full and front.consumed == false.

If the backend connection then goes away, two paths treat the request as untouched and link the stream to a fresh backend:

  • end_stream_decision (lib/src/protocol/mux/shared.rs) answers EndStreamAction::Reconnect on !stream.front.consumed, and ConnectionH2::end_stream's Reconnect arm (lib/src/protocol/mux/h2.rs) sets StreamState::Link and queues the stream in pending_links;
  • ConnectionH2::handle_goaway_frame (lib/src/protocol/mux/h2.rs) re-links every stream above last_stream_id whose front.consumed is false.

Neither path clears front.out or restores the popped blocks, and ConnectionH2::start_stream does not either. The new backend connection's write pass then transmits the stale frames first.

Scenario

  1. An H2 backend connection is established. Its socket is full, for example behind another stream's upload.
  2. A client request is linked to it. The write pass encodes its HEADERS and parks the stream: the write took 0 bytes.
  3. The backend closes the connection, or sends GOAWAY with a last_stream_id below the stream.
  4. The stream is re-linked to a new H2 backend connection.

Effect (by code reading, no reproduction yet)

The new backend connection receives:

  • a HEADERS frame on the OLD connection's stream id, which the new connection never opened;
  • a field block encoded against the OLD encoder's dynamic table, so its dynamic indexes resolve to nothing, or to other entries on the new decoder.

The request's own header blocks are gone from front.blocks, so the new connection cannot re-encode them, and its own stream id for the request never receives a HEADERS frame.

The likely outcomes are COMPRESSION_ERROR or PROTOCOL_ERROR on the new backend connection, which takes down every stream it carries, or a request carrying wrong fields.

H1 backends are not affected the same way: the H1 converter's output is protocol-level bytes that a fresh H1 connection can send as they are.

Options

  • Treat a front that has been prepared (!front.out.is_empty(), or blocks already popped) as consumed for H2 backends. The stream then gets the non-retry outcome, 502 or RST(REFUSED_STREAM), instead of Reconnect.
  • Keep the request's un-encoded blocks until the HEADERS frame has started on the wire. That is the send-time encoding shape of hyperium/h2's FramedWrite::buffer.

Either needs a connection-level test with an H2 backend, a 0-byte first write, and a close or GOAWAY.

Activity

  1. FlorentinDUBOIS commented on Sep 27, 2026

    @FlorentinDUBOIS
    CollaboratorAuthor

    A worse variant than the one described above, found while reviewing #1630.

    Router::connect (lib/src/protocol/mux/router.rs) links a stream to an existing Connected, non-draining H2 backend connection of the same cluster before it considers opening a new one, picking the one with the fewest active streams. That connection is usually already shared with other clients' streams, and its HPACK decoder state was built by its encoder, not by the one that encoded the stale block.

    When the re-linked stream's stale HEADERS frame goes out on that shared connection:

    • if the old stream id is above the highest id this connection has opened, the backend accepts it as a new stream and decodes the stale field block against the shared connection's dynamic table. Its dynamic indexes then resolve to entries other clients' requests inserted there, so the backend rebuilds a request carrying fields of other clients (for example an indexed x-api-key or authorization-like custom header) — a cross-client field substitution, with no error;
    • otherwise (id already used or not odd-and-increasing as required by RFC 9113 §5.1.1), or when an index is out of range, the backend answers PROTOCOL_ERROR or COMPRESSION_ERROR with GOAWAY, which tears down every stream multiplexed on the shared connection.

    Both outcomes come from the same root cause: the request's un-encoded blocks were consumed by kawa.prepare on the first connection, and front.out still holds frames bound to that connection's stream id and encoder when the stream is re-linked.

  2. FlorentinDUBOIS commented on Sep 28, 2026

    @FlorentinDUBOIS
    CollaboratorAuthor

    Scope correction: Router, and with it every backend connection in Router::backends, belongs to a single Mux session (field Mux::router, lib/src/protocol/mux/mod.rs). An H2 backend connection is therefore shared only by the streams of one frontend connection, never between distinct client TCP connections. Any cross-user impact requires an intermediary (a CDN or load balancer, for example) that multiplexes several users over one frontend connection to sozu.

    For this issue: the re-link can only reach another backend connection of the same session. The stale field block is then decoded against a table built by that session's other streams, so the substitution described above happens between streams of one frontend connection: requests of the same client, or of different users behind the same intermediary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions