-
Notifications
You must be signed in to change notification settings - Fork 394
fix(clerk-js): Always prefer oauth popup flow in an iframe #6455
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
+246
−3
Merged
Changes from all commits
Commits
Show all changes
4 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,5 @@ | ||
--- | ||
'@clerk/clerk-js': patch | ||
--- | ||
|
||
Fix iframe detetction and ensure we prefer the oauth popup flow when in an iframe. |
229 changes: 229 additions & 0 deletions
229
packages/clerk-js/src/ui/utils/__tests__/originPrefersPopup.spec.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,229 @@ | ||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
import { originPrefersPopup } from '../originPrefersPopup'; | ||
|
||
// Mock the inIframe function | ||
vi.mock('@/utils', () => ({ | ||
inIframe: vi.fn(), | ||
})); | ||
|
||
// Import the mocked function | ||
import { inIframe } from '@/utils'; | ||
const mockInIframe = vi.mocked(inIframe); | ||
|
||
describe('originPrefersPopup', () => { | ||
// Store original location to restore after tests | ||
const originalLocation = window.location; | ||
|
||
// Helper function to mock window.location.origin | ||
const mockLocationOrigin = (origin: string) => { | ||
Object.defineProperty(window, 'location', { | ||
value: { | ||
origin, | ||
}, | ||
writable: true, | ||
configurable: true, | ||
}); | ||
}; | ||
|
||
beforeEach(() => { | ||
// Reset all mocks before each test | ||
vi.clearAllMocks(); | ||
|
||
// Set default origin | ||
mockLocationOrigin('https://example.com'); | ||
}); | ||
|
||
afterEach(() => { | ||
// Restore original location | ||
Object.defineProperty(window, 'location', { | ||
value: originalLocation, | ||
writable: true, | ||
configurable: true, | ||
}); | ||
}); | ||
|
||
describe('when in iframe', () => { | ||
it('should return true regardless of origin', () => { | ||
mockInIframe.mockReturnValue(true); | ||
mockLocationOrigin('https://not-a-preferred-origin.com'); | ||
|
||
expect(originPrefersPopup()).toBe(true); | ||
}); | ||
|
||
it('should return true even with preferred origin', () => { | ||
mockInIframe.mockReturnValue(true); | ||
mockLocationOrigin('https://app.lovable.app'); | ||
|
||
expect(originPrefersPopup()).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('when not in iframe', () => { | ||
beforeEach(() => { | ||
mockInIframe.mockReturnValue(false); | ||
}); | ||
|
||
describe('with preferred origins', () => { | ||
it('should return true for .lovable.app domains', () => { | ||
mockLocationOrigin('https://app.lovable.app'); | ||
expect(originPrefersPopup()).toBe(true); | ||
|
||
mockLocationOrigin('https://my-project.lovable.app'); | ||
expect(originPrefersPopup()).toBe(true); | ||
}); | ||
|
||
it('should return true for .lovableproject.com domains', () => { | ||
mockLocationOrigin('https://project.lovableproject.com'); | ||
expect(originPrefersPopup()).toBe(true); | ||
|
||
mockLocationOrigin('https://demo.lovableproject.com'); | ||
expect(originPrefersPopup()).toBe(true); | ||
}); | ||
|
||
it('should return true for .webcontainer-api.io domains', () => { | ||
mockLocationOrigin('https://stackblitz.webcontainer-api.io'); | ||
expect(originPrefersPopup()).toBe(true); | ||
|
||
mockLocationOrigin('https://container.webcontainer-api.io'); | ||
expect(originPrefersPopup()).toBe(true); | ||
}); | ||
|
||
it('should return true for .vusercontent.net domains', () => { | ||
mockLocationOrigin('https://codesandbox.vusercontent.net'); | ||
expect(originPrefersPopup()).toBe(true); | ||
|
||
mockLocationOrigin('https://preview.vusercontent.net'); | ||
expect(originPrefersPopup()).toBe(true); | ||
}); | ||
|
||
it('should return true for .v0.dev domains', () => { | ||
mockLocationOrigin('https://preview.v0.dev'); | ||
expect(originPrefersPopup()).toBe(true); | ||
|
||
mockLocationOrigin('https://app.v0.dev'); | ||
expect(originPrefersPopup()).toBe(true); | ||
}); | ||
|
||
it('should handle HTTPS and HTTP protocols', () => { | ||
mockLocationOrigin('http://localhost.lovable.app'); | ||
expect(originPrefersPopup()).toBe(true); | ||
|
||
mockLocationOrigin('https://secure.v0.dev'); | ||
expect(originPrefersPopup()).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('with non-preferred origins', () => { | ||
it('should return false for common domains', () => { | ||
const nonPreferredOrigins = [ | ||
'https://example.com', | ||
'https://google.com', | ||
'https://github.com', | ||
'https://localhost:3000', | ||
'https://app.mycompany.com', | ||
'https://production-site.com', | ||
]; | ||
|
||
nonPreferredOrigins.forEach(origin => { | ||
mockLocationOrigin(origin); | ||
expect(originPrefersPopup()).toBe(false); | ||
}); | ||
}); | ||
|
||
it('should return false for similar but non-matching domains', () => { | ||
const similarOrigins = [ | ||
'https://lovable.app.com', // wrong order | ||
'https://notlovable.app', // different subdomain structure | ||
'https://lovableproject.org', // wrong TLD | ||
'https://webcontainer.io', // missing -api | ||
'https://vusercontent.com', // wrong TLD | ||
'https://v0.com', // missing .dev | ||
'https://v1.dev', // wrong subdomain | ||
]; | ||
|
||
similarOrigins.forEach(origin => { | ||
mockLocationOrigin(origin); | ||
expect(originPrefersPopup()).toBe(false); | ||
}); | ||
}); | ||
|
||
it('should return false for domains that contain preferred origins as substrings', () => { | ||
const containingOrigins = [ | ||
'https://not-lovable.app-something.com', | ||
'https://fake-webcontainer-api.io.malicious.com', | ||
'https://evil-vusercontent.net.phishing.com', | ||
]; | ||
|
||
containingOrigins.forEach(origin => { | ||
mockLocationOrigin(origin); | ||
expect(originPrefersPopup()).toBe(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('edge cases', () => { | ||
it('should handle empty origin', () => { | ||
mockLocationOrigin(''); | ||
expect(originPrefersPopup()).toBe(false); | ||
}); | ||
|
||
it('should be case sensitive', () => { | ||
mockLocationOrigin('https://app.LOVABLE.APP'); | ||
expect(originPrefersPopup()).toBe(false); | ||
|
||
mockLocationOrigin('https://APP.V0.DEV'); | ||
expect(originPrefersPopup()).toBe(false); | ||
}); | ||
|
||
it('should handle malformed origins gracefully', () => { | ||
// These shouldn't normally happen, but we should handle them gracefully | ||
mockLocationOrigin('not-a-url'); | ||
expect(originPrefersPopup()).toBe(false); | ||
|
||
mockLocationOrigin('file://'); | ||
expect(originPrefersPopup()).toBe(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('integration scenarios', () => { | ||
it('should prioritize iframe detection over origin matching', () => { | ||
mockInIframe.mockReturnValue(true); | ||
mockLocationOrigin('https://definitely-not-preferred.com'); | ||
|
||
expect(originPrefersPopup()).toBe(true); | ||
expect(mockInIframe).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
it('should call inIframe function', () => { | ||
mockInIframe.mockReturnValue(false); | ||
mockLocationOrigin('https://example.com'); | ||
|
||
originPrefersPopup(); | ||
|
||
expect(mockInIframe).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
it('should work with real-world scenarios', () => { | ||
// Scenario 1: Developer working in CodeSandbox | ||
mockInIframe.mockReturnValue(false); | ||
mockLocationOrigin('https://csb-123abc.vusercontent.net'); | ||
expect(originPrefersPopup()).toBe(true); | ||
|
||
// Scenario 2: Developer working in StackBlitz | ||
mockLocationOrigin('https://stackblitz.webcontainer-api.io'); | ||
expect(originPrefersPopup()).toBe(true); | ||
|
||
// Scenario 3: App embedded in iframe on regular domain | ||
mockInIframe.mockReturnValue(true); | ||
mockLocationOrigin('https://myapp.com'); | ||
expect(originPrefersPopup()).toBe(true); | ||
|
||
// Scenario 4: Regular production app | ||
mockInIframe.mockReturnValue(false); | ||
mockLocationOrigin('https://myapp.com'); | ||
expect(originPrefersPopup()).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
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.