-
Notifications
You must be signed in to change notification settings - Fork 459
fix(clerk-js): validate window navigation protocols and honor allowedRedirectProtocols #8961
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
jacekradko
wants to merge
1
commit into
main
Choose a base branch
from
jacek/validate-window-navigation-protocols
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.
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| '@clerk/clerk-js': patch | ||
| '@clerk/react': patch | ||
| '@clerk/shared': patch | ||
| '@clerk/ui': patch | ||
| --- | ||
|
|
||
| Fix missing redirect URL protocol validation for Clerk UI browser navigations, including the multi-session add-account flow. | ||
|
|
||
| Internal browser navigations now consistently honor configured redirect protocols and fail closed across mixed ClerkJS/UI bundle versions. |
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
124 changes: 124 additions & 0 deletions
124
packages/shared/src/internal/clerk-js/__tests__/windowNavigate.test.ts
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,124 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { ALLOWED_PROTOCOLS, CLERK_BEFORE_UNLOAD_EVENT, windowNavigate } from '../windowNavigate'; | ||
|
|
||
| describe('windowNavigate', () => { | ||
| let originalLocation: Location; | ||
| let hrefSetter: ReturnType<typeof vi.fn>; | ||
| let warnSpy: ReturnType<typeof vi.spyOn>; | ||
| let eventSpy: ReturnType<typeof vi.fn>; | ||
|
|
||
| beforeEach(() => { | ||
| originalLocation = window.location; | ||
| hrefSetter = vi.fn(); | ||
| Object.defineProperty(window, 'location', { | ||
| configurable: true, | ||
| value: new Proxy( | ||
| { href: 'https://example.com/' }, | ||
| { | ||
| set: (target, prop, value) => { | ||
| if (prop === 'href') { | ||
| hrefSetter(value); | ||
| (target as any).href = value; | ||
| return true; | ||
| } | ||
| (target as any)[prop] = value; | ||
| return true; | ||
| }, | ||
| }, | ||
| ), | ||
| }); | ||
| warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
| eventSpy = vi.fn(); | ||
| window.addEventListener(CLERK_BEFORE_UNLOAD_EVENT, eventSpy); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| window.removeEventListener(CLERK_BEFORE_UNLOAD_EVENT, eventSpy); | ||
| Object.defineProperty(window, 'location', { | ||
| configurable: true, | ||
| value: originalLocation, | ||
| }); | ||
| warnSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['absolute https URL', 'https://example.com/dashboard'], | ||
| ['absolute http URL', 'http://example.com/dashboard'], | ||
| ['relative path', '/sign-in'], | ||
| ['wails protocol', 'wails://app/route'], | ||
| ['chrome-extension protocol', 'chrome-extension://abc/route'], | ||
| ])('navigates to %s', (_label, to) => { | ||
| windowNavigate(to); | ||
| expect(hrefSetter).toHaveBeenCalledTimes(1); | ||
| expect(eventSpy).toHaveBeenCalledTimes(1); | ||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['javascript', 'javascript:alert(1)'], | ||
| ['data', 'data:text/html,<script>alert(1)</script>'], | ||
| ['file', 'file:///etc/passwd'], | ||
| ['vbscript', 'vbscript:msgbox(1)'], | ||
| ['mixed-case JavaScript', 'JavaScript:alert(1)'], | ||
| ['upper-case JAVASCRIPT', 'JAVASCRIPT:alert(1)'], | ||
| ['leading-whitespace javascript', ' javascript:alert(1)'], | ||
| ['leading-tab javascript', '\tjavascript:alert(1)'], | ||
| ['leading-newline javascript', '\njavascript:alert(1)'], | ||
| ])('blocks %s: protocol and does not navigate', (_label, to) => { | ||
| windowNavigate(to); | ||
| expect(hrefSetter).not.toHaveBeenCalled(); | ||
| expect(eventSpy).not.toHaveBeenCalled(); | ||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('blocks javascript: URLs that the URL parser normalizes via the base URL', () => { | ||
| windowNavigate('javascript:alert(location.origin)//'); | ||
| expect(hrefSetter).not.toHaveBeenCalled(); | ||
| expect(eventSpy).not.toHaveBeenCalled(); | ||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['scheme-relative //host', '//evil.example/path'], | ||
| ['scheme-relative ///host', '///evil.example/path'], | ||
| ['backslash \\\\host', '\\\\evil.example\\path'], | ||
| ['mixed /\\host', '/\\evil.example/path'], | ||
| ['mixed \\/host', '\\/evil.example/path'], | ||
| ['leading-whitespace scheme-relative', ' //evil.example/path'], | ||
| ['leading-tab scheme-relative', '\t//evil.example/path'], | ||
| ])('blocks %s and does not navigate', (_label, to) => { | ||
| windowNavigate(to); | ||
| expect(hrefSetter).not.toHaveBeenCalled(); | ||
| expect(eventSpy).not.toHaveBeenCalled(); | ||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('still rejects scheme-relative URLs when an extended allowlist is supplied', () => { | ||
| windowNavigate('//evil.example/path', { | ||
| allowedProtocols: [...ALLOWED_PROTOCOLS, 'slack:'], | ||
| }); | ||
| expect(hrefSetter).not.toHaveBeenCalled(); | ||
| expect(eventSpy).not.toHaveBeenCalled(); | ||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('honors a caller-supplied extended allowlist for custom protocols', () => { | ||
| windowNavigate('slack://channel/123', { | ||
| allowedProtocols: [...ALLOWED_PROTOCOLS, 'slack:'], | ||
| }); | ||
| expect(hrefSetter).toHaveBeenCalledTimes(1); | ||
| expect(hrefSetter).toHaveBeenCalledWith('slack://channel/123'); | ||
| expect(eventSpy).toHaveBeenCalledTimes(1); | ||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('still rejects disallowed protocols when an extended allowlist is supplied', () => { | ||
| windowNavigate('javascript:alert(1)', { | ||
| allowedProtocols: [...ALLOWED_PROTOCOLS, 'slack:'], | ||
| }); | ||
| expect(hrefSetter).not.toHaveBeenCalled(); | ||
| expect(eventSpy).not.toHaveBeenCalled(); | ||
| expect(warnSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.