Skip to content

fix(oauth): refresh an expired token behind an insufficient_scope challenge - #2738

Merged
cliffhall merged 2 commits into
v2/mainfrom
v2/fix/2724-insufficient-scope-expired-token
Oct 20, 2026
Merged

cliffhall merged 2 commits into
v2/mainfrom
v2/fix/2724-insufficient-scope-expired-token

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #2724

Problem

Some resource servers (the reporter's case is an AWS Bedrock AgentCore Gateway) answer an expired access token with 403 insufficient_scope instead of 401 invalid_token. OAuthManager.checkAuthChallengeSatisfied() tested only scope coverage for that reason. So it reported the challenge satisfied, handleAuthChallenge told the transport to retry, the same dead token was replayed, and startWithRecovery gave up after one identical 403. The refresh token was never tried. This is the gap #2051 closed for token_expired, never carried over to insufficient_scope.

Change

Follows the approach agreed in the issue thread, with one addition (the external post-grant callers, below).

  • insufficient_scope now also requires a provably unexpired token (isAccessTokenProvablyUnexpired, the same JWT exp test token_expired uses). An expired JWT or an opaque token with covered scope now returns false, so handleAuthChallenge reaches mcpAuth. Scope is covered, so this is an ordinary refresh (forceReauthorization: false), not a forced re-auth.
  • Post-grant callers pass { scopeOnly: true } (new CheckAuthChallengeSatisfiedOptions). A token that was just minted is live because the grant produced it, and a freshly issued opaque token has no readable exp, so the liveness test would turn a successful refresh into a failure. That applies to:
    • the three legs inside handleAuthChallenge the thread identified (standard refresh, EMA silent re-mint, tryForceReauthorizationForStepUp)
    • and two external callers the thread didn't cover: the CLI/TUI runner (runner-interactive-oauth.ts) after its OAuth callback, and the web client's OAuth-resume check in useOAuthRecovery. Without this, the gate's CLI oauth-interactive integration test failed, because a real step-up completed and was then rejected as "required scopes were not granted".
  • EMA: no step-up consent prompt when nothing is broadened. With the gate in place, an expired EMA token with covered scope would otherwise get step_up_confirm, a modal asking the user to approve a broader scope when nothing broader is requested. If storage already covers the challenge's scope, it now goes straight to silent re-mint. (Side effect 2 in the thread.)
  • specification/v2_auth_mid_session.md: the insufficient_scope bullet and both strategy tables are updated. The standard-OAuth "Silent: N/A" cell the thread called stale is now filled in.

Accepted trade-off (same as #2051, agreed in the thread): for opaque-token servers, insufficient_scope loses the free short-circuit, including the in-mutex re-check after a concurrent step-up, and costs one extra silent refresh.

Out of scope (per the thread): a same-token-retry fallback in RemoteClientTransport.startWithRecovery().

Tests

  • 4 new oauthManager tests (insufficient_scope challenge retry doesn't detect expired access token, gives up after one identical retry #2724 block), all failing on the unfixed source: expired JWT + covered scope is not satisfied; opaque + covered scope is not satisfied; handleAuthChallenge refreshes rather than replaying and accepts a refreshed opaque token; EMA re-mints without step_up_confirm.
  • 3 existing tests that relied on an opaque token being short-circuited now use live JWTs.
  • The runner and web-resume tests now check that { scopeOnly: true } is passed.
  • npm run local:gate is green. oauthManager.ts coverage: 99.66 / 99.07 / 100 / 99.65.

No UI change, so no screenshots.

🤖 Generated with Claude Code

…llenge

Some resource servers answer an expired access token with 403
insufficient_scope. checkAuthChallengeSatisfied() tested only scope for
that reason, so it reported the challenge satisfied, the transport
replayed the same dead token, and recovery gave up without trying the
refresh token: the gap #2051 closed for token_expired.

insufficient_scope now also requires a provably unexpired token. Callers
that have just completed a grant pass { scopeOnly: true }: the refresh,
re-mint and forced re-authorization legs in handleAuthChallenge, the
runner after its callback, and the web client on OAuth resume. A freshly
issued opaque token has no readable exp, so the liveness test would
reject it. An EMA challenge whose scope storage already covers skips the
step-up confirm, since re-minting it broadens nothing.

Closes #2724

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall cliffhall added the v2 Issues and PRs for v2 label Oct 20, 2026
@cliffhall
cliffhall requested a balanced review from Copilot October 20, 2026 03:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Tokens with omitted scope still bypass valid refresh tokens despite sufficient persisted scope.

1 open issue
What changed in this PR

Updates shared OAuth recovery for servers that report expired tokens as insufficient_scope, addressing #2724.

Changes:

  • Requires token-liveness evidence before skipping recovery.
  • Adds scope-only checks after grants and avoids unnecessary EMA consent prompts.
  • Updates regression tests and authentication documentation.
File Description
specification/​v2_auth_mid_session.md Documents revised recovery behavior.
core/​mcp/​oauthManager.ts Adds liveness checks and adjusts recovery.
core/​mcp/​inspectorClient.ts Forwards challenge-check options.
core/​auth/​node/​runner-interactive-oauth.ts Uses scope-only checks after callbacks.
core/​auth/​challenge.ts Defines scope-only check options.
clients/​web/​src/​test/​core/​mcp/​oauthManager.test.ts Adds expired-token recovery regressions.
clients/​web/​src/​test/​core/​auth/​runner-interactive-oauth.test.ts Checks runner option forwarding.
clients/​web/​src/​hooks/​useOAuthRecovery.ts Uses scope-only checks after OAuth resume.
clients/​web/​src/​hooks/​useOAuthRecovery.test.tsx Checks resume option forwarding.

Review effort: Balanced

Comment thread core/mcp/oauthManager.ts Resolved
A token response may omit `scope` when it equals the request. The
forceReauthorization decision compared the challenge only with the
token's own scope, so a covered challenge on an expired token with no
token scope forced interactive authorization instead of using the
refresh token. Use resolveEffectiveGrantedScope, as
checkAuthChallengeSatisfied does.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: cliffhall <[email protected]>
@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 1: 1 finding, fixed.

  • Use persisted scope when token scope is omitted (core/mcp/oauthManager.ts): fixed in 0934e38. forceReauthorization now judges coverage with resolveEffectiveGrantedScope, so an expired token whose response omitted scope refreshes instead of forcing interactive auth. The regression test covers both token shapes.

No suppressed comments. Requesting round 2.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused recovery changes have targeted regression coverage and no unresolved blocking findings.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review loop closed: round 2 was clean (no inline comments, no suppressed findings; it reports the round-1 finding resolved). Stopping on the first clean round.

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

Labels

v2 Issues and PRs for v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

insufficient_scope challenge retry doesn't detect expired access token, gives up after one identical retry

2 participants