Skip to content

fix: accept same-origin requests behind a scheme-rewriting proxy - #511

Open
NgoQuocViet2001 wants to merge 1 commit into
agegr:mainfrom
NgoQuocViet2001:fix-proxy-rewritten-origin
Open

fix: accept same-origin requests behind a scheme-rewriting proxy#511
NgoQuocViet2001 wants to merge 1 commit into
agegr:mainfrom
NgoQuocViet2001:fix-proxy-rewritten-origin

Conversation

@NgoQuocViet2001

Copy link
Copy Markdown
Contributor

Summary

  • accept an Origin that differs from the request URL by scheme only, when a proxy is in front and the Origin authority equals the Host header
  • keep the exact-origin comparison for direct requests

Fixes #497

Why

A relay can report the external scheme in x-forwarded-proto while rewriting Origin onto the backend authority. Next.js derives https from the forwarded header, Origin still says http, and the two differ by scheme alone — so every POST is rejected with 403 Untrusted API request even though the request is same-origin. PI_WEB_ALLOWED_HOSTS can't help, because the Host the relay sends is already loopback.

Scope of the relaxation

The scheme is only ignored when x-forwarded-proto is present and the Origin authority matches Host. A cross-origin page cannot set Host, and isApiRequestHostAllowed() still runs first, so DNS rebinding and foreign origins are rejected exactly as before. Ports are still compared, so localhost:30142 does not pass for localhost:30141.

I went with the narrower of the options in the issue (B) rather than dropping the scheme comparison outright (A), since it leaves direct-connection behaviour untouched. Happy to switch to A if you prefer it — it would also cover the loopback-spelling case from #329.

Test plan

  • Ran: node --experimental-strip-types --test lib/request-security.test.mjs (13 passed, 3 new)
  • Ran: npm run lint
  • Checked: with the new branch reverted, the proxy case fails and the rest still pass
  • Checked: npm test failure count is unchanged from a clean 2a6e537 on this Windows checkout

A relay can report the external scheme in x-forwarded-proto while rewriting
Origin onto the backend authority. Next.js then derives https from the
forwarded header while Origin still says http, the two differ by scheme
alone, and every POST is rejected with 403 Untrusted API request.

Accept that pairing only when a proxy is in front and the Origin authority
still equals the Host header, which a cross-origin page cannot forge. Direct
requests keep the exact-origin comparison.

Fixes agegr#497
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.

403 "Untrusted API request" behind a proxy that rewrites the Origin scheme (Azure Dev Tunnels)

1 participant