-
Notifications
You must be signed in to change notification settings - Fork 402
fix(clerk-js): Add Partitioned to __client_uat in CHIPS build #5785
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
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,5 @@ | ||
| --- | ||
| '@clerk/clerk-js': patch | ||
| --- | ||
|
|
||
| Adding Partitioned attribute to \_\_client_uat cookie in CHIPS build variant |
127 changes: 127 additions & 0 deletions
127
packages/clerk-js/src/core/auth/cookies/__tests__/clientUat.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,127 @@ | ||
| import { createCookieHandler } from '@clerk/shared/cookie'; | ||
| import { addYears } from '@clerk/shared/date'; | ||
|
|
||
| import { inCrossOriginIframe } from '../../../../utils'; | ||
| import { getCookieDomain } from '../../getCookieDomain'; | ||
| import { getSecureAttribute } from '../../getSecureAttribute'; | ||
| import { createClientUatCookie } from '../clientUat'; | ||
|
|
||
| jest.mock('@clerk/shared/cookie'); | ||
| jest.mock('@clerk/shared/date'); | ||
| jest.mock('../../../../utils'); | ||
| jest.mock('../../getCookieDomain'); | ||
| jest.mock('../../getSecureAttribute'); | ||
|
|
||
| describe('createClientUatCookie', () => { | ||
| const mockCookieSuffix = 'test-suffix'; | ||
| const mockExpires = new Date('2024-12-31'); | ||
| const mockDomain = 'test.domain'; | ||
| const mockSet = jest.fn(); | ||
| const mockRemove = jest.fn(); | ||
| const mockGet = jest.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mockGet.mockReset(); | ||
| (addYears as jest.Mock).mockReturnValue(mockExpires); | ||
| (inCrossOriginIframe as jest.Mock).mockReturnValue(false); | ||
| (getCookieDomain as jest.Mock).mockReturnValue(mockDomain); | ||
| (getSecureAttribute as jest.Mock).mockReturnValue(true); | ||
| (createCookieHandler as jest.Mock).mockImplementation(() => ({ | ||
| set: mockSet, | ||
| remove: mockRemove, | ||
| get: mockGet, | ||
| })); | ||
| }); | ||
|
|
||
| it('should create both suffixed and non-suffixed cookie handlers', () => { | ||
| createClientUatCookie(mockCookieSuffix); | ||
| expect(createCookieHandler).toHaveBeenCalledTimes(2); | ||
| expect(createCookieHandler).toHaveBeenCalledWith('__client_uat'); | ||
| expect(createCookieHandler).toHaveBeenCalledWith('__client_uat_test-suffix'); | ||
| }); | ||
|
|
||
| it('should set cookies with correct parameters in non-cross-origin context', () => { | ||
| const cookieHandler = createClientUatCookie(mockCookieSuffix); | ||
| cookieHandler.set({ | ||
| id: 'test-client', | ||
| updatedAt: new Date('2024-01-01'), | ||
| signedInSessions: ['session1'], | ||
| }); | ||
|
|
||
| expect(mockSet).toHaveBeenCalledTimes(2); | ||
| expect(mockSet).toHaveBeenCalledWith('1704067200', { | ||
| domain: mockDomain, | ||
| expires: mockExpires, | ||
| sameSite: 'Strict', | ||
| secure: true, | ||
| partitioned: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('should set cookies with None sameSite in cross-origin context', () => { | ||
| (inCrossOriginIframe as jest.Mock).mockReturnValue(true); | ||
| const cookieHandler = createClientUatCookie(mockCookieSuffix); | ||
| cookieHandler.set({ | ||
| id: 'test-client', | ||
| updatedAt: new Date('2024-01-01'), | ||
| signedInSessions: ['session1'], | ||
| }); | ||
|
|
||
| expect(mockSet).toHaveBeenCalledWith('1704067200', { | ||
| domain: mockDomain, | ||
| expires: mockExpires, | ||
| sameSite: 'None', | ||
| secure: true, | ||
| partitioned: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('should set value to 0 when client is undefined', () => { | ||
| const cookieHandler = createClientUatCookie(mockCookieSuffix); | ||
| cookieHandler.set(undefined); | ||
|
|
||
| expect(mockSet).toHaveBeenCalledWith('0', { | ||
| domain: mockDomain, | ||
| expires: mockExpires, | ||
| sameSite: 'Strict', | ||
| secure: true, | ||
| partitioned: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('should set value to 0 when client has no signed in sessions', () => { | ||
| const cookieHandler = createClientUatCookie(mockCookieSuffix); | ||
| cookieHandler.set({ | ||
| id: 'test-client', | ||
| updatedAt: new Date('2024-01-01'), | ||
| signedInSessions: [], | ||
| }); | ||
|
|
||
| expect(mockSet).toHaveBeenCalledWith('0', { | ||
| domain: mockDomain, | ||
| expires: mockExpires, | ||
| sameSite: 'Strict', | ||
| secure: true, | ||
| partitioned: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('should get cookie value from suffixed cookie first, then fallback to non-suffixed', () => { | ||
| mockGet.mockImplementationOnce(() => '1234567890').mockImplementationOnce(() => '9876543210'); | ||
|
|
||
| const cookieHandler = createClientUatCookie(mockCookieSuffix); | ||
| const result = cookieHandler.get(); | ||
|
|
||
| expect(result).toBe(1234567890); | ||
| }); | ||
|
|
||
| it('should return 0 when no cookie value is present', () => { | ||
| mockGet.mockImplementationOnce(() => undefined).mockImplementationOnce(() => undefined); | ||
|
|
||
| const cookieHandler = createClientUatCookie(mockCookieSuffix); | ||
| const result = cookieHandler.get(); | ||
|
|
||
| expect(result).toBe(0); | ||
| }); | ||
| }); |
91 changes: 91 additions & 0 deletions
91
packages/clerk-js/src/core/auth/cookies/__tests__/session.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,91 @@ | ||
| import { createCookieHandler } from '@clerk/shared/cookie'; | ||
| import { addYears } from '@clerk/shared/date'; | ||
|
|
||
| import { inCrossOriginIframe } from '../../../../utils'; | ||
| import { getSecureAttribute } from '../../getSecureAttribute'; | ||
| import { createSessionCookie } from '../session'; | ||
|
|
||
| jest.mock('@clerk/shared/cookie'); | ||
| jest.mock('@clerk/shared/date'); | ||
| jest.mock('../../../../utils'); | ||
| jest.mock('../../getSecureAttribute'); | ||
|
|
||
| describe('createSessionCookie', () => { | ||
| const mockCookieSuffix = 'test-suffix'; | ||
| const mockToken = 'test-token'; | ||
| const mockExpires = new Date('2024-12-31'); | ||
| const mockSet = jest.fn(); | ||
| const mockRemove = jest.fn(); | ||
| const mockGet = jest.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| mockGet.mockReset(); | ||
| (addYears as jest.Mock).mockReturnValue(mockExpires); | ||
| (inCrossOriginIframe as jest.Mock).mockReturnValue(false); | ||
| (getSecureAttribute as jest.Mock).mockReturnValue(true); | ||
| (createCookieHandler as jest.Mock).mockImplementation(() => ({ | ||
| set: mockSet, | ||
| remove: mockRemove, | ||
| get: mockGet, | ||
| })); | ||
| }); | ||
|
|
||
| it('should create both suffixed and non-suffixed cookie handlers', () => { | ||
| createSessionCookie(mockCookieSuffix); | ||
| expect(createCookieHandler).toHaveBeenCalledTimes(2); | ||
| expect(createCookieHandler).toHaveBeenCalledWith('__session'); | ||
| expect(createCookieHandler).toHaveBeenCalledWith('__session_test-suffix'); | ||
| }); | ||
|
|
||
| it('should set cookies with correct parameters in non-cross-origin context', () => { | ||
| const cookieHandler = createSessionCookie(mockCookieSuffix); | ||
| cookieHandler.set(mockToken); | ||
|
|
||
| expect(mockSet).toHaveBeenCalledTimes(2); | ||
| expect(mockSet).toHaveBeenCalledWith(mockToken, { | ||
| expires: mockExpires, | ||
| sameSite: 'Lax', | ||
| secure: true, | ||
| partitioned: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('should set cookies with None sameSite in cross-origin context', () => { | ||
| (inCrossOriginIframe as jest.Mock).mockReturnValue(true); | ||
| const cookieHandler = createSessionCookie(mockCookieSuffix); | ||
| cookieHandler.set(mockToken); | ||
|
|
||
| expect(mockSet).toHaveBeenCalledWith(mockToken, { | ||
| expires: mockExpires, | ||
| sameSite: 'None', | ||
| secure: true, | ||
| partitioned: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('should remove both cookies when remove is called', () => { | ||
| const cookieHandler = createSessionCookie(mockCookieSuffix); | ||
| cookieHandler.remove(); | ||
|
|
||
| expect(mockRemove).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('should get cookie value from suffixed cookie first, then fallback to non-suffixed', () => { | ||
| mockGet.mockImplementationOnce(() => 'suffixed-value').mockImplementationOnce(() => 'non-suffixed-value'); | ||
|
|
||
| const cookieHandler = createSessionCookie(mockCookieSuffix); | ||
| const result = cookieHandler.get(); | ||
|
|
||
| expect(result).toBe('suffixed-value'); | ||
| }); | ||
|
|
||
| it('should fallback to non-suffixed cookie when suffixed cookie is not present', () => { | ||
| mockGet.mockImplementationOnce(() => undefined).mockImplementationOnce(() => 'non-suffixed-value'); | ||
|
|
||
| const cookieHandler = createSessionCookie(mockCookieSuffix); | ||
| const result = cookieHandler.get(); | ||
|
|
||
| expect(result).toBe('non-suffixed-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 |
|---|---|---|
|
|
@@ -37,8 +37,9 @@ export const createClientUatCookie = (cookieSuffix: string): ClientUatCookieHand | |
| * Generally, this is handled by redirectWithAuth() being called and relying on the dev browser ID in the URL, | ||
| * but if that isn't used we rely on this. In production, nothing is cross-domain and Lax is used when client_uat is set from FAPI. | ||
| */ | ||
| const sameSite = inCrossOriginIframe() ? 'None' : 'Strict'; | ||
| const sameSite = __BUILD_VARIANT_CHIPS__ ? 'None' : inCrossOriginIframe() ? 'None' : 'Strict'; | ||
jacekradko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const secure = getSecureAttribute(sameSite); | ||
| const partitioned = __BUILD_VARIANT_CHIPS__ && secure; | ||
| const domain = getCookieDomain(); | ||
|
|
||
| // '0' indicates the user is signed out | ||
|
|
@@ -53,8 +54,8 @@ export const createClientUatCookie = (cookieSuffix: string): ClientUatCookieHand | |
| suffixedClientUatCookie.remove(); | ||
| clientUatCookie.remove(); | ||
|
|
||
| suffixedClientUatCookie.set(val, { expires, sameSite, domain, secure }); | ||
| clientUatCookie.set(val, { expires, sameSite, domain, secure }); | ||
| suffixedClientUatCookie.set(val, { domain, expires, partitioned, sameSite, secure }); | ||
| clientUatCookie.set(val, { domain, expires, partitioned, sameSite, secure }); | ||
|
Comment on lines
+57
to
+58
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Classic @jacekradko 😉
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤷🏼♂️ |
||
| }; | ||
|
|
||
| return { | ||
|
|
||
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.
That's a lot of mocks 😅
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.
Too many?