-
Notifications
You must be signed in to change notification settings - Fork 441
fix(clerk-js): keep dev browser token in memory to prevent stale partitioned cookie reads #8161
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
+103
−2
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5f859be
fix(clerk-js): keep dev browser token in memory to prevent stale part…
brkalow 8bb6bbb
revert(clerk-js): undo unit test changes for devBrowser
brkalow 88409f4
fix(clerk-js): add changeset for dev browser partitioned cookie fix
brkalow fe8f025
Merge branch 'main' into bryce/fix-dev-browser-partitioned-cookie-race
brkalow 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 dev browser token being read from a stale non-partitioned cookie when `partitionedCookies` is enabled. The token is now kept in memory so FAPI requests always use the authoritative value. |
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,87 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { parsePublishableKey } from '@clerk/shared/keys'; | ||
|
|
||
| import { appConfigs } from '../presets'; | ||
| import type { FakeUser } from '../testUtils'; | ||
| import { createTestUtils, testAgainstRunningApps } from '../testUtils'; | ||
|
|
||
| testAgainstRunningApps({ withEnv: [appConfigs.envs.withEmailCodes] })( | ||
| 'dev browser partitioned cookies @generic', | ||
| ({ app }) => { | ||
| test.describe.configure({ mode: 'serial' }); | ||
|
|
||
| let fakeUser: FakeUser; | ||
|
|
||
| test.beforeAll(async () => { | ||
| const u = createTestUtils({ app }); | ||
| fakeUser = u.services.users.createFakeUser(); | ||
| await u.services.users.createBapiUser(fakeUser); | ||
| }); | ||
|
|
||
| test.afterAll(async () => { | ||
| await fakeUser.deleteIfExists(); | ||
| await app.teardown(); | ||
| }); | ||
|
|
||
| test('URL query param dev browser token takes precedence over existing partitioned cookie on initial load', async ({ | ||
| page, | ||
| context, | ||
| }) => { | ||
| const pk = app.env.publicVariables.get('CLERK_PUBLISHABLE_KEY'); | ||
| const { frontendApi } = parsePublishableKey(pk)!; | ||
| const fapiOrigin = `https://${frontendApi}`; | ||
|
|
||
| // Obtain a valid dev browser token directly from FAPI before any page load | ||
| const devBrowserRes = await page.request.post(`${fapiOrigin}/v1/dev_browser`); | ||
| expect(devBrowserRes.ok()).toBe(true); | ||
| const { id: freshToken } = await devBrowserRes.json(); | ||
| expect(freshToken).toBeTruthy(); | ||
|
|
||
| // Pre-set a stale __clerk_db_jwt cookie before the page ever loads. | ||
| // This simulates the partitioned cookie that already exists in the browser | ||
| // from a previous session. | ||
| const appUrl = new URL(app.serverUrl); | ||
| await context.addCookies([ | ||
| { | ||
| name: '__clerk_db_jwt', | ||
| value: 'stale_partitioned_value', | ||
| domain: appUrl.hostname, | ||
| path: '/', | ||
| }, | ||
| ]); | ||
|
|
||
| // Collect every dev browser token attached to FAPI requests | ||
| const fapiTokens: string[] = []; | ||
| page.on('request', req => { | ||
| if (req.url().includes('__clerk_db_jwt') && req.url().includes('/v1/')) { | ||
| const url = new URL(req.url()); | ||
| const token = url.searchParams.get('__clerk_db_jwt'); | ||
| if (token) { | ||
| fapiTokens.push(token); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| // Initial page load with the fresh token in the URL query param, | ||
| // simulating a redirect back from Clerk's Account Portal. | ||
| const signInUrl = new URL(app.serverUrl + '/sign-in'); | ||
| signInUrl.searchParams.set('__clerk_db_jwt', freshToken); | ||
|
|
||
| await page.goto(signInUrl.toString()); | ||
| await page.waitForLoadState('networkidle'); | ||
|
|
||
| // Every FAPI request during initial load must use the URL token, | ||
| // not the stale partitioned cookie. | ||
| expect(fapiTokens.length).toBeGreaterThan(0); | ||
| for (const token of fapiTokens) { | ||
| expect(token).toBe(freshToken); | ||
| expect(token).not.toBe('stale_partitioned_value'); | ||
| } | ||
|
|
||
| // Verify clerk-js is functional: sign in should succeed | ||
| const u = createTestUtils({ app, page, context }); | ||
| await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeUser.email, password: fakeUser.password }); | ||
| await u.po.expect.toBeSignedIn(); | ||
| }); | ||
| }, | ||
| ); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't think of a reason why this wouldn't be safe. Potentially if there are already multiple dev browser cookies and a different value would have been read from cookies, but that would be a separate issue for us.