Skip to content

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

Merged
mattzcarey merged 1 commit into
cloudflare:mainfrom
matthewhenry1:fix/cimd-coexisting-grants
Nov 25, 2026
Merged

mattzcarey merged 1 commit into
cloudflare:mainfrom
matthewhenry1:fix/cimd-coexisting-grants

Conversation

@matthewhenry1

@matthewhenry1 matthewhenry1 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Related: cloudflare/workers-oauth-provider#433 makes this the library default. This PR does not wait on it; remove this override once mcp.cloudflare.com upgrades to a release that includes it.

Stops one cause of invalid_grant: Grant not found on mcp.cloudflare.com: a new sign-in from a hosted CIMD client revokes the user's other connections from that client. Works with the current library (1.2.3) and needs no client change.

Problem

  • Some agent platforms ask for one sign-in per agent: users sign in to the same MCP server separately for each agent they configure.
  • mcp.cloudflare.com can't tell those sign-ins apart: they share the user's Cloudflare login and the platform's CIMD client ID and callback, so Agent B's sign-in revokes Agent A's grant (grant matching).
  • What the user sees: Agent A worked, then fails on its next use with invalid_grant: Grant not found, and reconnecting A breaks B. Only one connection per Cloudflare login survives.
  • An unfinished sign-in still triggers it: the revoke runs when the user approves, before the client exchanges the code (#L6829-L6836).
  • The library's redirect-URI scoping doesn't help: fix(cimd): scope default grant revocation to the authorizing redirect URI workers-oauth-provider#299 limits CIMD revocation to grants with the same redirect URI, but a hosted platform uses one callback for every connection.

Change

Tradeoffs

  • Earlier CIMD grants are no longer revoked on a new sign-in. They last until the 30-day refreshTokenTTL runs out or the client revokes them (RFC 7009).
  • An earlier grant keeps its original scopes, so an older, broader grant can outlive a narrower new one.
  • Grants already revoked aren't restored. Affected connections reconnect once.

Testing

  • New test (cimd.test.ts#L227-L242): two full CIMD authorizations by the same user keep 2 grants, and both refresh tokens return 200. Without the change, only 1 grant remains.
  • Full suite: 894 tests pass.
  • Not covered: whether Cloudflare's upstream OAuth keeps both connections' upstream refresh tokens valid. Confirm in staging with two connections.

Library follow-up

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.
@eileen-notion

Copy link
Copy Markdown

+1 Just confirming that I repro-ed the bug that revokes access when a 2nd connection is established.

  1. Connect Cloudflare MCP from Custom Agent A
  2. Connect Cloudflare MCP from Custom Agent B
  3. Go back to Custom Agent A and see that Cloudflare now says "Needs login"

@mattzcarey mattzcarey left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed locally: the new test fails without the override, and typecheck, lint and the suite pass. cloudflare/workers-oauth-provider#434 makes this the library default; drop the override when we bump to it.

@mattzcarey
mattzcarey merged commit a903b53 into cloudflare:main Nov 25, 2026
2 checks passed
sam-huckaby pushed a commit to cloudflare/workers-oauth-provider that referenced this pull request Nov 25, 2026
A new authorization from a Client ID Metadata Document client no longer
revokes the user's other grants from that client. `revokeExistingGrants`
now defaults to `false` for a client resolved from a metadata document,
and `true` means the same for every client: revoke the user's other
grants for this client and resource. Builds on #433 by @matthewhenry1
and replaces it; released as a minor because the meaning of `true`
changes for CIMD clients.

A CIMD `client_id` is shared by every installation and connection of the
client, and nothing in the request tells them apart:
- #299 limited default revocation to grants from the same redirect URI,
but a hosted client uses one https callback for all its connections, so
signing in a second agent revoked the first, which then failed with
`invalid_grant: Grant not found`
- #433 turned the default off but kept the redirect URI rule under
`true`, so one option meant two things depending on the client ID
- a URL `client_id` stored in KV with `clientIdMetadataDocumentEnabled`
off is a registered client, so it keeps the default `true`
- with the redirect URI rule gone, grant keys stop mirroring
`redirectUri` into KV metadata, so a long redirect URI no longer pushes
a grant over the 1,024-byte limit; the grant record and
`listUserGrants()` still carry it

```ts
// Default: a CIMD client's earlier grants are kept.
await env.OAUTH_PROVIDER.completeAuthorization({ request, userId, scope, props, metadata });

// One grant per user and resource, for any client. For a CIMD client this signs out its other installations.
await env.OAUTH_PROVIDER.completeAuthorization({ request, userId, scope, props, metadata, revokeExistingGrants: true });
```

`npm run check` ran. I also ran cloudflare/mcp's CIMD tests against this
build without the override from cloudflare/mcp#442: two sign-ins from
one CIMD client keep both grants.

---------

Co-authored-by: matthewhenry1 <31288868+matthewhenry1@users.noreply.github.com>
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.

3 participants