core icon indicating copy to clipboard operation
core copied to clipboard

Support Client ID Metadata Documents when verifying redirect_uri

Open kingpanther13 opened this issue 4 months ago • 2 comments

Proposed change

The authorization server metadata at /.well-known/oauth-authorization-server advertises client_id_metadata_document_supported: true (#166220), but fetch_redirect_uris() only parses HTML <link rel="redirect_uri"> tags. A spec-compliant Client ID Metadata Document client (draft-ietf-oauth-client-id-metadata-document) — whose URL-shaped client_id hosts a JSON document with a redirect_uris array — is rejected with "Invalid redirect URI" after login whenever its redirect_uri is not same-origin with its client_id. Google Gemini Spark's custom connected apps (MCP) hit exactly this: client_id on accountlinking.google.com, redirect_uri on a different Google host.

This change makes fetch_redirect_uris() fall back to parsing the fetched document as a CIMD JSON document when no link tags are found: an object's redirect_uris list of strings is returned as-is for RFC 6749 exact matching (CIMD requires absolute URIs, so no relative resolution). Link tags keep precedence, and the same-origin fast path, companion-app allowlist, 10 kB fetch cap, and error handling are unchanged, so existing IndieAuth clients are unaffected. Malformed documents (non-object JSON, non-list or non-string redirect_uris) yield no redirect URIs, matching today's behavior for pages without link tags.

Behavior was verified against a live instance (core-2026.7.1) with a real hosted CIMD document: unpatched rejects the document-authorized redirect (while accepting a same-origin redirect the document does not authorize); patched accepts exactly the documented URIs. Repro steps are in the linked issue.

Review hardening (Copilot + independent review rounds): the document only counts when the client identifier URL satisfies the draft's rules (https with a path component and no fragment) and the response is a direct 200 (no redirects followed) whose body was read completely within the 10 kB cap — a stranded or capped prefix is never parsed. The document must be strict UTF-8 RFC 8259 JSON (NaN/Infinity rejected), its client_id must round-trip, and every redirect_uris entry must be an absolute, fragment-free URI with a valid port; unparsable values fail closed instead of raising into the login flow (urlparse otherwise turns a crafted entry or requested redirect_uri into an exception). Every rejection is logged at debug level so a failing client is diagnosable from the server log without creating an unauthenticated log-flood vector, including the common "document is valid but the requested redirect_uri is not registered" case. Link-tag scanning is unchanged except that tags without a usable href are skipped instead of resolving to the client_id URL itself.

Type of change

  • [ ] Dependency upgrade
  • [x] Bugfix (non-breaking change which fixes an issue)
  • [ ] New integration (thank you!)
  • [ ] New feature (which adds functionality to an existing integration)
  • [ ] Deprecation (breaking change to happen in the future)
  • [ ] Breaking change (fix/feature causing existing functionality to break)
  • [ ] Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #176282
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request: home-assistant/developers.home-assistant#3244
  • Link to frontend pull request:

Checklist

  • [x] I understand the code I am submitting and can explain how it works.
  • [x] The code change is tested and works locally.
  • [x] Local tests pass. Your PR cannot be merged unless tests pass
  • [x] There is no commented out code in this PR.
  • [x] I have followed the development checklist
  • [x] I have followed the perfect PR recommendations
  • [x] The code has been formatted using Ruff (ruff format homeassistant tests)
  • [x] Tests have been added to verify that the new code works.
  • [x] Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • [ ] The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • [ ] New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • [ ] For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

kingpanther13 avatar Jul 11 '26 16:07 kingpanther13

Hey there @home-assistant/core, mind taking a look at this pull request 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 pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign auth Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @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 pull request.
  • @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 pull request.

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

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:

Learn more about our pull request process.

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

All review feedback is addressed as of 635666aa — the parsing is split into _parse_link_tag_redirect_uris / _parse_metadata_document_redirect_uris, fetch exceptions no longer fall through to body parsing, the read stops at the cap without truncating, and the known IndieAuth deviations are documented in verify_redirect_uri. CI is green and I've resolved every thread that's answered and settled, per the process note.

Two threads are left open deliberately, both waiting on your call:

  1. Same-origin + document validation — same-origin short-circuits before any fetch today; requiring a document there would break the frontend's own login (client_id = instance URL serving the SPA index) and the #153820 connectors. A validate-only-when-present middle path is possible if you want it.
  2. redirect_uris scheme rules — currently any absolute, fragment-free URI is accepted (native clients need private-use schemes and loopback http per RFC 8252). The open question is whether to also reject non-loopback http entries per RFC 9700 §2.6 in this PR — happy to add that if you want it.

kingpanther13 avatar Jul 15 '26 10:07 kingpanther13

Looks good to me. @emontnemery or @balloob can we find an additional core member to do a security focused review? Thank you.

allenporter avatar Jul 18 '26 13:07 allenporter