-
Notifications
You must be signed in to change notification settings - Fork 0
fix(auth): harden post-login redirect validation #125
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
Merged
Changes from 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| import { redirect } from '@sveltejs/kit'; | ||
| import type { PageServerLoad } from './$types'; | ||
| import { safeRedirect } from './safe-redirect'; | ||
|
|
||
| export const load: PageServerLoad = async ({ locals, url }) => { | ||
| if (locals.session?.userType === 'org') { | ||
| redirect(303, url.searchParams.get('redirect') ?? '/portal/dashboard'); | ||
| redirect(303, safeRedirect(url.searchParams.get('redirect'))); | ||
| } | ||
| }; |
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,22 @@ | ||
| // The post-login `redirect` query param is attacker-controlled, so it must | ||
| // never be handed to `redirect()`/`goto()` without validation — otherwise it | ||
| // is an open redirect (an attacker can send `?redirect=https://evil.example` | ||
| // or the protocol-relative `?redirect=//evil.example` to bounce a logged-in | ||
| // user off-site). Only same-origin, path-absolute targets are allowed. | ||
|
|
||
| export const DEFAULT_REDIRECT = '/portal/dashboard'; | ||
|
|
||
| /** | ||
| * Return a safe post-login redirect target. A value is only accepted when it | ||
| * begins with a single `/` (a same-origin absolute path). Everything else — | ||
| * empty/missing values, absolute URLs (`https://…`), protocol-relative URLs | ||
| * (`//host`) and the backslash variant browsers normalise to them (`/\host`) | ||
| * — falls back to {@link DEFAULT_REDIRECT}. | ||
| */ | ||
| export function safeRedirect(target: string | null | undefined): string { | ||
| if (!target || !target.startsWith('/')) return DEFAULT_REDIRECT; | ||
| // Reject protocol-relative URLs. Browsers treat `\` as `/`, so `/\host` | ||
| // escapes the origin just like `//host` does. | ||
| if (target[1] === '/' || target[1] === '\\') return DEFAULT_REDIRECT; | ||
| return target; | ||
| } | ||
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,35 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { safeRedirect, DEFAULT_REDIRECT } from '../../src/routes/auth/login/safe-redirect'; | ||
|
|
||
| describe('safeRedirect', () => { | ||
| it('accepts same-origin absolute paths', () => { | ||
| expect(safeRedirect('/portal/dashboard')).toBe('/portal/dashboard'); | ||
| expect(safeRedirect('/portal/settings')).toBe('/portal/settings'); | ||
| expect(safeRedirect('/')).toBe('/'); | ||
| expect(safeRedirect('/a?b=c#d')).toBe('/a?b=c#d'); | ||
| }); | ||
|
|
||
| it('falls back for missing values', () => { | ||
| expect(safeRedirect(null)).toBe(DEFAULT_REDIRECT); | ||
| expect(safeRedirect(undefined)).toBe(DEFAULT_REDIRECT); | ||
| expect(safeRedirect('')).toBe(DEFAULT_REDIRECT); | ||
| }); | ||
|
|
||
| it('rejects values that do not start with a slash', () => { | ||
| expect(safeRedirect('portal/dashboard')).toBe(DEFAULT_REDIRECT); | ||
| expect(safeRedirect('https://evil.example')).toBe(DEFAULT_REDIRECT); | ||
| expect(safeRedirect('http://evil.example/path')).toBe(DEFAULT_REDIRECT); | ||
| expect(safeRedirect('javascript:alert(1)')).toBe(DEFAULT_REDIRECT); | ||
| expect(safeRedirect(' /portal/dashboard')).toBe(DEFAULT_REDIRECT); | ||
| }); | ||
|
|
||
| it('rejects protocol-relative URLs', () => { | ||
| expect(safeRedirect('//evil.example')).toBe(DEFAULT_REDIRECT); | ||
| expect(safeRedirect('//evil.example/path')).toBe(DEFAULT_REDIRECT); | ||
| }); | ||
|
|
||
| it('rejects the backslash variant browsers normalise to protocol-relative', () => { | ||
| expect(safeRedirect('/\\evil.example')).toBe(DEFAULT_REDIRECT); | ||
| expect(safeRedirect('/\\/evil.example')).toBe(DEFAULT_REDIRECT); | ||
| }); | ||
| }); |
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.