Skip to content

fix(proxy-protocol): discard the address block of a PROXY-v2 LOCAL header - #1334

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/ppv2-local-address
Sep 18, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/ppv2-local-address

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Security: a forged PROXY-v2 LOCAL header lets any peer choose the source address Sōzu trusts

The PROXY protocol v2 wire format does not tie the LOCAL command to the AF_UNSPEC family —
only HAProxy's own emitter pairs the two. A crafted peer can send LOCAL (ver/cmd 0x20) with
family 0x11 and a fully populated 12-byte AF_INET block, and parse_addr_v2 switches on the
family nibble alone.

That block reached every consumer of HeaderV2::addr, all of which attribute
ProxyAddr::source() to the client. So any peer able to reach a listener with expect_proxy = true,
or a relay or SNI-preread PROXY listener, could forge the source address Sōzu

  • writes to its access logs,
  • injects as X-Real-IP,
  • and counts against max_connections_per_ip — evading that cap entirely by picking a fresh
    address per connection.

Fix

Per the HAProxy PROXY protocol specification §2.2, a LOCAL header describes a connection the
upstream proxy originated on its own behalf (typically a health check) and its address block must be
ignored in favour of the real socket endpoints. parse_v2_header now yields ProxyAddr::AfUnspec
for every LOCAL header — one choke point rather than a check at each consumer.

The match is exhaustive on purpose: a future command must make an explicit attribution decision
rather than silently inherit PROXY's.

One choke point is sound because none of the consumers reads command or family, none
re-serializes the parsed header (relay.rs forwards the buffered bytes verbatim), and all obtain
the address exclusively through this ProxyAddr.

The six consumers do not resolve AfUnspec alike

This is operator-visible and the documentation now says so, because it got it wrong three times
during review:

consumer on LOCAL
ExpectProxyProtocol::into_pipe falls back to peer_addr
RelayProxyProtocol::into_pipe falls back to peer_addr
TCP SNI preread (Output::Routed { proxy_source }) falls back to peer_addr
TcpSession::effective_session_address — feeds the raw-TCP max_connections_per_ip gate falls back to peer_addr
HttpSession::upgrade_expect closes the session
HttpsSession::upgrade_expect closes the session

The HTTP pair each need both a source and a destination; AfUnspec supplies neither, and the
refused upgrade becomes SessionIsToBeClosed — closed at the expect stage, before any request is
read.

Not a regression for legitimate traffic. HAProxy pairs LOCAL with AF_UNSPEC, which already
parsed to AfUnspec, so those sessions already closed. Only the forged populated block changes
behaviour: from "upgrade with attacker-chosen addresses" to "close".

Framing and validation unchanged

The block is still parsed rather than skipped, so a malformed or unknown family is still rejected and
the declared length still delimits the header; the family byte is still reported as read.
it_should_not_parse_proxy_protocol_with_unknown_family is untouched and green.

Tests — red in both directions

  • Command::Local => parsed_addr (undoing the fix) turns exactly five tests red: the IPv4 and IPv6
    parser guards, and one guard at each of expect.rs, relay.rs, tcp_preread.
  • The opposite mutation, Command::Proxy => AfUnspec, turns six positive guards red — proving
    this secures PROXY support rather than disabling it.

Each carries a To SEE THESE RED: block naming the exact mutation, per the house convention.

Also in this changeset

Three comments claimed the peer_addr fallback already happened "when the header carried
Command::Local (no encapsulated addresses)". The parenthetical was false — that is exactly the
assumption this fix exists to remove — and they are corrected.

While editing the module's LIFECYCLE.md, 29 of its 35 file.rs:NNN citations proved stale: single
lines uniformly off by one (a //! module-doc line inserted after the document was written), ranges
off by 38 to 57 (the debug_assert! campaign growing the functions). They are corrected here because
that file documents the behaviour this PR changes. The same rot elsewhere in doc/ is filed
separately rather than expanded into this PR.

Verification

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    818 + 1 + 5 passed, 0 failed

The HTTP close was confirmed empirically, not by reading: flipping the expect_proxy e2e emitter to
Command::Local turns test_x_real_ip_send_and_elide red with "backend received no request".

…ader

The PROXY protocol v2 wire format does not tie the `LOCAL` command to the
`AF_UNSPEC` family — only HAProxy's own emitter pairs the two. A crafted
peer could therefore send `LOCAL` (0x20) with family `0x11` and a fully
populated 12-byte `AF_INET` block, and `parse_addr_v2` switches on the
family nibble alone, so that block reached every consumer of
`HeaderV2::addr`. All six attribute `ProxyAddr::source()` to the client, so
any peer able to reach a listener with `expect_proxy = true`, or a relay or
SNI-preread PROXY listener, could forge the source address Sōzu records in
its access logs, injects as `X-Real-IP`, and counts against
`max_connections_per_ip` — and so evade that cap by picking a fresh address
per connection.

Per the HAProxy PROXY protocol specification §2.2, a `LOCAL` header
describes a connection the upstream proxy originated on its own behalf,
typically a health check, and its address block must be ignored in favour
of the real socket endpoints. `parse_v2_header` now yields
`ProxyAddr::AfUnspec` for every `LOCAL` header. The `match` is exhaustive
on purpose: a future command must make an explicit attribution decision
rather than inherit `PROXY`'s by default.

One choke point serves all six consumers: none of them reads `command` or
`family`, none re-serializes the parsed header — `relay.rs` forwards the
buffered bytes verbatim — and all six obtain the address exclusively
through this `ProxyAddr`.

The six do not resolve `AfUnspec` alike, and that split is operator-visible.
The four TCP-side consumers fall back to the front socket's `peer_addr`, so
the session proceeds attributed to the real peer. The two HTTP-side ones do
not: `HttpSession::upgrade_expect` and `HttpsSession::upgrade_expect` each
need both a source and a destination, `AfUnspec` supplies neither, and the
refused upgrade becomes `SessionIsToBeClosed` — the session is closed at the
expect stage, before any request is read. That is not a regression for
legitimate traffic, because HAProxy pairs `LOCAL` with `AF_UNSPEC`, which
already parsed to `AfUnspec`; only the forged populated block changes
behaviour, from "upgrade with attacker-chosen addresses" to "close".

Framing and validation are unchanged. The block is still parsed rather than
skipped, so a malformed or unknown family is still rejected and the declared
length still delimits the header, and the `family` byte is still reported as
it was read off the wire.

A `PROXY` header carrying the same block is still honoured, and the guards
for that were seen red under the opposite mutation, which proves this
secures PROXY support rather than disabling it.

Three comments asserting the fallback already happened "when the header
carried `Command::Local` (no encapsulated addresses)" were false and are
corrected. While editing the module's lifecycle document, 29 of its 35
`file.rs:NNN` citations proved stale — single lines uniformly off by one
after a module-doc line was inserted, ranges off by 38 to 57 after the
`debug_assert!` campaign grew the functions — and are corrected too.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 1e7ed9c into main Sep 18, 2026
23 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/ppv2-local-address branch September 18, 2026 20:16
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