-
Notifications
You must be signed in to change notification settings - Fork 461
feat(js): attach an optional server-configured session token to sign-in #9299
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
zourzouvillys
wants to merge
4
commits into
main
Choose a base branch
from
theo/js-session-token-clean
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
de9b78e
feat(js): attach an optional server-configured session token to sign-in
zourzouvillys 418ac83
fix(js): take the Protect token inline and harden acquisition
zourzouvillys 7a76ac2
fix(js): follow the shape-3 Specter contract for the inline token
zourzouvillys ed13b06
Merge branch 'main' into theo/js-session-token-clean
zourzouvillys 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,12 @@ | ||
| --- | ||
| '@clerk/clerk-js': minor | ||
| '@clerk/shared': minor | ||
| --- | ||
|
|
||
| Acquire an optional Protect session token and attach it to sign-in and sign-up requests. | ||
|
|
||
| On instances whose loader config references the new `{cid}` / `{pid}` / `{rid}` / `{instance_id}` / `{sdkver}` placeholders, Clerk mints an opaque, random correlation id and substitutes it into the loader's attributes and `textContent`. The loader is served with a signed session token, which is taken up once per browser session — shared across tabs under a lock rather than acquired once per tab — and travels alongside the correlation id and an acquisition status in the form-encoded body of sign-in and sign-up requests. | ||
|
|
||
| Acquisition can never block or fail a sign-in: it is bounded by a deadline, and when no token can be obtained a status (`no_token`, `timeout`, `script_error`, `fetch_error`, `unsupported`, `http_<n>`) travels in its place and the request proceeds unchanged. Only the loader carrying the correlation id is governed by the shared token; any other configured loader is still applied on every page load. Nothing is stored in the browser unless a loader references `{cid}`, `{pid}` or `{rid}`. | ||
|
|
||
| `ProtectLoader` gains two optional fields: `tokenTimeoutMs`, and `tokenUrl` for instances that opt into fetching the token from a dedicated endpoint instead of taking the one served with the loader. | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| import type { ProtectLoader } from '@clerk/shared/types'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { Protect } from '../protect'; | ||
| import { __internal_resetProtectStorage } from '../protectSession'; | ||
| import type { Environment } from '../resources'; | ||
|
|
||
| const environment = (loaders: unknown[], id = ''): Environment => | ||
| ({ protectConfig: { id, loaders } }) as unknown as Environment; | ||
|
|
||
| /** No `src`: jsdom fetches real URLs, which fires `error` and races the events we drive here. */ | ||
| const loader = (overrides: Partial<ProtectLoader> = {}): ProtectLoader => ({ | ||
| target: 'head', | ||
| type: 'script', | ||
| attributes: { 'data-cid': '{cid}' }, | ||
| tokenTimeoutMs: 200, | ||
| ...overrides, | ||
| }); | ||
|
|
||
| const nowSeconds = () => Math.floor(Date.now() / 1_000); | ||
|
|
||
| /** The token loader is injected under the acquisition lock, so it appears a few ticks in. */ | ||
| const injected = async (selector: string): Promise<Element> => { | ||
| for (let i = 0; i < 100; i++) { | ||
| const element = document.head.querySelector(selector); | ||
| if (element) { | ||
| return element; | ||
| } | ||
| await new Promise(resolve => setTimeout(resolve, 0)); | ||
| } | ||
| throw new Error(`nothing matched ${selector}`); | ||
| }; | ||
|
|
||
| const serveInline = (element: Element, overrides: Record<string, unknown> = {}) => { | ||
| (globalThis as unknown as Record<string, unknown>).__clerk_specter = { | ||
| v: 3, | ||
| id: '11111111-2222-3333-4444-555555555555', | ||
| cid: element.getAttribute('data-cid'), | ||
| ready: Promise.resolve({ token: 'v1.payload.mac', exp: nowSeconds() + 43_200 }), | ||
| ...overrides, | ||
| }; | ||
| element.dispatchEvent(new Event('load')); | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| localStorage.clear(); | ||
| __internal_resetProtectStorage(); | ||
| document.head.innerHTML = ''; | ||
| document.body.innerHTML = ''; | ||
| delete (globalThis as unknown as Record<string, unknown>).__clerk_specter; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| localStorage.clear(); | ||
| __internal_resetProtectStorage(); | ||
| delete (globalThis as unknown as Record<string, unknown>).__clerk_specter; | ||
| }); | ||
|
|
||
| describe('Protect.load', () => { | ||
| it('does nothing without a protect config', () => { | ||
| new Protect().load(environment([])); | ||
| expect(document.head.querySelector('script')).toBeNull(); | ||
| expect(localStorage.getItem('__clerk_protect_pid')).toBeNull(); | ||
| }); | ||
|
|
||
| it('applies an untemplated loader unchanged and reports no params', async () => { | ||
| const protect = new Protect(); | ||
| protect.load( | ||
| environment([ | ||
| { target: 'head', type: 'script', attributes: { 'data-loader': 'https://loader.example.com/ins_2abc.js' } }, | ||
| ]), | ||
| ); | ||
|
|
||
| expect(document.head.querySelector('script')?.getAttribute('data-loader')).toBe( | ||
| 'https://loader.example.com/ins_2abc.js', | ||
| ); | ||
| await expect(protect.getRequestParams()).resolves.toBeUndefined(); | ||
| expect(localStorage.getItem('__clerk_protect_pid')).toBeNull(); | ||
| }); | ||
|
|
||
| it('substitutes the placeholders it recognises and leaves the rest verbatim', async () => { | ||
| const protect = new Protect(); | ||
| protect.load( | ||
| environment( | ||
| [ | ||
| loader({ | ||
| attributes: { | ||
| 'data-src': 'https://loader.example.com/{instance_id}/{cid}/loader.js', | ||
| 'data-pid': '{pid}', | ||
| 'data-rid': '{rid}', | ||
| 'data-unknown': '{whatever}', | ||
| 'data-count': 3, | ||
| }, | ||
| }), | ||
| ], | ||
| 'ins_2abc', | ||
| ), | ||
| ); | ||
|
|
||
| const element = await injected('script'); | ||
| const pid = element.getAttribute('data-pid') as string; | ||
| const rid = element.getAttribute('data-rid') as string; | ||
|
|
||
| expect(pid).toMatch(/^[a-z2-7]{26}$/); | ||
| expect(rid).toMatch(/^[a-z2-7]{26}$/); | ||
| expect(element.getAttribute('data-src')).toBe(`https://loader.example.com/ins_2abc/1-${pid}-${rid}/loader.js`); | ||
| expect(element.getAttribute('data-unknown')).toBe('{whatever}'); | ||
| expect(element.getAttribute('data-count')).toBe('3'); | ||
| }); | ||
|
|
||
| it('substitutes placeholders in textContent as well as attributes', async () => { | ||
| const protect = new Protect(); | ||
| protect.load(environment([loader({ textContent: 'window.__vendor_cid = "{cid}";' })], 'ins_2abc')); | ||
|
|
||
| const element = await injected('script'); | ||
| expect(element.textContent).toBe(`window.__vendor_cid = "${element.getAttribute('data-cid')}";`); | ||
| expect(element.textContent).not.toContain('{cid}'); | ||
| }); | ||
|
|
||
| it('leaves {instance_id} verbatim when the environment does not carry one', async () => { | ||
| const protect = new Protect(); | ||
| protect.load(environment([loader({ attributes: { 'data-src': '{instance_id}/{cid}.js' } })])); | ||
|
|
||
| expect((await injected('script')).getAttribute('data-src')).toContain('{instance_id}'); | ||
| }); | ||
|
|
||
| it('attaches the token once it has been acquired', async () => { | ||
| const protect = new Protect(); | ||
| protect.load(environment([loader()])); | ||
|
|
||
| serveInline(await injected('script')); | ||
|
|
||
| await expect(protect.getRequestParams()).resolves.toMatchObject({ | ||
| __clerk_protect_token: 'v1.payload.mac', | ||
| __clerk_protect_status: 'ok', | ||
| }); | ||
| }); | ||
|
|
||
| it('still applies the other loaders when a token for this browser session is shared', async () => { | ||
| localStorage.setItem( | ||
| '__clerk_protect_st', | ||
| JSON.stringify({ token: 'v1.cached.mac', exp: nowSeconds() + 43_200, rid: 'b'.repeat(26) }), | ||
| ); | ||
|
|
||
| const protect = new Protect(); | ||
| protect.load( | ||
| environment([ | ||
| loader({ attributes: { 'data-role': 'detection' } }), | ||
| loader({ attributes: { 'data-cid': '{cid}', 'data-role': 'token' } }), | ||
| ]), | ||
| ); | ||
|
|
||
| // Acquisition happens once per browser session, so the token loader is skipped… | ||
| expect(document.head.querySelector('[data-role="token"]')).toBeNull(); | ||
| // …but the detection loader has its own job and runs on every page load regardless. | ||
| expect(document.head.querySelector('[data-role="detection"]')).not.toBeNull(); | ||
| await expect(protect.getRequestParams()).resolves.toMatchObject({ __clerk_protect_token: 'v1.cached.mac' }); | ||
| }); | ||
|
|
||
| it('does not apply a loader that is outside its rollout', async () => { | ||
| vi.spyOn(Math, 'random').mockReturnValue(0.9); | ||
|
|
||
| const protect = new Protect(); | ||
| protect.load(environment([loader({ rollout: 0.1 })])); | ||
|
|
||
| expect(document.head.querySelector('script')).toBeNull(); | ||
| // Out of rollout means Protect is off for this browser, so there is nothing to report either. | ||
| await expect(protect.getRequestParams()).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('reports script_error when the loader element fails to load', async () => { | ||
| const protect = new Protect(); | ||
| protect.load(environment([loader({ tokenTimeoutMs: 5_000 })])); | ||
|
|
||
| (await injected('script')).dispatchEvent(new Event('error')); | ||
|
|
||
| await expect(protect.getRequestParams()).resolves.toMatchObject({ __clerk_protect_status: 'script_error' }); | ||
| }); | ||
|
|
||
| it('drops a malformed loader entry without failing the rest of the load', async () => { | ||
| const protect = new Protect(); | ||
|
|
||
| // The config is server-controlled and cached; a bad entry must not take Clerk.load() down. | ||
| expect(() => protect.load(environment([null, loader({ attributes: { 'data-role': 'good' } })]))).not.toThrow(); | ||
|
|
||
| expect(document.head.querySelector('[data-role="good"]')).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('survives a loader config that is not an array of objects at all', () => { | ||
| const protect = new Protect(); | ||
| expect(() => protect.load(environment(['nope', 42, undefined]))).not.toThrow(); | ||
| }); | ||
| }); |
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.