Repository navigation
fix(oauth): Match resource indicators that differ only by query or trailing slash - #2410
Conversation
…ailing slash
Protected resource metadata echoes the query of the URL a client dials,
so clients disagree about whether `?utm_source=plugin`, `?codemode=false`,
or a trailing slash belongs in the RFC 8707 resource. A login in one form
followed by a code exchange or refresh in the other failed with
`invalid_target` ("Requested resource was not included in the
authorization request"), and tokens bound to one form failed on `/mcp`
with "Token audience does not match resource server".
Add a `resourceMatchCanonical` option to the workers-oauth-provider patch
that compares scheme, host, port, and path for both checks, and enable it.
The `/mcp/{org}/{project}` path binding is unchanged.
Co-Authored-By: David Cramer <23610+dcramer@users.noreply.github.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Why is this an optional behavior? Help me understand what's broken today. Are we doing something wrong against the spec? Is it a problem with the upstream provider that we're supposed to be bumping because it fixes it? |
|
Good questions. Here's what I found: What's broken today Production advertises the URL the client dialed, query included, as the resource identifier: The client stores the grant under whichever form it sends at Are we doing something wrong against the spec? Partly, yes. RFC 8707 §2 says a resource "SHOULD NOT include a query component" unless the query is what scopes the resource. Would bumping the upstream provider fix it? Not as a drop-in. We're on 0.10.3, and upstream is at 1.2.4. 1.x addresses this with a different model:
So 1.x would fix the query variant for new sign-ins if we set
Why is it an option? Only so our patch keeps the library's default intact and the deviation shows up in our config, not buried in a diff of My take: this PR stops the failures for existing sessions now, with no forced re-auth. The upgrade-aligned fix is a separate migration: bump to 1.2.4 with canonical |
|
So the idea is we'd basically no-op the query param and support both, or we'd stop using the query param, or what? |
|
Both, in two steps:
We wouldn't stop using the query params themselves. Step 2 alone wouldn't fix things: grants that already carry the query, and clients that build the resource from their configured URL rather than from our metadata, still need step 1. One thing to check before step 2: whether any client rejects metadata whose I can open step 2 as a separate PR if you want it. |
MCP clients fail to sign in or refresh with
invalid_target("Requested resource was not included in the authorization request") when the RFC 8707resourceat login and at the token request differ only by a query string or a trailing slash. Tokens bound to one form also fail on/mcpwith "Token audience does not match resource server".Why it happens. Our protected resource metadata echoes the query of the URL the client dials (
/mcp?utm_source=plugin→"resource": "https://mcp.sentry.dev/mcp?utm_source=plugin"). Clients disagree about whether that query belongs in the resource. For example, opencode now drops it on purpose so that a refresh "names the same resource the login did" (anomalyco/opencodepackages/core/src/mcp/oauth.ts). workers-oauth-provider 0.10.3 compares the token-request resource to the grant with===, and compares the token audience to the request URL including the query. So a grant from/mcp?utm_source=pluginrejects a refresh for/mcp, and the other way around.Production impact (sentry/mcp-server logs, last 14 days):
invalid_targettoken responses. The baseline is 40–90 a day, rising to 72 and 88 on Nov 17–18.?utm_source=plugin), Codex, and clients that send no user agent. The affected users dial/mcpwith?utm_source=pluginor?codemode=false.grok-cli/*traffic on/mcp?utm_source=plugin), so it hits the same path.Fix. The existing provider patch gets a
resourceMatchCanonicaloption, enabled inindex.ts. With it, the token endpoint's requested-vs-granted check and the/mcpaudience check compare scheme, host, port, and path, ignoring the query and trailing slashes.What stays the same. The org/project path binding is unchanged: a
/mcp/sentrygrant still rejects a/mcp/othertoken request, and its token still gets a 401 on/mcp/other. The query on/mcp(skills,codemode,utm_source) is not an authorization boundary for OAuth tokens, because granted skills come from the grant props. Anyone upgrading the provider has to port the patch;docs/cloudflare/oauth-architecture.mdsays how.Related: #1247. This PR does not fix that issue: a percent-encoded query (
/mcp%3Futm_source=plugin) is still rejected when the client starts authorization.via @dcramer.
--
View Session [Sentry]