-
Notifications
You must be signed in to change notification settings - Fork 122
fix: remove unsafe redirect_to from url #273
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
Open
LucasXu0
wants to merge
3
commits into
main
Choose a base branch
from
fix/redirect_to
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+243
−5
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,178 @@ | ||
| import { afterAuth, isSafeRedirectUrl, safeDecodeRedirectParam } from '../sign_in'; | ||
|
|
||
| // Mock localStorage | ||
| const localStorageMock = (() => { | ||
| let store: Record<string, string> = {}; | ||
| return { | ||
| getItem: (key: string) => store[key] ?? null, | ||
| setItem: (key: string, value: string) => { | ||
| store[key] = value; | ||
| }, | ||
| removeItem: (key: string) => { | ||
| delete store[key]; | ||
| }, | ||
| clear: () => { | ||
| store = {}; | ||
| }, | ||
| }; | ||
| })(); | ||
|
|
||
| Object.defineProperty(window, 'localStorage', { value: localStorageMock }); | ||
|
|
||
| // Mock window.location (jsdom blocks direct href assignment) | ||
| let hrefValue = 'http://localhost/login'; | ||
| Object.defineProperty(window, 'location', { | ||
| writable: true, | ||
| value: { | ||
| get href() { | ||
| return hrefValue; | ||
| }, | ||
| set href(v: string) { | ||
| hrefValue = v; | ||
| }, | ||
| origin: 'http://localhost', | ||
| }, | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| localStorageMock.clear(); | ||
| hrefValue = 'http://localhost/login'; | ||
| }); | ||
|
|
||
| describe('isSafeRedirectUrl', () => { | ||
| describe('safe URLs', () => { | ||
| it('returns true for a simple relative path', () => { | ||
| expect(isSafeRedirectUrl('/app')).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for a relative path with query string', () => { | ||
| expect(isSafeRedirectUrl('/app?tab=settings')).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for a relative path with nested segments', () => { | ||
| expect(isSafeRedirectUrl('/workspace/settings')).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for an absolute URL matching window.location.origin', () => { | ||
| expect(isSafeRedirectUrl('http://localhost/app')).toBe(true); | ||
| }); | ||
|
|
||
| it('returns true for an absolute URL with the same origin but a deep path', () => { | ||
| expect(isSafeRedirectUrl('http://localhost/workspace/abc/view/def')).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('unsafe URLs', () => { | ||
| it('returns false for an absolute URL with a different origin', () => { | ||
| expect(isSafeRedirectUrl('https://evil.com')).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for an absolute URL with a different subdomain', () => { | ||
| expect(isSafeRedirectUrl('https://phishing.appflowy.com')).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for a protocol-relative URL', () => { | ||
| expect(isSafeRedirectUrl('//evil.com')).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for a javascript: URL', () => { | ||
| expect(isSafeRedirectUrl('javascript:alert(1)')).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for a data: URL', () => { | ||
| expect(isSafeRedirectUrl('data:text/html,<script>alert(1)</script>')).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for an empty string', () => { | ||
| expect(isSafeRedirectUrl('')).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for a malformed string', () => { | ||
| expect(isSafeRedirectUrl('not a url at all %%')).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for an http URL on a different host', () => { | ||
| expect(isSafeRedirectUrl('http://attacker.com/app')).toBe(false); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('afterAuth', () => { | ||
| it('redirects to /app when no redirectTo is stored', () => { | ||
| afterAuth(); | ||
| expect(window.location.href).toBe('/app'); | ||
| }); | ||
|
|
||
| it('redirects to /app when stored redirectTo is an external URL', () => { | ||
| localStorage.setItem('redirectTo', encodeURIComponent('https://evil.com')); | ||
| afterAuth(); | ||
| expect(window.location.href).toBe('/app'); | ||
| }); | ||
|
|
||
| it('redirects to /app when stored redirectTo is a protocol-relative URL', () => { | ||
| localStorage.setItem('redirectTo', encodeURIComponent('//evil.com/attack')); | ||
| afterAuth(); | ||
| expect(window.location.href).toBe('/app'); | ||
| }); | ||
|
|
||
| it('redirects to /app when stored redirectTo contains a UUID path', () => { | ||
| const uuidPath = 'http://localhost/app/550e8400-e29b-41d4-a716-446655440000'; | ||
| localStorage.setItem('redirectTo', encodeURIComponent(uuidPath)); | ||
| afterAuth(); | ||
| expect(window.location.href).toBe('/app'); | ||
| }); | ||
|
|
||
| it('redirects to /app path while preserving query params for root path', () => { | ||
| localStorage.setItem('redirectTo', encodeURIComponent('http://localhost/?foo=bar')); | ||
| afterAuth(); | ||
| expect(window.location.href).toBe('http://localhost/app?foo=bar'); | ||
| }); | ||
|
|
||
| it('redirects to /app when stored redirectTo has malformed encoding', () => { | ||
| localStorage.setItem('redirectTo', '%zz'); | ||
| afterAuth(); | ||
| expect(window.location.href).toBe('/app'); | ||
| }); | ||
|
|
||
| it('follows a safe relative path', () => { | ||
| localStorage.setItem('redirectTo', encodeURIComponent('/settings')); | ||
| afterAuth(); | ||
| expect(window.location.href).toBe('/settings'); | ||
| }); | ||
|
|
||
| it('follows a safe absolute same-origin URL', () => { | ||
| localStorage.setItem('redirectTo', encodeURIComponent('http://localhost/settings')); | ||
| afterAuth(); | ||
| expect(window.location.href).toBe('http://localhost/settings'); | ||
| }); | ||
|
|
||
| it('clears localStorage after execution regardless of outcome', () => { | ||
| localStorage.setItem('redirectTo', encodeURIComponent('https://evil.com')); | ||
| afterAuth(); | ||
| expect(localStorage.getItem('redirectTo')).toBeNull(); | ||
| }); | ||
|
|
||
| it('clears localStorage even for a safe redirect', () => { | ||
| localStorage.setItem('redirectTo', encodeURIComponent('/settings')); | ||
| afterAuth(); | ||
| expect(localStorage.getItem('redirectTo')).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('safeDecodeRedirectParam', () => { | ||
| it('decodes a valid percent-encoded string', () => { | ||
| expect(safeDecodeRedirectParam('https%3A%2F%2Fevil.com')).toBe('https://evil.com'); | ||
| }); | ||
|
|
||
| it('returns the string unchanged when nothing is encoded', () => { | ||
| expect(safeDecodeRedirectParam('/settings')).toBe('/settings'); | ||
| }); | ||
|
|
||
| it('returns null for a malformed percent-encoded sequence', () => { | ||
| expect(safeDecodeRedirectParam('%zz')).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns null for a lone percent sign', () => { | ||
| expect(safeDecodeRedirectParam('%')).toBeNull(); | ||
| }); | ||
| }); |
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
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.