-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(execute): block cross-origin session-authenticated workflow runs #5062
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
+121
−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,53 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import type { NextRequest } from 'next/server' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { isCrossOriginSessionRequest } from '@/lib/core/security/same-origin' | ||
| import { getBaseUrl } from '@/lib/core/utils/urls' | ||
|
|
||
| function makeRequest(headers: Record<string, string>): NextRequest { | ||
| return { headers: new Headers(headers) } as unknown as NextRequest | ||
| } | ||
|
|
||
| describe('isCrossOriginSessionRequest', () => { | ||
| it('allows a same-origin browser fetch', () => { | ||
| expect(isCrossOriginSessionRequest(makeRequest({ 'sec-fetch-site': 'same-origin' }))).toBe( | ||
| false | ||
| ) | ||
| }) | ||
|
|
||
| it('rejects same-site requests (sibling subdomains are not our origin)', () => { | ||
| expect(isCrossOriginSessionRequest(makeRequest({ 'sec-fetch-site': 'same-site' }))).toBe(true) | ||
| }) | ||
|
|
||
| it('rejects cross-site requests', () => { | ||
| expect(isCrossOriginSessionRequest(makeRequest({ 'sec-fetch-site': 'cross-site' }))).toBe(true) | ||
| }) | ||
|
|
||
| it('rejects navigations not initiated from our front-end', () => { | ||
| expect(isCrossOriginSessionRequest(makeRequest({ 'sec-fetch-site': 'none' }))).toBe(true) | ||
| }) | ||
|
|
||
| it('falls back to the Origin header when Sec-Fetch-Site is absent (same-origin allowed)', () => { | ||
| const origin = new URL(getBaseUrl()).origin | ||
| expect(isCrossOriginSessionRequest(makeRequest({ origin }))).toBe(false) | ||
| }) | ||
|
|
||
| it('rejects a foreign Origin when Sec-Fetch-Site is absent', () => { | ||
| expect(isCrossOriginSessionRequest(makeRequest({ origin: 'https://evil.example.com' }))).toBe( | ||
| true | ||
| ) | ||
| }) | ||
|
|
||
| it('allows requests where the origin cannot be determined (no Sec-Fetch-Site, no Origin)', () => { | ||
| expect(isCrossOriginSessionRequest(makeRequest({}))).toBe(false) | ||
| }) | ||
|
|
||
| it('trusts the unforgeable Sec-Fetch-Site over a spoofed same-origin Origin', () => { | ||
| const origin = new URL(getBaseUrl()).origin | ||
| expect( | ||
| isCrossOriginSessionRequest(makeRequest({ 'sec-fetch-site': 'cross-site', origin })) | ||
| ).toBe(true) | ||
| }) | ||
| }) |
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,36 @@ | ||
| import type { NextRequest } from 'next/server' | ||
| import { isSameOrigin } from '@/lib/core/utils/validation' | ||
|
|
||
| /** | ||
| * Returns true when a request is provably cross-origin — a browser fetch driven | ||
| * from a different site than our own. Used to reject session-cookie CSRF on | ||
| * state-changing routes: a cross-site browser request always carries | ||
| * `Sec-Fetch-Site: cross-site` or a mismatched `Origin`, and neither header can | ||
| * be set by in-browser attacker JavaScript (both are forbidden headers). | ||
| * | ||
| * `Sec-Fetch-Site` is the primary signal; only `same-origin` is treated as our | ||
| * own front-end. The app is single-origin, so `same-site` (sibling subdomains), | ||
| * `cross-site`, and `none` are all rejected. When it is absent, fall back to an | ||
| * `Origin` same-origin check. When neither header is present the origin cannot | ||
| * be determined, so the request is allowed — a genuine cross-site browser attack | ||
| * cannot omit these headers. | ||
| * | ||
| * This is CSRF protection only. It does not defend against a non-browser client | ||
| * that forges headers directly (no header-based check can); that surface is | ||
| * covered by the credit and execution rate-limit gates. | ||
| */ | ||
| export function isCrossOriginSessionRequest(req: NextRequest): boolean { | ||
| const secFetchSite = req.headers.get('sec-fetch-site') | ||
| if (secFetchSite) { | ||
| return secFetchSite !== 'same-origin' | ||
| } | ||
|
|
||
| const origin = req.headers.get('origin') | ||
| if (!origin) return false | ||
|
|
||
| try { | ||
| return !isSameOrigin(origin) | ||
| } catch { | ||
| return false | ||
| } | ||
| } | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
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.