Skip to content

Fix repeated silent authentication failures caused by stale refresh tokens - #335641

Open
GaspardRuan (gaspardruan) wants to merge 1 commit into
microsoft:mainfrom
gaspardruan:fix/microsoft-authentication-stale-refresh-token
Open

Fix repeated silent authentication failures caused by stale refresh tokens#335641
GaspardRuan (gaspardruan) wants to merge 1 commit into
microsoft:mainfrom
gaspardruan:fix/microsoft-authentication-stale-refresh-token

Conversation

@gaspardruan

Copy link
Copy Markdown

Fixes #335639

Problem

I investigated this after Settings Sync continued to show “Sign in to Sync Settings” following a successful Microsoft sign-in on macOS. Logs showed createSession returning a session, followed by getSessions returning no sessions after AADSTS700082. I can no longer reproduce the original sign-in failure with my real account. Instead, the tests use a simulated account, synthetic tokens, and simulated identity-server responses with the real MSAL library and VS Code authentication code to reproduce the token-cache defect described below. The same regression tests fail on the original code and pass with the proposed fix.

The Microsoft authentication extension's current MSAL dependency can repeatedly reload a rejected refresh token from persisted storage. MSAL removes the token from memory on an invalid_grant / bad_token response, but does not call the persistence callback on that failure path.

When a rejected family refresh token coexists with a fresh application refresh token for the same account, repeated silent requests keep submitting the family token and failing despite the fresh credential being available.

Reproduction

See #335639 for the reproduction steps, expected results before and after the fix, and an auxiliary standalone MSAL reproduction.

Fix

Persist MSAL's removal of the rejected token before retrying silent authentication once, within the existing PCA sequencer. Apply this to ordinary silent acquisition and ID-token refresh. Broker authentication and unrelated errors keep their existing behavior.

If the persisted cache changed while the request was in flight, reload it instead of writing the failed request's snapshot. This check is not atomic with the subsequent write, so a concurrent update between them can still be overwritten. A concurrent cache change can also exhaust the retry, requiring a subsequent session request.

Copilot AI balanced review requested due to automatic review settings September 11, 2026 07:56
@vs-code-engineering

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

TylerLeonhardt

Matched files:

  • extensions/microsoft-authentication/src/common/cachePlugin.ts
  • extensions/microsoft-authentication/src/node/cachedPublicClientApplication.ts
  • extensions/microsoft-authentication/src/node/test/tokenCache.test.ts
  • extensions/microsoft-authentication/src/node/tokenCache.ts

@gaspardruan GaspardRuan (gaspardruan) changed the title Persist rejected MSAL refresh tokens before retrying silent authentication Fix repeated silent authentication failures caused by stale refresh tokens Sep 11, 2026
@gaspardruan

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused implementation matches the described failure mode and has comprehensive regression coverage.

Pull request overview

Fixes repeated Microsoft authentication failures by persisting MSAL’s rejected refresh-token removal before a bounded retry.

Changes:

  • Adds cache-aware silent token acquisition with one retry.
  • Preserves concurrent SecretStorage updates.
  • Adds regression and concurrency coverage.
File summaries
File Description
common/cachePlugin.ts Handles cache persistence after authentication failures.
node/tokenCache.ts Implements invalidation and bounded retry logic.
node/cachedPublicClientApplication.ts Integrates recovery into non-broker silent authentication.
node/test/tokenCache.test.ts Tests recovery, persistence, errors, and concurrent updates.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Microsoft authentication keeps using an expired refresh token after successful sign-in

3 participants