Skip to content

test: cover function-based "trust proxy" hop behavior#7387

Open
BIGSUS24 wants to merge 1 commit into
expressjs:masterfrom
BIGSUS24:fix/document-trust-proxy-function-hop-behavior
Open

test: cover function-based "trust proxy" hop behavior#7387
BIGSUS24 wants to merge 1 commit into
expressjs:masterfrom
BIGSUS24:fix/document-trust-proxy-function-hop-behavior

Conversation

@BIGSUS24

Copy link
Copy Markdown

Closes #6541.

trust proxy as a function is undocumented and had zero test coverage, which is exactly why #6541 got filed as a bug when it isn't one. req.ip calls the function once per forwarded hop via proxyaddr, fn(addr, i), starting from the socket address (i === 0) and moving outward through X-Forwarded-For, stopping at the first hop that returns false. That's the intended behavior per proxy-addr, just never pinned down anywhere.

@dougwilson said in the issue thread that undocumented/untested behavior like this is exactly what he wants contributors to lock down, so here's that:

  • test asserting the exact (addr, i) call sequence
  • test asserting req.ip resolves to the first untrusted hop
  • test asserting the function isn't called when there's no X-Forwarded-For
  • short doc comment above the ip getter describing the callback contract

No production logic changed, the getter itself is untouched. Ran the full suite locally (1263 passing) plus lint, both clean.

Closes expressjs#6541. When "trust proxy" is set to a function, req.ip calls
it once per forwarded hop as fn(addr, i) starting from the socket
address, stopping at the first hop it doesn't trust. This was
undocumented and untested, which was the actual bug in expressjs#6541 (the
reported call pattern was working as intended, just surprising).

Adds test/req.ip.js coverage for the call sequence, the resolved
address, and the no-XFF-header case, plus a short doc comment on the
ip getter.
Copilot AI review requested due to automatic review settings July 20, 2026 17:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.

"trust proxy" callback called multiple times on single request

2 participants