Skip to content

fix: stop a new CIMD authorization from revoking the user's other grants - #433

Closed
matthewhenry1 wants to merge 3 commits into
cloudflare:mainfrom
matthewhenry1:fix/cimd-keep-other-grants
Closed

matthewhenry1 wants to merge 3 commits into
cloudflare:mainfrom
matthewhenry1:fix/cimd-keep-other-grants

Conversation

@matthewhenry1

@matthewhenry1 matthewhenry1 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

For CIMD clients, revokeExistingGrants now defaults to false, so a new sign-in no longer revokes the user's other connections from the same client. Follows #297 / #299. mcp.cloudflare.com applies the same change directly in cloudflare/mcp#442; this PR changes the default for every other server built on the library.

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, all with one CIMD client ID and one https callback.
  • The server can't tell those sign-ins apart: fix(cimd): scope default grant revocation to the authorizing redirect URI #299 limits default CIMD revocation to grants with the same redirect URI, but every connection shares it, so Agent B's sign-in revokes Agent A's grant.
  • 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 user survives.
  • An unfinished sign-in still triggers it: the revoke runs when the user approves, before the client exchanges the code.
  • Redirect URIs don't identify installations: the sweep only revokes when the match could be another live connection.
    • Hosted client, one https callback: every connection shares it.
    • Fixed loopback port or private-use scheme (com.example.app:/callback): the same on every device.
    • Ephemeral loopback port: different every time, so the same installation never matches and nothing is pruned.

Change

Tradeoffs

  • Re-authorizing no longer prunes the earlier grant. It lasts until refreshTokenTTL runs out or the client revokes it (RFC 7009). Apps that relied on pruning can pass revokeExistingGrants: true or call revokeGrant().
  • An earlier grant keeps its original scopes, so an older, broader grant can outlive a narrower new one.
  • Grants already revoked aren't restored.
  • This follows fix(cimd): scope default grant revocation to the authorizing redirect URI #299's own reasoning: over-revocation is the bug, and accumulating grants is "the lesser evil vs. cross-device logout".

Spec alignment

  • Shared client IDs are intended: a CIMD document "is intended to consistently describe one client" (CIMD §1, §4.5).
  • Redirect URIs don't identify installations: servers "MUST allow any port to be specified at the time of the request" for loopback redirects (RFC 8252 §7.3), and a private-use scheme is the same in every copy of the app (§7.1).
  • Separate client URLs have a cost: §4.5 allows them, but each is "a brand new client" to every server, with its own consent (CIMD §10.4).
  • Replacing grants is server policy: no spec requires or forbids it.

Testing

In the "Grant Revocation Scoping" suite of tests/oauth-cimd.test.ts:

  • Changed: "same installation re-authorizing revokes the old grant" is now "keeps the earlier grant when an authorization reuses the same redirect URI", because a fixed loopback port can belong to another device.
  • Changed: the legacy-grant test now passes revokeExistingGrants: true, the only path that revokes CIMD grants. The same-redirect-URI grant is revoked and the legacy grant survives.
  • New: a hosted client with one https callback connects twice. Both access tokens return 200, the first refresh returns 200, and 2 grants remain.
  • New: with revokeExistingGrants: true, only the grant from the same redirect URI is revoked.
  • Unchanged: different loopback ports keep both grants, and a DCR client still has its earlier grant revoked.
  • Checks: reverting src fails the changed and hosted-client tests; ignoring an explicit true fails the opt-in test; letting legacy grants match fails the legacy-grant test. npm run check is clean (tsc, 1191/1191 tests), and the build and Prettier pass.

Release

Patch, like #299: stored data doesn't change, so previously issued tokens still validate. It does change a default, so a minor bump is reasonable too.

A CIMD client_id is shared by every installation and connection of the
client, and so is its redirect URI: a hosted client uses one https
callback for all of its connections, and a fixed loopback port or
private-use scheme is the same on every device. Scoping default
revocation to the redirect URI (cloudflare#299) still revoked another live
connection, whose refresh then failed with 'Grant not found'.

completeAuthorization() now leaves a CIMD client's existing grants
alone. Pre-registered and DCR clients are unchanged.
@changeset-bot

changeset-bot Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0829450

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/workers-oauth-provider Patch

Not sure what this means? Click here to learn what changesets are.

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

Only the default changes: a CIMD client's grants are left alone unless
the application passes revokeExistingGrants: true, which keeps the
0.10.3 behavior of revoking grants from the same redirect URI.
@matthewhenry1
matthewhenry1 marked this pull request as ready for review November 25, 2026 19:02
devin-ai-integration[bot]

This comment was marked as resolved.

The storage section, JSDoc and changeset now say default revocation is off
for any https URL client_id, including one pre-registered in KV with CIMD
fetching off, matching isClientIdMetadataDocumentUrl.

The legacy-grant test now passes revokeExistingGrants: true, the only path
that revokes CIMD grants. It checks that the same-redirect-URI grant is
revoked and the grant without a stored redirectUri survives.

@devin-ai-integration devin-ai-integration Bot 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.

Devin Review found 1 new potential issue.

Devin Review

`completeAuthorization()` stores a new grant and, by default, revokes existing grants for the same user, client, and resource after the new grant is safely stored. A grant for another registered resource is a separate authorization and is not revoked. Set `revokeExistingGrants: false` only when the application intentionally allows concurrent grants within the same resource.

For Client ID Metadata Document clients, whose client_id is the metadata URL shared by every installation, default revocation is additionally scoped to grants created from the same redirect URI, so one installation's re-authorization does not revoke another's. Grants created before the redirect URI was recorded are never auto-revoked by CIMD clients.
For clients whose client_id is an https URL, default revocation is off. That covers Client ID Metadata Document clients and a URL client_id pre-registered in KV with CIMD fetching off. The client_id is shared by every installation and connection, and the redirect URI doesn't tell them apart either: a hosted client uses one https callback for all of its connections, and a fixed loopback port or private-use scheme is the same on every device. So by default one authorization never revokes another's grant; grants end when they expire or when the client revokes them (RFC 7009). Passing `revokeExistingGrants: true` revokes only the CIMD client's grants created from the same redirect URI. Grants created before the redirect URI was recorded are never revoked this way.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔍 Pathless HTTPS client IDs still revoke grants

A pre-registered https://client.example.com still revokes matching grants by default. The CIMD URL check requires an explicit path, which the documented exception omits.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@sam-huckaby

Copy link
Copy Markdown
Collaborator

Going to merge #434 which modifies this a little bit. Thank you for the quick submit though!

sam-huckaby pushed a commit 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.

2 participants