-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(desktop): stop the OAuth connect callback from failing on a bare-path callback URL #7005
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fbd094d
fix(desktop): stop the OAuth connect callback from failing on a bare-…
icecrasher321 d9b50f1
fix(desktop): compose the connect completion URL through the URL API
icecrasher321 5e8e10b
fix(urls): give base URLs the no-trailing-slash form their call sites…
icecrasher321 e57f284
chore(urls): stop claiming path-prefixed base URLs are supported
icecrasher321 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { mockGetSession, mockRedirect, baseUrl } = vi.hoisted(() => ({ | ||
| mockGetSession: vi.fn(), | ||
| mockRedirect: vi.fn((url: string) => { | ||
| throw new Error(`NEXT_REDIRECT:${url}`) | ||
| }), | ||
| /** Mutable so a test can give the deployment a trailing-slash base URL. */ | ||
| baseUrl: { value: 'https://sim.test' }, | ||
| })) | ||
|
|
||
| vi.mock('@/lib/auth', () => ({ | ||
| auth: { api: { getSession: mockGetSession } }, | ||
| getSession: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/auth/auth-client', () => ({ | ||
| client: { oauth2: { link: vi.fn() } }, | ||
| signOut: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/core/utils/urls', () => ({ | ||
| getBaseUrl: () => baseUrl.value, | ||
| })) | ||
|
|
||
| /** Keeps the landing-page barrel the real shell pulls in out of this graph. */ | ||
| vi.mock('@/app/desktop/components/desktop-handoff-shell', () => ({ | ||
| DesktopHandoffShell: () => null, | ||
| })) | ||
|
|
||
| vi.mock('next/navigation', () => ({ | ||
| redirect: mockRedirect, | ||
| })) | ||
|
|
||
| vi.mock('next/headers', () => ({ | ||
| headers: vi.fn(async () => new Headers()), | ||
| })) | ||
|
|
||
| import DesktopConnectPage from '@/app/desktop/connect/page' | ||
|
|
||
| const VALID_STATE = 'a'.repeat(32) | ||
| const PORT = '57979' | ||
|
|
||
| function pageProps(params: Record<string, string>) { | ||
| return { searchParams: Promise.resolve(params) } | ||
| } | ||
|
|
||
| async function renderPage(params: Record<string, string>) { | ||
| const result = (await DesktopConnectPage(pageProps(params))) as unknown as { | ||
| type: { name: string } | ||
| props: Record<string, unknown> | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| describe('DesktopConnectPage', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| baseUrl.value = 'https://sim.test' | ||
| mockGetSession.mockResolvedValue({ user: { id: 'user-1', email: 'user@example.com' } }) | ||
| }) | ||
|
|
||
| it('hands the launcher an absolute complete URL so the callback can read the draft back', async () => { | ||
| // Better Auth stores `callbackURL` verbatim, and the OAuth callback parses it | ||
| // with `new URL`. A bare path threw there, failing the whole callback with a | ||
| // 500 after the provider had already authorized. | ||
| const result = await renderPage({ | ||
| provider: 'google-email', | ||
| state: VALID_STATE, | ||
| port: PORT, | ||
| draftId: 'draft-1', | ||
| }) | ||
|
|
||
| expect(result.type.name).toBe('ConnectLauncher') | ||
| expect(result.props.providerId).toBe('google-email') | ||
|
|
||
| const completeUrl = new URL(result.props.completeUrl as string) | ||
| expect(completeUrl.origin).toBe('https://sim.test') | ||
| expect(completeUrl.pathname).toBe('/desktop/connect/complete') | ||
| expect(completeUrl.searchParams.get('state')).toBe(VALID_STATE) | ||
| expect(completeUrl.searchParams.get('port')).toBe(PORT) | ||
| expect(completeUrl.searchParams.get('credentialDraftId')).toBe('draft-1') | ||
| }) | ||
|
|
||
| it('keeps the complete URL absolute when no draft rides along', async () => { | ||
| const result = await renderPage({ | ||
| provider: 'google-email', | ||
| state: VALID_STATE, | ||
| port: PORT, | ||
| }) | ||
|
|
||
| expect(result.type.name).toBe('ConnectLauncher') | ||
| expect(() => new URL(result.props.completeUrl as string)).not.toThrow() | ||
| }) | ||
|
|
||
| it('keeps the completion route intact when the deployment base URL has a trailing slash', async () => { | ||
| // `//desktop/connect/complete` matches no route, so the provider result | ||
| // would never reach the loopback and the connect would hang. | ||
| baseUrl.value = 'https://sim.test/' | ||
|
|
||
| const launcher = await renderPage({ | ||
| provider: 'google-email', | ||
| state: VALID_STATE, | ||
| port: PORT, | ||
| }) | ||
| expect(new URL(launcher.props.completeUrl as string).pathname).toBe('/desktop/connect/complete') | ||
|
|
||
| await expect( | ||
| DesktopConnectPage( | ||
| pageProps({ | ||
| provider: 'google-email', | ||
| state: VALID_STATE, | ||
| port: PORT, | ||
| workspaceId: 'workspace-1', | ||
| }) | ||
| ) | ||
| ).rejects.toThrow('NEXT_REDIRECT:') | ||
| const callbackUrl = new URL(mockRedirect.mock.calls[0][0]).searchParams.get('callbackURL') | ||
| expect(new URL(callbackUrl as string).pathname).toBe('/desktop/connect/complete') | ||
| }) | ||
|
|
||
| it('sends a workspace-scoped connect to the authorize route with an absolute callback', async () => { | ||
| await expect( | ||
| DesktopConnectPage( | ||
| pageProps({ | ||
| provider: 'google-email', | ||
| state: VALID_STATE, | ||
| port: PORT, | ||
| workspaceId: 'workspace-1', | ||
| }) | ||
| ) | ||
| ).rejects.toThrow('NEXT_REDIRECT:') | ||
|
|
||
| const authorize = new URL(mockRedirect.mock.calls[0][0]) | ||
| expect(authorize.pathname).toBe('/api/auth/oauth2/authorize') | ||
| expect(authorize.searchParams.get('providerId')).toBe('google-email') | ||
| expect(authorize.searchParams.get('workspaceId')).toBe('workspace-1') | ||
| expect(authorize.searchParams.get('callbackURL')).toBe( | ||
| `https://sim.test/desktop/connect/complete?state=${VALID_STATE}&port=${PORT}` | ||
| ) | ||
| }) | ||
|
|
||
| it('rejects a malformed request without reading the session', async () => { | ||
| const invalid = [ | ||
| { provider: 'Google', state: VALID_STATE, port: PORT }, | ||
| { provider: 'google-email', state: 'short', port: PORT }, | ||
| { provider: 'google-email', state: VALID_STATE }, | ||
| { provider: 'google-email', state: VALID_STATE, port: PORT, draftId: 'bad draft' }, | ||
| ] | ||
|
|
||
| for (const params of invalid) { | ||
| const result = await renderPage(params) | ||
| expect(result.type.name).toBe('InvalidRequest') | ||
| } | ||
| expect(mockGetSession).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
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
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.