Skip to content

Believe X-Forwarded-For only from configured proxies - #13

Merged
CodingRule merged 3 commits into
CodingRule:mainfrom
shiwng:trusted-proxy-xff
Nov 26, 2026
Merged

CodingRule merged 3 commits into
CodingRule:mainfrom
shiwng:trusted-proxy-xff

Conversation

@shiwng

@shiwng shiwng commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Closes #10.

sv_proxies "127.0.0.1, 10.0.0.5" names the proxies to trust, empty by default. From a trusted peer the client address is the right-most X-Forwarded-For entry that is not itself a listed proxy, so a chain of two proxies works; from anywhere else the header is ignored. Three socket-level tests and both docs pages cover it.

Shiwang and others added 3 commits November 25, 2026 19:46
sv_proxies lists the reverse proxies to trust, off by default. The client address is the right-most header entry that is not one of them.
sv_proxies lists the reverse proxies to trust, off by default. The client address is the right-most header entry that is not one of them.
Two ways a client behind a trusted proxy could still choose its own address:

- Only the first X-Forwarded-For line was read. A proxy that adds its own
  line instead of appending to the client's left the client's line first.
- A hop that did not parse was skipped, so a proxy entry with a port made
  the walk continue into the client's own entries.

All lines are now read in order, an entry with a port is read as its
address, and any other unparseable hop ends the walk at the proxy's
address. Also puts refusal()'s doc comment back on refusal() and runs
cargo fmt over the branch.

Co-Authored-By: Shiwang <shiwang@Shiwangs-MacBook-Air.local>
Co-Authored-By: Claude Mythos 5.2 <noreply@anthropic.com>
@CodingRule

Copy link
Copy Markdown
Owner

Thanks for this, it does what #10 asked and the tests and docs are exactly right. While reviewing I found two cases where a client behind a trusted proxy could still pick its own address, so I pushed one commit (a5ee2a6) onto your branch to close them rather than send you back for another round. The PR and the feature stay yours.

  • Two header lines. Only the first X-Forwarded-For line was read. A proxy that adds its own line instead of appending (HAProxy's option forwardfor does) leaves the client's line first, so a client sending X-Forwarded-For: 203.0.113.9 was recorded as ip:203.0.113.9. All lines are now read in order.
  • A hop that does not parse. Unparseable hops were skipped, so with 203.0.113.9, 198.51.100.7:51234 the walk went past the proxy's entry into the client's. An entry with a port is now read as its address, and anything else ends the walk at the proxy's address.

Also in the commit: three tests for those cases, a paragraph in docs/hosting.md, the doc comment for refusal() moved back onto it, and cargo fmt (CI's format check would have failed on a few long lines).

Locally on Windows: cargo fmt --all --check, cargo clippy --workspace --all-targets --locked -- -D warnings and cargo test --workspace --locked all pass. If you disagree with any of it, say so here.

@CodingRule
CodingRule merged commit 827be30 into CodingRule:main Nov 26, 2026
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.

Option to trust X-Forwarded-For behind a reverse proxy

2 participants