Session token cache drops every token when the device clock runs fast, so the poller mints a token every 1.5 s (fast-clock side of #10401)

オープン
#10,575 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

  • #10583 @umeraamir69 による — マージされずにクローズ

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
28/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
javascript, typescript

調査の方向性

The bug sits in the session token cache, where expiresAt is computed as Math.min(l.exp, n.createdAt + l.exp - l.iat), and in startPollingForSessionToken, which calls session.getToken() every 1500 ms or 5000 ms. Reproduce by overriding Date.now to add about 200 s and watching for repeated POST …/tokens calls. Done means a freshly minted token stays cached under a fast clock, but the issue leaves the fix approach open, so confirm the direction with maintainers first; the linked PR #10583 was closed without merging.

索引モデルが issue の本文から書いたものです。

説明

Packages
  • @clerk/clerk-js 6.42.1 (latest; same in 6.42.2-canary.v20261102095039). Same effect in 6.41.0, 6.7.5 and 6.3.3.
  • Loaded via @clerk/vue 2.0.7 and @clerk/react 6.14.x (CDN @clerk/clerk-js@6), and bundled directly.
What happens

When the device clock runs about a minute or more fast, clerk-js discards every session token as soon as it is cached. Every getToken() call and every poller tick then goes to POST /v1/client/sessions/{id}/tokens.

Measured with one focused tab and the clock 200 s ahead: 43 token POSTs in 71 s, versus one every ~44 s with a correct clock. With about ten tabs open the Frontend API answers 429, getToken() keeps retrying (up to 8 times online, ~160 s), and anything that waits for a token stalls.

This is not theoretical: within four days we saw two production clients whose clocks were +198 s and +224 s off (one after a laptop battery had run completely flat).

Reproduction
  1. Sign in to any app that uses clerk-js 6.42.1.
  2. Set the OS clock ~3 minutes ahead, or in DevTools: const real = Date.now.bind(Date); Date.now = () => real() + 200_000;
  3. Keep the tab focused and watch the network panel: POST …/tokens about every 1.5 s (every 5 s when the tab is not focused).
Cause

In 6.42.1 the cache entry is created with createdAt: Math.floor(Date.now() / 1e3) and stores

expiresAt: Math.min(l.exp, n.createdAt + l.exp - l.iat)

and both the read path and the eviction timer compare it with the device clock (expiresAt - Math.floor(Date.now() / 1e3), evict at <= 5 / <= 0). With a fast clock localInsert + lifetime is later than exp, so min() picks the server exp, and exp - localNow is already negative (or below the 5 s buffer) at insert. The entry is dropped at once. Before 6.42 the check was (exp - iat) - (localNow - iat) <= 5, which has the same effect.

The poller (startPollingForSessionToken, 1500 ms while document.hasFocus(), else 5000 ms) calls session.getToken() on every tick, so every tick mints a token. Focus and visibilitychange refreshes add more.

#10401 states this directly ("Fast clocks behave as before") and notes that the clock offset cannot be measured in the browser because FAPI does not expose Date cross-origin. #10361, which would have aged tokens from the local insert time, was closed in favour of #10401.

Suggested fix

Any of these would make a fast clock harmless:

  1. For a token this client has just minted (response of POST …/tokens), judge freshness by local elapsed time since insert against the token lifetime (exp - iat), as in #10361. Keep the current min() behaviour only for tokens that arrive already aged (hydrated lastActiveToken, another tab's broadcast), which is the case #10401 was protecting.
  2. Or estimate the clock offset once from a freshly minted token (localInsert - iat, accurate to about the request time) and use it in every Date.now() comparison of the token cache and poller.
  3. Or add Date to the Frontend API's Access-Control-Expose-Headers, so the SDK can measure the offset from any FAPI response.

As a workaround we are moving our GraphQL auth links to a token cache of our own that ages tokens with the monotonic clock since receipt, so our own requests stop depending on the device clock. The poller and focus refresh inside clerk-js still mint at the rates above, and only a fix in clerk-js can stop that.

主要言語
TypeScript
スター
1.8k
フォーク
492
平均マージ
1日 7時間
マージ済み PR(30日)
357

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

clerk/javascript のほかの issue

clerk/javascript の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。