Title: clerk-js: SessionTokenCache serves server-expired session tokens when the device clock runs behind (regression of #672) · Issue #10185 · clerk/javascript · GitHub
Open Graph Title: clerk-js: SessionTokenCache serves server-expired session tokens when the device clock runs behind (regression of #672) · Issue #10185 · clerk/javascript
X Title: clerk-js: SessionTokenCache serves server-expired session tokens when the device clock runs behind (regression of #672) · Issue #10185 · clerk/javascript
Description: Preliminary Checks I have reviewed the documentation: https://clerk.com/docs I have searched for existing issues: https://github.com/clerk/javascript/issues I have not already reached out to Clerk support via email or Discord This issue ...
Open Graph Description: Preliminary Checks I have reviewed the documentation: https://clerk.com/docs I have searched for existing issues: https://github.com/clerk/javascript/issues I have not already reached out to Clerk ...
X Description: Preliminary Checks I have reviewed the documentation: https://clerk.com/docs I have searched for existing issues: https://github.com/clerk/javascript/issues I have not already reached out to Clerk ...
Opengraph URL: https://github.com/clerk/javascript/issues/10185
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"clerk-js: SessionTokenCache serves server-expired session tokens when the device clock runs behind (regression of #672)","articleBody":"### Preliminary Checks\n\n- [x] I have reviewed the documentation: https://clerk.com/docs\n- [x] I have searched for existing issues: https://github.com/clerk/javascript/issues\n- [x] I have not already reached out to Clerk support via email or Discord\n- [x] This issue is not a question, general help request, or anything other than a bug report directly related to Clerk.\n\n### Reproduction\n\nhttps://gist.github.com/AKhozya/06c6aab6ed3416e50fc8227823ac0a6c\n\n### Publishable key\n\nNot included: the bug is in clerk-js's token cache and does not depend on an instance. Happy to share one if it helps.\n\n### Description\n\nIf the device clock runs behind the server, `SessionTokenCache.get()` keeps returning a session token after the token has expired by server time. Once the token is more than 5 s past `exp`, the backend's default allowance (`DEFAULT_CLOCK_SKEW_IN_MS`, `packages/backend/src/jwt/verifyJwt.ts:20`) rejects it.\n\n**Cause.** When a token resolves, `tokenCache.ts:411` sets `live.createdAt = issuedAt`, the JWT `iat` in server time. `get()` (line 194) computes `elapsed = deviceNow - createdAt`, so `remainingTtl = (exp - iat) - (deviceNow - iat) = exp - deviceNow`. That compares server time with device time directly. If the device runs S seconds behind, the cache serves the token for S seconds after it expired. The deletion timer (line 409, `expiresAt - deviceNow`) fires S seconds late as well.\n\n**History.**\n\n| PR | Merged | Change to `tokenCache.ts` |\n|---|---|---|\n| #672 | 2023-01-27 | \"Account for user clock skew in TokenCache\": `createdAt` from the device clock at caching time, so elapsed time is measured on one clock |\n| #7317 | 2026-01-30 | \"Proactive session token refresh\": `value.createdAt = issuedAt` |\n| #9030 | 2026-07-03 | keeps it as `live.createdAt = issuedAt` |\n\n`protectSession.ts:291` already describes this hazard for another store and avoids it by measuring elapsed time on one clock.\n\n**Steps to reproduce:** apply the test from the gist to `packages/clerk-js/src/core/__tests__/tokenCache.test.ts` on main `44b09ba` and run that file.\n\n1. The server issues a token at T with a 60 s lifetime.\n2. The device clock reads T − 40 when the token is cached.\n3. Advance 50 s: `get()` returns the token (correct, 10 s remain).\n4. Advance 20 s more: by server time the token expired 10 s ago.\n\n**Expected behavior:** at step 4, `get()` returns `undefined`, as it does in the control test with no skew.\n\n**Actual behavior:** at step 4, `get()` still returns the token. Result: `Tests 1 failed | 69 passed (70)`; only the skew test fails.\n\n**Effect in an app.** Our Expo app sends the session token to our own Express backend, which calls `verifyToken` and answers a failed verification with 401.\n\n| Emulator clock | Chat sends that failed |\n|---|---|\n| 36 s behind the host | about 5 of 15, each with the app's error for an HTTP 4xx other than 429 (status not captured) |\n| resynced to the host | 0 of 10 |\n\nOur workaround, not yet deployed: `clockSkewInMs: 60_000` on `verifyToken`, and one retry with `getToken({ skipCache: true })` after a 401.\n\n**On a fix.** If `createdAt` takes the device clock when the token resolves, the new test passes. #672 uses the device clock at caching time. If a token is already partway through its lifetime when it reaches the cache, `get()` counts its full lifetime from that moment. If the clocks agree, the deletion timer (`exp` minus device time) still evicts such a token at its real expiry, so `get()` serves it late only if that timer runs late. It also fails one existing test, `broadcast resilience > a failing postMessage does not evict the freshly cached token`, because that test's fixture has `iat` after `exp`. A fix that handles both a device clock running behind and a token that reaches the cache partway through its lifetime may need the offset between the server clock and the device clock, for example measured when a token is freshly minted. We have not tested such a fix.\n\n### Environment\n\n| Item | Value |\n|---|---|\n| `@clerk/expo` | 4.6.9 |\n| `@clerk/clerk-js` installed | 6.33.0; its built bundle sets the cache entry's `createdAt` to the token's `iat` |\n| clerk/javascript commit tested | main `44b09ba` |\n| Expo | SDK 57 |\n| React Native | 0.86.3 |\n| Device | Android emulator, system image android-37.1 |\n","author":{"url":"https://github.com/AKhozya","@type":"Person","name":"AKhozya"},"datePublished":"2026-10-09T21:48:13Z","url":"https://github.com/clerk/javascript/issues/10185"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:dd1fb862-b657-75fc-abb0-a4163cefef16 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DF32:9464C:1290C54:195A48A:6AFB6DA1 |
| html-safe-nonce | 61fafae34cacfbbfa9ec504ae99c8fbfcd8cd3b8c2ecc21a146cb4a32242f0c2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERjMyOjk0NjRDOjEyOTBDNTQ6MTk1QTQ4QTo2QUZCNkRBMSIsInZpc2l0b3JfaWQiOiIzNDQ4NTY2ODUwMjEwNDU5MDQxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 93031a6fdb87143c09edee06ecb87762b0704aee8c6c3cf18ba3d935cb436dce |
| hovercard-subject-tag | issue:5785079358 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/clerk/javascript/10185/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0eface708f9ae1e973c562fc0b1720fa0a5d5de55bb9e9d551829617ffe6e961/clerk/javascript/issues/10185 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0eface708f9ae1e973c562fc0b1720fa0a5d5de55bb9e9d551829617ffe6e961/clerk/javascript/issues/10185 |
| og:image:alt | Preliminary Checks I have reviewed the documentation: https://clerk.com/docs I have searched for existing issues: https://github.com/clerk/javascript/issues I have not already reached out to Clerk ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | AKhozya |
| hostname | github.com |
| expected-hostname | github.com |
| None | cca289ecd54650fcf3c0b037a58b97186dee2cc4406819d87fab93728fd83700 |
| turbo-cache-control | no-preview |
| go-import | github.com/clerk/javascript git https://github.com/clerk/javascript.git |
| octolytics-dimension-user_id | 49538330 |
| octolytics-dimension-user_login | clerk |
| octolytics-dimension-repository_id | 438331492 |
| octolytics-dimension-repository_nwo | clerk/javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 438331492 |
| octolytics-dimension-repository_network_root_nwo | clerk/javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 88808b1fae5ab8a526a73e7ce5542a10eb9d6971 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width