Files
iam/internal/oidc/federation_contract_test.go
zeekayandHanzo Dev 0b0b3941d4 A session-cookie write comes only from the issuer's own page, and a process with no issuer names none
The session cookie is SameSite=Lax, which keeps it off requests from other
sites but not from other hosts of the same site. Once hanzo.ai signs people
in, any page on any *.hanzo.ai host could post a form to /v1/iam/login with
type=device and approve an attacker's `hanzo login` code, or with type=code
mint an authorization code, or edit the account. sessions.Forgeable is now
the one rule for a write the cookie authorizes: Sec-Fetch-Site must be
same-origin or none, and a request with a body must carry it as JSON.
sessions.Writer resolves the session only past it, and every cookie write
goes through it: the login SSO and device branches, callerOf/callerFrom
(profile, password rotation, terms, consent, preferences, onboarding,
link, unlink, passkey enrollment), and invitation accept. A refused write
is 403 before the session is read. The passkey finish, which the challenge
cookie authorizes, asks the same rule. A bearer from a client that is not a
browser needs no fetch metadata, with or without a cookie riding along.

Signing out ends sessions, so sessions.Clear asks the rule too, with one
exception: a top-level navigation (Sec-Fetch-Mode navigate, Sec-Fetch-Dest
document), which is how a relying party signs a person out. An image, a
frame or a sibling's form ends nothing.

The federation binding cookie is __Host-hanzo_fed and only that name is
read; a test now pins that the bare hanzo_fed name is refused.

server.RequireIssuer is the boot check for issuers: it installs IAM_ISSUER
and IAM_ISSUER_MAP, then IAM_FEDERATION_ORIGIN_MAP, and returns an error for
an empty or non-https IAM_ISSUER (outside IAM_DEV_HOST_RELATIVE=1), a
malformed issuer map, or a sign-in map that would strand the binding. `iam
serve` calls it, and a host that embeds IAM must call it before NewApp.
devFallbackIssuer and the lazy env resolvers are gone: a process that never
installed an issuer has none, so every issuer read is an error, discovery
answers 503, no token is minted and none is accepted, where it used to
answer https://hanzo.id. No issuer value changes.

Co-authored-by: Hanzo Dev <[email protected]>
2026-10-17 19:45:43 -07:00

87 lines
4.1 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package oidc
import "testing"
// The federation callback is not an internal detail. It is the redirect_uri iam
// hands every external IdP, and an IdP refuses any value it was not told about in
// advance. So this string is a CONTRACT held in two places at once: here, and in
// each provider's own console.
//
// Nothing in this package can observe the other half. When federation moved off
// Casdoor's `<iam host>/callback` to the canonical path below, the GitHub App's
// callback list was updated and Google's OAuth client was not — so Google refused
// sign-in on EVERY brand with `Error 400: redirect_uri_mismatch` while this suite
// stayed green, GitHub kept working, and the only report was a person who could
// not log in.
//
// ⚠️ ASSERT THROUGH resolveFederationOrigin, NEVER resolveIssuer. The two were one
// value until the origin was unbraided from the issuer; today, for a host
// IAM_FEDERATION_ORIGIN_MAP does not name, the federation resolver FALLS BACK to the issuer, so
// both spellings pass and the wrong one is indistinguishable from the right one.
// The moment an origin is pinned — which is the entire point of that split — a
// test written against resolveIssuer keeps passing while the real callback moves.
// That is the exact false green this file exists to prevent, so it is worth the
// one line of care.
func TestFederationCallbackIsTheRegisteredContract(t *testing.T) {
installIssuerResolver(t, "https://hanzo.id", testIssuerMap)
for host, want := range map[string]string{
"hanzo.id": "https://hanzo.id/v1/iam/oauth/callback",
"iam.hanzo.ai": "https://hanzo.id/v1/iam/oauth/callback",
"lux.id": "https://lux.id/v1/iam/oauth/callback",
"iam.lux.network": "https://lux.id/v1/iam/oauth/callback",
"id.zoo.network": "https://id.zoo.network/v1/iam/oauth/callback",
"pars.id": "https://pars.id/v1/iam/oauth/callback",
} {
// The composition federationCallbackURL performs, through the same seam a
// live request takes.
if got := mustOrigin(t, host) + PathFederationCallback; got != want {
t.Errorf("federation callback for %s = %s, want %s\n"+
"If this change is intended, register the new URI with EVERY external IdP "+
"(the Google OAuth client AND the GitHub App) BEFORE shipping — each refuses "+
"any redirect_uri it does not already hold, and neither failure is visible from here.",
host, got, want)
}
}
}
// A same-host map is a no-op: each brand keeps its own callback, which is the
// list registered with Google and GitHub. A host the map names as its OWN origin
// gets a callback of its own — the line that has to be registered with every
// external IdP before the map ships.
func TestFederationCallbackPerBrandUnderASameHostMap(t *testing.T) {
t.Setenv("IAM_ISSUER", "https://hanzo.id")
t.Setenv("IAM_ISSUER_MAP", `{"hanzo.id":"https://hanzo.id","lux.id":"https://lux.id"}`)
t.Setenv("IAM_FEDERATION_ORIGIN_MAP", `{"hanzo.id":"https://hanzo.id","lux.id":"https://lux.id"}`)
prevIss, prevFed := activeResolver.Load(), activeFederationResolver.Load()
t.Cleanup(func() { activeResolver.Store(prevIss); activeFederationResolver.Store(prevFed) })
activeResolver.Store(nil)
activeFederationResolver.Store(nil)
if err := InitIssuerResolver(); err != nil {
t.Fatalf("InitIssuerResolver: %v", err)
}
if err := InitFederationResolver(); err != nil {
t.Fatalf("InitFederationResolver: %v", err)
}
for host, want := range map[string]string{
"hanzo.id": "https://hanzo.id/v1/iam/oauth/callback",
"lux.id": "https://lux.id/v1/iam/oauth/callback",
} {
if got := mustOrigin(t, host) + PathFederationCallback; got != want {
t.Errorf("callback for %s = %s, want %s", host, got, want)
}
}
installFederationResolver(t, `{"hanzo.ai":"https://hanzo.ai"}`)
if got, want := mustOrigin(t, "hanzo.ai")+PathFederationCallback, "https://hanzo.ai/v1/iam/oauth/callback"; got != want {
t.Errorf("callback for hanzo.ai = %s, want %s\n"+
"Register it with EVERY external IdP (the Google OAuth client AND the GitHub App) "+
"BEFORE the map ships: each refuses a redirect_uri it does not already hold.", got, want)
}
}