-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Add origin checks for UI route submissions #14708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+174
−6
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1f88818
fix: additional layer of CSRF protection.
jacob-ebey 7ad6a13
make non optional but configurable
jacob-ebey 2975d14
pass null through to still check when oringinating from a sandboxed
jacob-ebey a0fd42c
simplify a bit
jacob-ebey a9862ac
update fixture
jacob-ebey 3ad9a11
update integration tests
jacob-ebey 7b01c8b
Update changelog and error message
brophdawg11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@react-router/dev": minor | ||
| "react-router": minor | ||
| --- | ||
|
|
||
| Add additional layer of CSRF protection by rejecting submissions to UI routes from external origins. If you need to permit access to specific external origins, you can specify them in the `react-router.config.ts` config `allowedActionOrigins` field. |
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| export function throwIfPotentialCSRFAttack( | ||
| headers: Headers, | ||
| allowedActionOrigins: string[] | undefined, | ||
| ) { | ||
| let originHeader = headers.get("origin"); | ||
| let originDomain = | ||
| typeof originHeader === "string" && originHeader !== "null" | ||
| ? new URL(originHeader).host | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, a coworker said he experienced exceptions from cases where Origin was not a valid URL. I'm not sure what cases this happen in, but it happened.
|
||
| : originHeader; | ||
| let host = parseHostHeader(headers); | ||
|
|
||
| if (originDomain && (!host || originDomain !== host.value)) { | ||
| if (!isAllowedOrigin(originDomain, allowedActionOrigins)) { | ||
| if (host) { | ||
| // This seems to be an CSRF attack. We should not proceed with the action. | ||
| throw new Error( | ||
| `${host.type} header does not match \`origin\` header from a forwarded ` + | ||
| `action request. Aborting the action.`, | ||
| ); | ||
| } else { | ||
| // This is an attack. We should not proceed with the action. | ||
| throw new Error( | ||
| "`x-forwarded-host` or `host` headers are not provided. One of these " + | ||
| "is needed to compare the `origin` header from a forwarded action " + | ||
| "request. Aborting the action.", | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Implementation of micromatch by Next.js https://github.com/vercel/next.js/blob/ea927b583d24f42e538001bf13370e38c91d17bf/packages/next/src/server/app-render/csrf-protection.ts#L6 | ||
| function matchWildcardDomain(domain: string, pattern: string) { | ||
| const domainParts = domain.split("."); | ||
| const patternParts = pattern.split("."); | ||
|
|
||
| if (patternParts.length < 1) { | ||
| // pattern is empty and therefore invalid to match against | ||
| return false; | ||
| } | ||
|
|
||
| if (domainParts.length < patternParts.length) { | ||
| // domain has too few segments and thus cannot match | ||
| return false; | ||
| } | ||
|
|
||
| // Prevent wildcards from matching entire domains (e.g. '**' or '*.com') | ||
| // This ensures wildcards can only match subdomains, not the main domain | ||
| if ( | ||
| patternParts.length === 1 && | ||
| (patternParts[0] === "*" || patternParts[0] === "**") | ||
| ) { | ||
| return false; | ||
| } | ||
|
|
||
| while (patternParts.length) { | ||
| const patternPart = patternParts.pop(); | ||
| const domainPart = domainParts.pop(); | ||
|
|
||
| switch (patternPart) { | ||
| case "": { | ||
| // invalid pattern. pattern segments must be non empty | ||
| return false; | ||
| } | ||
| case "*": { | ||
| // wildcard matches anything so we continue if the domain part is non-empty | ||
| if (domainPart) { | ||
| continue; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| case "**": { | ||
| // if this is not the last item in the pattern the pattern is invalid | ||
| if (patternParts.length > 0) { | ||
| return false; | ||
| } | ||
| // recursive wildcard matches anything so we terminate here if the domain part is non empty | ||
| return domainPart !== undefined; | ||
| } | ||
| case undefined: | ||
| default: { | ||
| if (domainPart !== patternPart) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // We exhausted the pattern. If we also exhausted the domain we have a match | ||
| return domainParts.length === 0; | ||
| } | ||
|
|
||
| function isAllowedOrigin( | ||
| originDomain: string, | ||
| allowedActionOrigins: string[] | undefined = [], | ||
| ) { | ||
| return allowedActionOrigins.some( | ||
| (allowedOrigin) => | ||
| allowedOrigin && | ||
| (allowedOrigin === originDomain || | ||
| matchWildcardDomain(originDomain, allowedOrigin)), | ||
| ); | ||
| } | ||
|
|
||
| function parseHostHeader(headers: Headers) { | ||
| let forwardedHostHeader = headers.get("x-forwarded-host"); | ||
| let forwardedHostValue = forwardedHostHeader?.split(",")[0]?.trim(); | ||
| let hostHeader = headers.get("host"); | ||
|
|
||
| return forwardedHostValue | ||
| ? { | ||
| type: "x-forwarded-host", | ||
| value: forwardedHostValue, | ||
| } | ||
| : hostHeader | ||
| ? { | ||
| type: "host", | ||
| value: hostHeader, | ||
| } | ||
| : undefined; | ||
| } | ||
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This documentation could be better.
The host header includes protocol (
https://), but the comparison happens without.The docs could state that origins should be provided without protocol.
Also, globs wont match if the origin is
"null":*,**andn*doesn't match.I'm not sure how to allow if the origin header is completely absent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can get the docs updated to be more specific there - how about "The allowed origins (excluding protocol) for actions / mutations on UI routes (i.e., those with a UI component). Micromatch glob patterns are supported."?
We can also look into the glob matching for
null- I would think that should be matched by *.If the header is absent, this check won't even happen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This caught me off-guard today too. Omitting the protocol from the origin feels like it might be a Bad Idea™, and I'm wondering if RR should be fairly strict about this considering it's meant to mitigate a security issue.