-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(execute): reject only cross-site session execution (CSRF guard) #5068
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
+106
−0
Merged
Changes from all commits
Commits
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
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,32 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import type { NextRequest } from 'next/server' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { isCrossSiteSessionRequest } from '@/lib/core/security/same-origin' | ||
|
|
||
| function makeRequest(headers: Record<string, string>): NextRequest { | ||
| return { headers: new Headers(headers) } as unknown as NextRequest | ||
| } | ||
|
|
||
| describe('isCrossSiteSessionRequest', () => { | ||
| it('rejects cross-site requests', () => { | ||
| expect(isCrossSiteSessionRequest(makeRequest({ 'sec-fetch-site': 'cross-site' }))).toBe(true) | ||
| }) | ||
|
|
||
| it('allows same-origin browser fetches', () => { | ||
| expect(isCrossSiteSessionRequest(makeRequest({ 'sec-fetch-site': 'same-origin' }))).toBe(false) | ||
| }) | ||
|
|
||
| it('allows same-site fetches (sibling subdomains, e.g. www.<domain> -> <domain>)', () => { | ||
| expect(isCrossSiteSessionRequest(makeRequest({ 'sec-fetch-site': 'same-site' }))).toBe(false) | ||
| }) | ||
|
|
||
| it('allows user-initiated requests (Sec-Fetch-Site: none)', () => { | ||
| expect(isCrossSiteSessionRequest(makeRequest({ 'sec-fetch-site': 'none' }))).toBe(false) | ||
| }) | ||
|
|
||
| it('allows requests with no Sec-Fetch-Site header (older clients)', () => { | ||
| expect(isCrossSiteSessionRequest(makeRequest({}))).toBe(false) | ||
| }) | ||
| }) |
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,23 @@ | ||
| import type { NextRequest } from 'next/server' | ||
|
|
||
| /** | ||
| * Returns true when a request is provably cross-site — a browser fetch driven | ||
| * from a different site than our own. Used to reject session-cookie CSRF on | ||
| * state-changing routes. | ||
| * | ||
| * `Sec-Fetch-Site` is browser-set and a forbidden header, so page JavaScript | ||
| * cannot forge it. A cross-site browser request (the CSRF threat) always reports | ||
| * `cross-site`. We deliberately accept `same-origin`, `same-site`, and `none`: | ||
| * the app is served across sibling subdomains (e.g. `www.<domain>` calling | ||
| * `<domain>`), so a legitimate `same-site` fetch must NOT be blocked — rejecting | ||
| * it 403s real "Run" requests on those origins. An absent header (older clients) | ||
| * is also allowed; the conventional CSRF posture is to reject only a provable | ||
| * cross-site request. | ||
| * | ||
| * This is CSRF protection only. It does not defend against a non-browser client | ||
| * that forges headers directly (no header check can); that surface is covered by | ||
| * the credit and execution rate-limit gates. | ||
| */ | ||
| export function isCrossSiteSessionRequest(req: NextRequest): boolean { | ||
| return req.headers.get('sec-fetch-site') === 'cross-site' | ||
| } |
Oops, something went wrong.
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.