Skip to content

fix: resolve x-forwarded-* hop-aware, right-to-left - #229

Merged
pi0 merged 1 commit into
mainfrom
fix/trust-proxy-hop-aware
Jul 15, 2026
Merged

fix: resolve x-forwarded-* hop-aware, right-to-left#229
pi0 merged 1 commit into
mainfrom
fix/trust-proxy-hop-aware

Conversation

@pi0x

@pi0x pi0x commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

The X-Forwarded-* headers were resolved leftmost-first, so under the recommended trustProxy: "loopback" an attacker sending X-Forwarded-For: 9.9.9.9 got request.ip === "9.9.9.9". Because real proxies append to the header, the leftmost value is fully client-controlled — this defeats IP rate-limits, allowlists, geo checks and audit logs (F7). The same class of issue let forwarded proto and host be poisoned (cache keys, password-reset links).

New hop-aware model (D1)

Resolve the forwarded chain hop-aware, right-to-left from the immediate peer, skipping addresses in the trusted set — the first untrusted address is the client. This matches Express (trust proxy), nginx ($proxy_add_x_forwarded_for) and AWS ALB.

  • The client is the rightmost address not in the trusted set.
  • If every hop is trusted, the leftmost entry is the client.
  • If the immediate peer is untrusted, the forwarded headers are ignored entirely.
  • The same model gates forwarded proto/host: only the value contributed by the trusted proxy is honored, so a spoofed prefix cannot poison request.url.

Changes

  • Replace firstForwardedValue with forwardedList / resolveClientIP / trustedHops / forwardedHopValue in src/_trust-proxy.ts.
  • Apply at all three call sites: universal plugin, Node adapter (_node/request.ts + _node/url.ts), and AWS adapter (_aws/utils.ts, starting from the gateway-verified sourceIp as the nearest hop).
  • Rewrite the tests that enshrined the old leftmost behavior and add hop-aware cases (spoofed-prefix, all-trusted, untrusted-peer, proto/host poisoning).
  • Rewrite the trustProxy docs to state the hop model explicitly (Docs-T1).

Verification

  • pnpm vitest run --exclude '**/{deno,bun}*.test.ts' test/ — 860 passed, 32 skipped
  • pnpm lint and pnpm typecheck clean

🤖 Generated with Claude Code

`X-Forwarded-*` were resolved leftmost-first, so under a recommended
`trustProxy: "loopback"` an attacker sending `X-Forwarded-For: 9.9.9.9`
got `request.ip === "9.9.9.9"`, defeating IP rate-limits, allowlists,
geo checks and audit logs. The same class of issue let forwarded proto
and host be poisoned (cache keys, password-reset links).

Resolve the forwarded chain hop-aware, walking right-to-left from the
immediate peer and skipping addresses in the trusted set; the first
untrusted address is the client (Express / nginx / ALB semantics). If
every hop is trusted, the leftmost entry is the client; if the peer is
untrusted the headers are ignored entirely. The same hop model gates
forwarded proto/host: only the trusted proxy's entry is honored.

Applied at all three call sites (universal plugin, Node adapter, AWS
adapter — starting from the gateway-verified `sourceIp`). Replaces
`firstForwardedValue` with `forwardedList` / `resolveClientIP` /
`trustedHops` / `forwardedHopValue`. Rewrites the tests that enshrined
the old leftmost behavior and documents the hop model. (F7 / D1)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@pi0x
pi0x requested a review from pi0 as a code owner July 14, 2026 11:27
@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@pi0x, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 49 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 146cba7a-d25d-4b4e-a0bb-3ca9a0880a96

📥 Commits

Reviewing files that changed from the base of the PR and between f2edab1 and e9bcf32.

📒 Files selected for processing (8)
  • docs/1.guide/5.options.md
  • src/_trust-proxy.ts
  • src/adapters/_aws/utils.ts
  • src/adapters/_node/request.ts
  • src/adapters/_node/url.ts
  • test/aws.test.ts
  • test/node-trust-proxy.test.ts
  • test/trust-proxy-plugin.test.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/trust-proxy-hop-aware

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Jul 14, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/srvx@229

commit: e9bcf32

@pi0 pi0 changed the title fix(security): resolve X-Forwarded-* hop-aware, right-to-left fix: resolve X-Forwarded-* hop-aware, right-to-left Jul 14, 2026
pi0x

This comment was marked as outdated.

@pi0 pi0 changed the title fix: resolve X-Forwarded-* hop-aware, right-to-left fix: resolve x-forwarded-* hop-aware, right-to-left Jul 15, 2026
@pi0
pi0 merged commit 8d2d36c into main Jul 15, 2026
15 checks passed
@pi0
pi0 deleted the fix/trust-proxy-hop-aware branch July 15, 2026 08:54
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.

2 participants