core icon indicating copy to clipboard operation
core copied to clipboard

Auth advertises client_id_metadata_document_supported but never fetches the document — "Invalid redirect URI" for CIMD clients with cross-origin redirect_uri (Gemini Spark)

Open kingpanther13 opened this issue 4 months ago • 2 comments

The problem

The RFC 8414 metadata served at /.well-known/oauth-authorization-server advertises client_id_metadata_document_supported: true (added in #166220). Per the CIMD spec (draft-ietf-oauth-client-id-metadata-document, adopted by MCP), a client that sees this flag presents a URL-shaped client_id and expects the authorization server to fetch that URL — a JSON document containing redirect_uris — to validate its redirect.

Home Assistant never fetches the document. indieauth.verify_redirect_uri() accepts a redirect_uri only if it is (a) same scheme+netloc as the client_id, (b) on the hardcoded companion-app allowlist, or (c) published as an HTML <link rel="redirect_uri"> tag at the client_id URL (fetch_redirect_uris() runs an HTML parser only). A JSON CIMD document yields nothing, so any CIMD client whose redirect_uri is not same-origin with its client_id is rejected: login succeeds, then login_flow.py returns 403 "Invalid redirect URI" (source).

Claude.ai and ChatGPT MCP connectors work only by coincidence — their client_id and redirect_uri are same-origin, so check (a) passes and the missing fetch never matters. Google Gemini Spark's "custom connected apps" (MCP) use client_id = https://accountlinking.google.com/clientidmetadata/... with a redirect_uri on a different Google host, which the CIMD spec permits — so every Gemini Spark → Home Assistant OAuth attempt fails after login with "Invalid redirect URI".

Steps to reproduce:

  1. Expose an MCP server on a Home Assistant instance (e.g. via Nabu Casa).
  2. In the Gemini web app, add it as a custom connected app for Gemini Spark.
  3. Gemini Spark discovers HA's OAuth metadata, redirects to /auth/authorize with a CIMD client_id.
  4. Log in → "Error: Invalid redirect URI".

Suggested fix: in fetch_redirect_uris(), when the client_id URL responds with application/json, parse it as a CIMD document and exact-match the request's redirect_uri against its redirect_uris array (keeping the existing HTML link-tag path for IndieAuth clients). Alternatively, stop advertising client_id_metadata_document_supported until it is implemented.

What version of Home Assistant Core has the issue?

core-2026.7.2 (the code paths above are unchanged on dev)

What was the last working version of Home Assistant Core?

None — CIMD fetching has never been implemented; the advertisement flag was added in #166220.

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Auth

Link to integration documentation on our website

https://www.home-assistant.io/integrations/auth/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

Frontend shows "Error: Invalid redirect URI" after successful login.
Client ID observed in the authorize URL (reported downstream):
https://accountlinking.google.com/clientidmetadata/user_bound_provider/user_bound_custom-mcp-<id>-<instance>_ui_nabu_casa

Additional information

Downstream report with full analysis: homeassistant-ai/ha-mcp#1787. The comment added in #166220 acknowledges the gap: "The metadata document is not actually fetched as IndieAuth doesn't require it." That was safe for same-origin clients (claude.ai, ChatGPT — see #153820), but Google Gemini Spark is the first major MCP client to rely on the advertised CIMD behavior with a cross-origin redirect_uri.

kingpanther13 avatar Jul 11 '26 15:07 kingpanther13

Hey there @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (auth) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of auth can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign auth Removes the current integration label and assignees on the issue, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the issue.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the issue.

(message by CodeOwnersMention)


@kingpanther13 Thanks for reporting this issue!

Before we dive in, please make sure this isn't a duplicate by searching through existing issues. Also check recently closed issues, as your problem might already be fixed but not yet released.

https://github.com/home-assistant/core/issues?q=%20label%3A%22integration%3A%20auth%22%20 (message by IssueContext)


auth documentation auth source (message by IssueLinks)

home-assistant[bot] avatar Jul 11 '26 15:07 home-assistant[bot]

Reproduced on core-2026.7.1 (HAOS) with no Gemini involvement - the only preconditions are a URL-shaped client_id whose document is JSON (CIMD) and a cross-origin redirect_uri.

Step 1: host a CIMD document at any https URL. Live example: https://gist.githubusercontent.com/kingpanther13/da460da95761903e161d74149b7bd048/raw/de666a9338e540aac7a838faea0257853ecf7897/cimd.json - its redirect_uris is ["https://example.com/callback"].

Step 2: open /auth/authorize?response_type=code&client_id=<that URL, encoded>&redirect_uri=https%3A%2F%2Fexample.com%2Fcallback&state=test

Step 3: log in -> "Error: Invalid redirect URI" (403 from login_flow.py), even though the document explicitly authorizes that exact redirect_uri.

Control test isolating the cause: same client_id but redirect_uri=https://gist.githubusercontent.com/callback - same-origin with the client_id and NOT listed in the document - completes login and redirects with a code. The document is never consulted: the CIMD-authorized redirect is rejected while an unauthorized same-origin one is accepted.

kingpanther13 avatar Jul 11 '26 16:07 kingpanther13

Good Morning,

i encountered that issue too and wanted to ask when the fix will be merged, as i don't want to use the legacy mode for claude connection.

FabianClemenz avatar Aug 03 '26 06:08 FabianClemenz