Repository navigation
fix(fastify): detect proxy trust from request.ip, not trustProxy - #210
Merged
Merged
Conversation
The check that refused to forward a client-controlled address under trustProxy: true read trustProxy from initialConfig, which Fastify 5 does not expose, so it never fired and a caller could choose the client IP the auth API rate limits and audits against. It now asks Fastify's own request.ip about a request from documentation addresses (RFC 5737), and drops the address and warns when every address is trusted. The README recommended a hop count, which Fastify 5.12.1 and later ignore, leaving every user behind the proxy on one address. It now recommends the proxy's address or subnet, and the adapter warns once when requests carry X-Forwarded-For that nothing trusts. Closes #208.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #208.
Why
#208 reported that the README recommends a
trustProxyhop count, which Fastify 5.12.1 and later ignore. Fixing it turned up a second, more serious problem.The blanket-
trustProxyguard never fired. The adapter is meant to refuse to forward the client IP when the app trusts every proxy (trustProxy: true). With that setting,request.ipis the leftmostX-Forwarded-Forentry, which any client can set. The check readtrustProxyfromrequest.server.initialConfig, but Fastify 5 does not puttrustProxythere; it lives only in a closure. So the check was always false. Any Fastify adopter usingtrustProxy: true, a common setting, let callers choose the client IP the auth API rate limits and audits against.The hop-count advice.
getTrustProxyFnchanged in Fastify 5.12.1 so that a numerictrustProxytrusts nothing.request.ipis then the proxy, and every user behind it shares one rate-limit bucket and one audit address on the auth API.What changes
trustProxycannot be read, the adapter asks Fastify's ownrequest.ipgetter about a synthetic request: a peer and forwarded addresses from the RFC 5737 documentation ranges, which no real proxy has. If Fastify takes the leftmost forwarded address, the app trusts every address. The adapter then drops the client IP and warns once, as originally intended. The answer is cached per request class. If the getter cannot be found, behaviour falls back to forwardingrequest.ip, as before.X-Forwarded-Forbutrequest.ipis still the connecting peer, the adapter warns once that a proxy appears to be in front and nothing trusts it.resolveClientIp, and explains both failure modes.I tried a startup warning first, but it cannot work for the same reason:
trustProxyis not readable.The probe gives the expected answer on Fastify 5.10.0 (the workspace) and 5.12.5 (latest):
trustProxyrequest.ipfor peer10.0.0.5,X-Forwarded-For: 6.6.6.6, 203.0.113.5010.0.0.5true6.6.6.61(5.12.5)10.0.0.5"10.0.0.0/8"203.0.113.50(a, hop) => hop < 1203.0.113.50() => true6.6.6.6Checks
pnpm test: core 403, express 210, fastify 166, nextjs 165, all passing.tests/trustProxy.test.jssends requests through the plugin and asserts thex-seamless-client-ipsent upstream:trueand a catch-all function;X-Forwarded-Forarrives untrusted;Patch changeset for
@seamless-auth/fastify.