Skip to content

fix(auth): a new CIMD authorization keeps the user's other grants - #441

Closed
matthewhenry-cf wants to merge 1 commit into
cloudflare:mainfrom
matthewhenry-cf:fix/cimd-coexisting-grants
Closed

matthewhenry-cf wants to merge 1 commit into
cloudflare:mainfrom
matthewhenry-cf:fix/cimd-coexisting-grants

Conversation

@matthewhenry-cf

@matthewhenry-cf matthewhenry-cf commented Nov 25, 2026 •

Copy link
Copy Markdown

Problem

A CIMD client ID is the client's metadata document URL, so every install of the client shares it. A hosted client also shares one redirect URI across all of its connections. When completeAuthorization() replaces existing grants (the default revokeExistingGrants: true), workers-oauth-provider revokes every grant for that user that matches client ID, redirect URI and resource. For a hosted CIMD client, that's all of the user's other live connections (oauth-provider.ts#L6750-L6767).

Result: connecting a second time, for example from a second agent, revokes the first connection's grant. Its next refresh fails with invalid_grant: Grant not found, and reconnecting it then revokes the other one. The revocation also runs before the client exchanges the new code (#L6829-L6836), so even an abandoned authorization breaks the existing connection.

Change

Pass revokeExistingGrants: false for CIMD clients. Registered (DCR) clients get a client ID per install, so they keep replacement.

Tradeoffs

  • Earlier CIMD grants are no longer revoked when the user authorizes again. They stay valid until the 30-day refreshTokenTTL runs out or the client revokes them (RFC 7009). The fixed TTL caps how many build up.
  • Re-authorizing to add scopes leaves the older, narrower grant alive until it expires.
  • This applies to every CIMD client, desktop ones included.
  • Grants already revoked aren't restored, so affected connections need one reconnect.

Tests

New test in tests/auth/cimd.test.ts: two full CIMD authorizations for the same user keep 2 grants, and both refresh tokens return 200. Before this change it fails because only 1 grant remains. The full suite passes (894 tests).

Spec alignment

The specs neither require nor forbid replacing earlier grants on re-authorization. It's authorization-server policy. This change brings that policy in line with how CIMD identifies clients.

Question Spec Effect
What does a CIMD client_id identify? CIMD §1: best suited to clients with "an established, stable, and publicly accessible web presence, such as a web service". §4.5: a document "is intended to consistently describe one client". The client_id identifies the application, shared by every user and connection. It is not an installation.
Is the redirect URI per installation? CIMD §4.2 requires redirect URIs to be registered and matched exactly. It never makes them per installation. A hosted client legitimately uses one callback for every connection.
Must the server keep only one grant per user and client? Nothing in MCP authorization, OAuth 2.1 or CIMD requires it. CIMD §10.12 only says persisted authorization state must be scoped "at least" to client ID + resource. Replacement is allowed but optional. Allowing coexisting grants is equally compliant.
Can the client avoid the collision itself? CIMD §4.5: a client "MUST NOT" derive its document from external input such as the URL path or query. §10.4: a new client_id is "a brand new client". No. Per-connection documents are non-compliant, and rotating client IDs splits consent and branding across many identities. The server has to account for hosted clients.
Is Grant not found itself a violation? OAuth 2.1 §4.3: the server may revoke "for any reason", and the client "has to handle the case of a refresh token expiring at an arbitrary time". No, but revoking a live connection because the same user connected again elsewhere is a poor policy for hosted CIMD clients.

MCP now prefers CIMD and deprecates DCR as of 2026-07-28 (client registration). So more clients will share one client_id across many users and connections, and this policy will affect more of them. It isn't specific to one client: the same matching applies to any hosted agent platform where one user holds several connections. DCR clients still get one client_id per registration, so they keep replacement.

Follow-up worth considering: the same default lives in workers-oauth-provider, so other MCP servers built on it with CIMD enabled have the same behavior.

A CIMD client ID and its redirect URI are shared by every connection a hosted
client holds (one metadata URL, one callback), so the provider's default grant
replacement revoked a user's other live connections whenever they authorized
again. Their next refresh failed with 'Grant not found'. Registered (DCR)
clients get a client ID per install and keep replacement.
@matthewhenry-cf
matthewhenry-cf deleted the fix/cimd-coexisting-grants branch November 25, 2026 15:32
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.

1 participant