AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Multi-Session: GetToken() On Non-Active Session Writes Its Token To __Session Cookie

In a multi-session setup, calling getToken() on a non-active session (or during cache hydration for a non-active session) incorrectly triggers a TokenUpdate event that updates the __session cookie, causing temporary authentication as the wrong user in focused tabs.

highConfidence 95%@clerk/clerk-JscurrentUser()clerkMiddlewareClerkProviderAffected V6.34.1

Origin Analysis

Session.getToken() and #hydrateCache set shouldDispatchTokenUpdate = true without verifying that the session is the client's active session, leading AuthCookieService to write the token to __session even when the session is not active.
1. Enable multi-session and sign in as user A, then add user B in the same browser (two sessions, A active). 2. Call getToken() on B's session object (e.g., clerk.client.sessions.find(s => s.id !== clerk.session.id).getToken()). 3. Check the __session cookie - it now contains B's token. 4. Keep the tab focused; B's token refresh will repeatedly overwrite __session until A's poller restores it, causing intermittent wrong-user authentication.

Fixing Code Block

Edge Case Audit

The fix prevents inactive sessions from updating the __session cookie, which may break applications that relied on this behavior to authenticate background requests as a secondary user from the focused tab. Multi-tab scenarios: if two tabs have different active sessions, the cookie may still be overwritten by whichever tab is focused and performs an update; session switching while a getToken call is in flight could still cause a brief inconsistency. Rollback: revert the added condition; consider clearing cookies after rollback. Concurrent refreshes or cache hydrations may still race if active session changes between the check and the token request.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state