Skip to content

fix: trust one proxy hop by default instead of every X-Forwarded-* value - #808

Open
cybrdude wants to merge 4 commits into
GoogleCloudPlatform:mainfrom
cybrdude:trust-proxy-hops
Open

cybrdude wants to merge 4 commits into
GoogleCloudPlatform:mainfrom
cybrdude:trust-proxy-hops

Conversation

@cybrdude

Copy link
Copy Markdown

Fixes #807

getServer() has called app.enable('trust proxy') since the first commit. In Express that is trust proxy: true: every entry of X-Forwarded-For is trusted, so req.ip becomes the left-most entry, which is whatever the client put in the header before the request reached Cloud Run. req.hostname and req.protocol follow X-Forwarded-Host and X-Forwarded-Proto the same way. Function code that keys rate limits, allowlists, geo decisions or audit logs on req.ip therefore keys them on a client-chosen value.

Cloud Run places one proxy in front of the function and appends the address it accepted the connection from as the last X-Forwarded-For entry, so one trusted hop is the setting that yields that address (the same value GoogleCloudPlatform/nodejs-docs-samples#3583 settled on for the Cloud Run samples).

This change:

  • adds a --trust-proxy flag / TRUST_PROXY environment variable (FrameworkOptions.trustProxy) that accepts a hop count, true, false, or a comma-separated list of trusted addresses or subnets, and passes it to Express as the trust proxy setting;
  • defaults it to 1;
  • documents the option in the README options table, including when to raise it (another proxy or load balancer in front of Cloud Run).

Behavior change: deployments behind an additional proxy layer that relied on true to reach the left-most entry now see the last entry; they can set TRUST_PROXY=2 (or a hop count matching their topology) or TRUST_PROXY=true to keep the previous behavior.

Tests: test/options.ts covers the default, CLI-over-env precedence, an address list and false; test/integration/http.ts adds a proxy headers block asserting that with the default only the platform-added entry is reported, and that trustProxy: true reproduces the old behavior. npm run build, npx mocha build/test --recursive (111 passing) and gts check are clean on Node 22.

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 is enabled for every hop, so req.ip, req.hostname and req.protocol follow client-supplied X-Forwarded-* values

1 participant