fix: accept same-origin requests behind a scheme-rewriting proxy - #511
Open
NgoQuocViet2001 wants to merge 1 commit into
Open
fix: accept same-origin requests behind a scheme-rewriting proxy#511NgoQuocViet2001 wants to merge 1 commit into
NgoQuocViet2001 wants to merge 1 commit into
Conversation
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
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.
Summary
Originthat differs from the request URL by scheme only, when a proxy is in front and theOriginauthority equals theHostheaderFixes #497
Why
A relay can report the external scheme in
x-forwarded-protowhile rewritingOriginonto the backend authority. Next.js deriveshttpsfrom the forwarded header,Originstill sayshttp, and the two differ by scheme alone — so every POST is rejected with403 Untrusted API requesteven though the request is same-origin.PI_WEB_ALLOWED_HOSTScan't help, because theHostthe relay sends is already loopback.Scope of the relaxation
The scheme is only ignored when
x-forwarded-protois present and theOriginauthority matchesHost. A cross-origin page cannot setHost, andisApiRequestHostAllowed()still runs first, so DNS rebinding and foreign origins are rejected exactly as before. Ports are still compared, solocalhost:30142does not pass forlocalhost: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
node --experimental-strip-types --test lib/request-security.test.mjs(13 passed, 3 new)npm run lintnpm testfailure count is unchanged from a clean2a6e537on this Windows checkout