Skip to content

feat(oauthserver): support Client ID Metadata Documents - #2895

Open
melcheikh wants to merge 2 commits into
supabase:masterfrom
melcheikh:feat/oauth-client-id-metadata-documents
Open

melcheikh wants to merge 2 commits into
supabase:masterfrom
melcheikh:feat/oauth-client-id-metadata-documents

Conversation

@melcheikh

Copy link
Copy Markdown
Contributor

What

Closes #2850.

Adds support for Client ID Metadata Documents (draft-02), which the MCP authorization spec uses so clients don't need to register. The client uses an HTTPS URL as its client_id, and the server reads the client's metadata from that URL.

Today MCP clients go through Dynamic Client Registration on every connection, so:

  • each reconnect creates another oauth_clients row and users see many near-identical apps;
  • the per-IP registration rate limit is shared by everyone connecting from the same platform (claude.ai, chatgpt.com).

With this change, one URL maps to one client, so consents and grants stay under a single app.

How it works

Off by default:

GOTRUE_OAUTH_SERVER_ALLOW_CLIENT_ID_METADATA_DOCUMENTS=true
# optional, empty allows any public host
GOTRUE_OAUTH_SERVER_CLIENT_ID_METADATA_DOCUMENT_ALLOWED_HOSTS=claude.ai,chatgpt.com
  • /oauth/authorize accepts a URL client_id. The document is fetched and validated, and a client with registration_type = metadata_document is created or updated from it (name, URIs, redirect URIs, grant types). The redirect_uri must exactly match one in the document. Errors are returned as JSON (not redirected), since the redirect URI can't be trusted yet.
  • /oauth/token accepts the URL client_id for clients created this way. The document is not fetched again there.
  • Tokens: the ID token aud/client_id and the access token client_id claim use the URL, the identifier the client knows itself by. Registered clients keep their UUID.
  • Consent screen: GET /oauth/authorizations/{id} returns client.client_id_url, so the UI can show its host as the spec recommends (the metadata is self-asserted).
  • Discovery: /.well-known/oauth-authorization-server advertises client_id_metadata_document_supported: true when enabled.

Spec requirements implemented

  • Client ID URL: https, has a path, no user info, no dot segments (including percent-encoded ones), no fragment, no query.
  • Document: client_id must match the URL with simple string comparison; client_secret / client_secret_expires_at are rejected; token_endpoint_auth_method must be none (shared secrets are forbidden by the spec and private_key_jwt isn't supported by this server); redirect_uris required and validated like DCR.
  • Fetching: HTTP 200 only, redirects are not followed, 5 KB read limit, 5 s timeout, application/json or application/*+json.
  • SSRF: connections to special-use addresses (RFC 6890: loopback, private, link-local, CGNAT, documentation ranges, NAT64, IPv4-mapped…) are refused in the dialer, on the address actually dialed, so DNS rebinding can't bypass it. Proxies are not used for these requests for the same reason.
  • Caching: valid documents are cached in memory following Cache-Control (max-age, no-store, no-cache), default 5 minutes, at most 24 hours, bounded to 1000 entries. Errors and invalid documents are never cached.

Migrations

  • 20261031100000: adds metadata_document to oauth_registration_type.
  • 20261031100001: adds oauth_clients.client_id_url (≤ 2048 chars) with a unique index on active rows.

openapi.yaml and the generated admin client are updated (registration_type enum, client_id_url).

Not included / open questions

Tests

  • internal/api/oauthserver/client_metadata_test.go: URL validation, special-use address detection, Cache-Control handling, fetcher against a TLS test server (caching, expiry, no-store, errors not cached, redirects not followed, every invalid document case), the default client refusing loopback, authorize creating a single client per URL and applying document changes, rejected documents, allow list, flag disabled, FindClientByClientID, and code exchange + refresh issuing the URL as client_id.
  • internal/api/oauth_client_id_metadata_test.go: discovery flag and /oauth/token client authentication with a URL client_id through the router.
  • internal/tokens: ID token aud/client_id use the client identifier.

go test ./internal/api/oauthserver/ ./internal/models/ ./internal/tokens/ ./internal/conf/ ./internal/api/ pass locally against Postgres 16, except tests that need outbound network/DNS, which fail the same way on master in my environment.

🤖 Generated with Claude Code

https://claude.ai/code/session_0187GfsUpzmeh8pDYi3eBwEq

MCP clients register through Dynamic Client Registration on every
connection, so each reconnect creates another client and the per-IP
registration rate limit is shared by everyone connecting from the same
platform. Client ID Metadata Documents
(draft-ietf-oauth-client-id-metadata-document-02, adopted by the MCP
authorization spec) let a client use an HTTPS URL as its client_id
instead, and the server reads the client's metadata from that URL.

Behind GOTRUE_OAUTH_SERVER_ALLOW_CLIENT_ID_METADATA_DOCUMENTS (off by
default), with an optional host allow list in
GOTRUE_OAUTH_SERVER_CLIENT_ID_METADATA_DOCUMENT_ALLOWED_HOSTS:

- /oauth/authorize accepts a URL client_id. The document is fetched,
  validated and used to create or update a single client per URL
  (registration_type metadata_document), so consents and grants stay
  under one app.
- Fetching follows the spec: https only, no redirects, 5 KB limit,
  5 s timeout, JSON content type, client_id must match the URL, no
  client_secret and only token_endpoint_auth_method "none". Connections
  to special-use IP addresses (RFC 6890) are refused on the dialed
  address, so DNS rebinding can't bypass the check.
- Valid documents are cached following Cache-Control (default 5 min,
  max 24 h); errors and invalid documents are never cached.
- /oauth/token accepts the URL client_id for clients created this way.
- ID token aud/client_id and the access token client_id claim use the
  URL, the identifier the client knows itself by.
- Authorization details return client_id_url so consent screens can
  show its host, and the server metadata advertises
  client_id_metadata_document_supported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0187GfsUpzmeh8pDYi3eBwEq
@melcheikh
melcheikh requested a review from a team as a code owner October 31, 2026 00:54
netip reports fec0::/10 (site-local, RFC 3879) and ::/96
(IPv4-compatible, RFC 4291) addresses as global unicast, so the client
metadata document fetcher could connect to them. Add both ranges to the
special-use list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0187GfsUpzmeh8pDYi3eBwEq

This branch has not been deployed

No deployments
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.

OAuth 2.1 server: support Client ID Metadata Documents (URL client_id) for MCP clients

1 participant