Skip to content

fix(auth): deduplicate concurrent auth() calls to prevent rotating refresh token race#1803

Open
lanxevo3 wants to merge 3 commits intomodelcontextprotocol:mainfrom
lanxevo3:fix/oauth-refresh-race-condition
Open

fix(auth): deduplicate concurrent auth() calls to prevent rotating refresh token race#1803
lanxevo3 wants to merge 3 commits intomodelcontextprotocol:mainfrom
lanxevo3:fix/oauth-refresh-race-condition

Conversation

@lanxevo3
Copy link
Copy Markdown

When two requests receive a 401 simultaneously, both call \�uth()\ which each call
efreshAuthorization()\ with the same rotating refresh token. OAuth servers that implement RFC 6819 §5.2.2.3 replay detection revoke the entire token family on seeing a reused refresh token — permanently breaking the session until manual re-authorization.

Fix

Adds a \pendingAuth\ Map that tracks in-flight \�uth()\ Promises per provider. Concurrent callers for the same provider receive the same Promise and wait for one refresh instead of each triggering their own. Safe for both rotating and non-rotating token schemes.

\\ s
// Before: race condition
const [r1, r2] = await Promise.all([
auth(provider, options), // both call refreshAuthorization()
auth(provider, options), // with the same rotating token
]);

// After: only one refresh runs
const pending = pendingAuth.get(provider);
if (pending !== undefined) return pending; // wait for in-flight instead
\\

Fixes #1760.

OAuth 2.1 §3.2 requires token endpoint requests to use
application/x-www-form-urlencoded regardless of grant type.

Add an explicit header.set() call immediately before the fetch in
executeTokenRequest() to prevent any addClientAuthentication
implementation from accidentally overriding the Content-Type.

Fixes modelcontextprotocol/inspector#1160
…en race

When two requests receive a 401 simultaneously, both call auth() which
each call refreshAuthorization() with the same rotating refresh token.
OAuth servers that implement RFC 6819 §5.2.2.3 replay detection revoke
the entire token family on seeing a reused refresh token — permanently
breaking the session until manual re-authorization.

The fix adds a pending-auth Map that tracks in-flight auth Promises per
provider. Concurrent callers receive the same Promise and wait for one
refresh instead of each triggering their own, which is safe for both
rotating and non-rotating token schemes.

Fixes modelcontextprotocol#1760.
@lanxevo3 lanxevo3 requested a review from a team as a code owner March 28, 2026 16:18
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 28, 2026

⚠️ No Changeset found

Latest commit: 45c3451

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 28, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1803

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1803

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1803

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1803

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1803

commit: 45c3451

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.

Race condition in auth() causes refresh token invalidation when rotating tokens are used

1 participant