Skip to content

fix(fe): fall back to the gateway twins when fetching app metadata - #4281

Merged
aterga merged 3 commits into
mainfrom
claude/increase-alternative-origins-limit-3e6kxx
Aug 25, 2026
Merged

aterga merged 3 commits into
mainfrom
claude/increase-alternative-origins-limit-3e6kxx

Conversation

@aterga

@aterga aterga commented Aug 24, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

App metadata (#4221) is fetched from the origin the app's identity is derived for. When that is a canister gateway origin, remapToLegacyDomain has already collapsed it onto ic0.app, so a canister derives one principal whether the user arrived via ic0.app, icp0.io or icp.net.

A canister need not be served on all three. The staging test app answers 400 client_domain_canister_mismatch at .ic0.app while serving the document at .icp0.io. The fetch failed, the failure was silent by design, and the screens fell back to the curated entry, so an app that had published the file correctly still showed as unbranded with nothing to indicate why.

Principal derivation needs that remap. The metadata document does not: it has no bearing on derivation, and the spec is explicit that it carries no principal-derivation security semantics.

Changes

  • appMetadataOrigins(origin) inverts the remap: for https://<sub>.ic0.app it yields that origin followed by the icp0.io and icp.net twins, preserving a .raw label. Any other origin (a custom domain, localhost) yields just itself. All three twins resolve to the same canister, so this widens where the document may be served, never whose document is used.
  • fetchAppMetadata tries those origins in turn and stops at the first that answers.
  • The same-origin rule for the logo now follows the document: it is validated against whichever origin actually served it, so a twin's document may reference a logo on that twin, and one pointing back at the remapped origin is rejected exactly as any other cross-origin reference is.
  • fetchCapped reports the status it saw rather than collapsing every failure to undefined, which is what lets the caller distinguish the two kinds of failure below.
  • Both directions of the gateway-origin mapping now share one regex (GATEWAY_ORIGIN_REGEX in urlUtils): remapToLegacyDomain normalizes onto ic0.app with it and gatewayOriginTwins inverts it, so the set of gateway domains and the shape of a canister subdomain are stated once. remapToLegacyDomain's behaviour is unchanged.
  • The spec documents the new discovery contract (docs/ii-spec.mdx, App metadata): which origins are asked and in what order, which statuses settle the question and which move on, and that the logo's same-origin requirement is relative to the domain that served the document rather than the one that was requested.

When a twin is tried

Only when the origin did not answer for the app:

Outcome at an origin Next
200, document valid done, metadata applied
200, document invalid or unparseable done, no metadata: the origin answered
404 done, no metadata: the canister says it has no such file
400, 5xx, other non-200 try the next twin
no response (network error, CORS rejection, redirect, timeout) try the next twin

The 404 case is what keeps this cheap. All three domains resolve to the same canister, so a canister that says "no such file" has answered for every one of them, and the ordinary case of an app that publishes nothing still costs exactly one request. Only an app in the broken position above pays for extra attempts.

Tests

11 new cases in appMetadata.test.ts (48 total in the file):

  • appMetadataOrigins: twins for a remapped origin, .raw preserved, no twins invented for a custom domain.
  • Fallback to icp0.io on the exact reported failure (400 client_domain_canister_mismatch at ic0.app, document at icp0.io), fallback on to icp.net, and fallback when the first origin cannot be reached at all.
  • No twin tried on 404, and none tried when a document is served but fails validation, each asserting the request count.
  • Every origin failing yields no metadata after three attempts.
  • A twin's logo resolves against that twin; a twin document whose logo points at the remapped origin is rejected.

Verification:

  • npx vitest run src/frontend/src/lib/utils/appMetadata.test.ts: 47 passed, 1 failed. The failure is should fetch a same-origin logo and render it from a blob url, which asserts expect.any(Blob) against a Blob from a different realm. It reproduces identically on main with this change stashed (36 passed, 1 failed), so it is a pre-existing sandbox issue and not a regression here.
  • npx vitest run src/frontend/src/lib/utils/validateDerivationOrigin.test.ts: 25 passed, covering the remapToLegacyDomain rewrite.
  • eslint --max-warnings 0, prettier --check and tsc --project tsconfig.all.json --noEmit are clean on the changed source files.
  • The e2e suite was not run locally (it needs a replica and the built canisters); CI covers it.

Note on sequencing

I understand the plan was to ship fuller support for this in about a week. This is the narrow fix for the reported symptom only, so it should be easy to drop or fold in if it overlaps with that work.


Generated by Claude Code

The origin an app's metadata is fetched from is the one its identity is
derived for, which `remapToLegacyDomain` has already collapsed onto
`ic0.app` when it is a canister gateway origin, so that a canister
derives one principal across `ic0.app`, `icp0.io` and `icp.net`.

A canister need not be served on all three. The staging test app answers
`400 client_domain_canister_mismatch` at `<id>.ic0.app` while serving the
document at `<id>.icp0.io`, so the fetch failed and the screens fell back
to the curated entry with nothing to indicate why. Principal derivation
needs that remap; the metadata document, which has no bearing on
derivation, does not.

Invert the remap and try the twins in turn. All three resolve to the same
canister, so this widens where the document may be served, never whose
document is used, and the same-origin rule for the logo now follows
whichever origin served the document.

An origin that answers `404`, or answers with a document at all, settles
the question and no twin is tried: an app that publishes nothing still
costs a single request, and a document that fails validation is still
that origin's answer rather than a reason to look elsewhere. Only a reply
that says the app is not served here (`400`, `5xx`) or no reply at all (a
network error, a CORS rejection, a redirect, a timeout) moves on.

To tell those apart, the shared capped fetch now reports the status it
saw instead of collapsing every failure to `undefined`.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds gateway-twin fallback when fetching canister app metadata.

Changes:

  • Generates equivalent ic0.app, icp0.io, and icp.net metadata origins.
  • Retries non-definitive fetch failures while preserving document-relative logo validation.
  • Adds gateway fallback and logo-origin tests.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
appMetadata.ts Implements gateway-origin fallback and status-aware fetching.
appMetadata.test.ts Tests fallback order, stopping rules, and logo origins.
Suppressed comments (1)

src/frontend/src/lib/utils/appMetadata.ts:493

  • This changes the public metadata discovery contract, but docs/ii-spec.mdx:350 still tells integrators that II fetches only from the derivation origin and that the document must be published there. It also does not describe the new status-based twin fallback or that logo same-origin is relative to the twin that served the document. Please update the specification so applications can reliably implement the behavior introduced here.
  for (const candidate of appMetadataOrigins(origin)) {
    const attempt = await fetchAppMetadataFrom(candidate);

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

claude added 2 commits August 24, 2026 14:07
Review feedback on #4281: the inverse of `remapToLegacyDomain` should not
restate its regex.

`GATEWAY_ORIGIN_REGEX` moves to `urlUtils` and captures both the canister
subdomain and the gateway domain, so `remapToLegacyDomain` normalizes
onto `ic0.app` with it and `gatewayOriginTwins` inverts it, from one
pattern and one list of gateway domains. `appMetadataOrigins` is then
just the origin followed by its twins.

`urlUtils` rather than `iiConnection` because `appMetadata` is a leaf
with no local imports, and `remapToLegacyDomain` keeps its home so its
call sites are untouched.

`remapToLegacyDomain` behaviour is unchanged: it previously matched only
the two remapped domains and passed everything else through, and now
matches all three and passes `ic0.app` through. Checked against the old
implementation over `.raw` labels, subdomain-suffix lookalikes
(`x.icp0.io.evil.com`), non-default ports, paths, uppercase and the empty
string.
The metadata document is no longer fetched only from the origin the app's
identity is derived for: when that is a canister gateway origin, the same
canister's other gateway domains are tried in turn, because a canister
reachable at `<id>.icp0.io` can answer `400 client_domain_canister_mismatch`
at the `<id>.ic0.app` origin principals are derived against.

Spell out the retry policy integrators need to implement against (which
statuses settle the question and which move on), and make explicit that the
logo's same-origin requirement is relative to the domain that served the
document, not to the requested one.
@aterga
aterga marked this pull request as ready for review August 24, 2026 15:02
@aterga
aterga requested a review from a team as a code owner August 24, 2026 15:02
@aterga
aterga requested a review from sea-snake August 24, 2026 15:02
@zeropath-ai

zeropath-ai Bot commented Aug 24, 2026

Copy link
Copy Markdown

✅ No security or compliance issues detected. Reviewed everything up to 23d8ffc.

Security Overview
Detected Code Changes
Change Type Relevant files
Enhancement ► src/frontend/src/lib/utils/appMetadata.ts
    Introduce appMetadataOrigins and related logic for gateway twin fallbacks
► src/frontend/src/lib/utils/urlUtils.ts
    Add gateway domain support and gatewayOriginTwins for origin mappings
Enhancement ► src/frontend/src/lib/utils/iiConnection.ts
    Update remapToLegacyDomain to use gateway origin regex-based mapping
Enhancement ► src/frontend/src/lib/utils/appMetadata.test.ts
    Add tests for gateway twin fallbacks, remapped origins, and logo handling

@sea-snake sea-snake left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just icp.net was probably sufficient, but PR is fine nontheless.

@aterga
aterga merged commit 48ddf5c into main Aug 25, 2026
45 checks passed
@aterga
aterga deleted the claude/increase-alternative-origins-limit-3e6kxx branch August 25, 2026 12:54
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.

4 participants