Skip to content

fix: Include pathname in resourceServer for RFC 8707 path-aware audience validation#109

Merged
mattzcarey merged 5 commits intocloudflare:mainfrom
bokhi:fix/rfc8707-path-aware-audience-validation
Feb 11, 2026
Merged

fix: Include pathname in resourceServer for RFC 8707 path-aware audience validation#109
mattzcarey merged 5 commits intocloudflare:mainfrom
bokhi:fix/rfc8707-path-aware-audience-validation

Conversation

@bokhi
Copy link
Contributor

@bokhi bokhi commented Nov 10, 2025

Fixes #108

Problem

When using RFC 8707 Resource Indicators with path components (e.g., resource=https://example.com/api), token validation fails with:

invalid_token: Token audience does not match resource server

This breaks OAuth flows for services like ChatGPT custom connectors.

Root Cause

In handleApiRequest, the resourceServer is computed using only the origin:

const resourceServer = `${requestUrl.protocol}//${requestUrl.host}`;

However, RFC 8707 recommends using full URLs with paths for resource indicators. Since audienceMatches performs strict equality (===):

  • Token audience: https://example.com/api (from authorization request's resource parameter)
  • Resource server: https://example.com (computed from request URL)
  • Result: mismatch → validation fails

Solution

This PR updates the resourceServer computation in both internal and external token validation flows to include the pathname component:

const resourceServer = `${requestUrl.protocol}//${requestUrl.host}${requestUrl.pathname}`;

Changes

  • src/oauth-provider.ts line 2194: Include pathname in resourceServer (internal token validation)
  • src/oauth-provider.ts line 2228: Include pathname in resourceServer (external token validation)

Testing

Tested with ChatGPT custom connector:

  • ✅ Authorization flow with resource=https://server/mcp
  • ✅ Token issued with aud: "https://server/mcp"
  • ✅ API request to https://server/mcp succeeds
  • ✅ Audience validation passes

References

…nce validation

Fixes cloudflare#108

When using RFC 8707 Resource Indicators with path components (e.g.,
resource=https://example.com/api), token validation was failing because
resourceServer was computed using only the origin (protocol + host),
while the token audience contained the full URL with path.

This commit updates the resourceServer computation in both internal and
external token validation flows to include the pathname component:

  const resourceServer = `${requestUrl.protocol}//${requestUrl.host}${requestUrl.pathname}`;

This ensures that audience validation succeeds when the resource
indicator includes the path component, as recommended by RFC 8707.
@changeset-bot
Copy link

changeset-bot bot commented Nov 10, 2025

🦋 Changeset detected

Latest commit: 3810a31

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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 11, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/workers-oauth-provider/@cloudflare/workers-oauth-provider@109

commit: 3810a31

- Modify audienceMatches() to support both origin-only and path-aware audiences
- Origin-only audiences (e.g., https://example.com) match by origin for backward compatibility
- Path-aware audiences (e.g., https://example.com/api) require exact match per RFC 8707
- Add 3 test cases for path-aware audience validation
- Update TestApiHandler to handle all /api/* paths

This fixes the CI failures in PR cloudflare#109 while maintaining backward compatibility
with existing code that uses origin-only resource indicators.

Fixes cloudflare#108
@mattzcarey
Copy link
Contributor

Looking at this :) thanks for your contribution.

@mattzcarey mattzcarey force-pushed the fix/rfc8707-path-aware-audience-validation branch from 3a53410 to 7609828 Compare January 8, 2026 08:54
nathan-bw pushed a commit to nathan-bw/mcp-server-google-oauth that referenced this pull request Jan 29, 2026
Patches @cloudflare/workers-oauth-provider to include pathname in
resourceServer computation, fixing "Token audience does not match
resource server" error when connecting from the Cloudflare MCP portal.

The bug occurs because RFC 8707 Resource Indicators include the path
(e.g., /mcp), but the library only compared against the origin.

Upstream fix: cloudflare/workers-oauth-provider#109

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The previous exact-path matching was too strict — a token with audience "https://example.com/api" would fail for requests to sub-paths like "/api/users". This made the fix unusable for real APIs (including MCP servers and ChatGPT connectors) that have multiple endpoints under a base path. Replace the dual exact-match/origin-match heuristic with proper path-boundary prefix matching:

- Origin-only audiences still match any path (backward compat)
- Path-aware audiences match the exact path and any sub-path on a path boundary ("/api" matches "/api/foo" but not "/api-v2")
- Origins are compared via URL normalization (case-insensitive host)
@mattzcarey mattzcarey force-pushed the fix/rfc8707-path-aware-audience-validation branch from 6b49b47 to 531b372 Compare February 11, 2026 12:04
@mattzcarey mattzcarey enabled auto-merge (squash) February 11, 2026 12:06
@mattzcarey mattzcarey merged commit 9f118f3 into cloudflare:main Feb 11, 2026
4 checks passed
@github-actions github-actions bot mentioned this pull request Feb 11, 2026
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.

Audience validation fails for RFC 8707 resource indicators with path

2 participants