Skip to content

Instantly share code, notes, and snippets.

@AKhozya
Created October 9, 2026 21:48
Show Gist options
  • Select an option

  • Save AKhozya/06c6aab6ed3416e50fc8227823ac0a6c to your computer and use it in GitHub Desktop.

Select an option

Save AKhozya/06c6aab6ed3416e50fc8227823ac0a6c to your computer and use it in GitHub Desktop.
clerk-js token cache serves server-expired tokens when the device clock runs behind (reproduction)

clerk-js token cache serves server-expired tokens when the device clock runs behind

Reproduction for a clerk/javascript bug report. It adds two tests to packages/clerk-js/src/core/__tests__/tokenCache.test.ts.

Test Device clock Expected On main 44b09ba
control matches the server token served at T+50, gone at T+70 passes
skew 40 s behind the server token served at T+50, gone at T+70 fails: the token is still served at T+70, 10 s after it expired

Run

git clone https://github.com/clerk/javascript.git js && cd js
git checkout 44b09badc83099854ac746f7340d3cec77b031d7
git apply /path/to/tokenCache-device-clock-skew.test.diff
npx -y [email protected] install --filter @clerk/clerk-js... --frozen-lockfile --ignore-scripts
(cd packages/shared && npx -y [email protected] run build)
cd packages/clerk-js && npx vitest --watch=false src/core/__tests__/tokenCache.test.ts

Result on main: Tests 1 failed | 69 passed (70); the failure is device clock skew > serves no token the server considers expired when the device clock runs behind (AssertionError: expected { entry: … } to be undefined).

diff --git i/packages/clerk-js/src/core/__tests__/tokenCache.test.ts w/packages/clerk-js/src/core/__tests__/tokenCache.test.ts
index 27c0a45..7524e19 100644
--- i/packages/clerk-js/src/core/__tests__/tokenCache.test.ts
+++ w/packages/clerk-js/src/core/__tests__/tokenCache.test.ts
@@ -1987,4 +1987,42 @@ describe('SessionTokenCache', () => {
expect(SessionTokenCache.get({ audience: 'https://api.example.com', tokenId: 'aud-split' })?.entry).toBeDefined();
});
});
+
+ describe('device clock skew', () => {
+ // Server issues the token at T with a 60s lifetime. The device clock reads
+ // T - skew when the token is cached; skew > 0 means the device runs behind.
+ const serverIat = 1666648260;
+ const cacheAt = async (skewSeconds: number) => {
+ vi.setSystemTime(new Date((serverIat - skewSeconds) * 1000));
+ const key = { tokenId: `skew-${skewSeconds}` };
+ const token = new Token({ id: key.tokenId, jwt: createJwtWithTtl(serverIat, 60), object: 'token' });
+ SessionTokenCache.set({ ...key, tokenResolver: Promise.resolve<TokenResource>(token) });
+ await tick();
+ return key;
+ };
+
+ it('serves the token within its lifetime and none after it when the clocks agree', async () => {
+ const key = await cacheAt(0);
+
+ // Server time T + 50: 10s remain.
+ vi.advanceTimersByTime(50 * 1000);
+ expect(SessionTokenCache.get(key)?.entry).toBeDefined();
+
+ // Server time T + 70: expired 10s ago.
+ vi.advanceTimersByTime(20 * 1000);
+ expect(SessionTokenCache.get(key)).toBeUndefined();
+ });
+
+ it('serves no token the server considers expired when the device clock runs behind', async () => {
+ const key = await cacheAt(40);
+
+ // Server time T + 50: 10s remain.
+ vi.advanceTimersByTime(50 * 1000);
+ expect(SessionTokenCache.get(key)?.entry).toBeDefined();
+
+ // Server time T + 70: expired 10s ago, past the backend's 5s default skew allowance.
+ vi.advanceTimersByTime(20 * 1000);
+ expect(SessionTokenCache.get(key)).toBeUndefined();
+ });
+ });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment