Files
ai/controllers/zap_principal_test.go
zeekay e71dfbc4f3 SuperAdmin is the host boundary's verdict, never the org a token names
util.IsSuperAdmin compared the resolved user's owner to "admin", a
second predicate beside the one the host boundary decides, and the two
disagreed. An admin-org application (no orgs, owner claim admin), an
admin-org person's sk- key, and an org spelled "admin " (the parse
trimmed orgs[0]) were each SuperAdmin here and refused by the boundary.

The host now states the verdict: object.SetSuper, which hanzo-inc/cloud
fills with principal.Super, the X-User-IsAdmin its boundary deletes from
every request and re-mints from authz.Claims.Sudo. Every HTTP seam that
turns a token into a principal goes through object.Verify, which stamps
it on iam.User.Sudo (never decoded); IsSuperAdmin and ScopeOwner read
that and compare no org. util.AdminOrg is gone; the rows that are the
platform's own are named by authz.AdminOrg.

With no host there is no SuperAdmin, and a ZAP door is not the host:
target deletes X-User-IsAdmin from a ZAP-built request, and zapPrincipal
carries a verdict only when the HTTP bridge vouched for the request it
was handed. A key is never the SuperAdmin, as at the host.
2026-10-18 22:21:43 -07:00

64 lines
2.3 KiB
Go

// Copyright 2026 Hanzo AI Inc. All Rights Reserved.
package controllers
import (
"context"
"testing"
iam "github.com/hanzoai/ai/internal/iam"
)
// What a credential has to look like to name somebody, asserted in the one place
// that decides it.
//
// Five copies of this used to exist and differed on two points, so both are
// pinned here: the space around a credential is not part of it, and a value that
// declares itself an API key by its prefix is evaluated as one and only one —
// never handed to JWT validation after failing as a key.
func TestWhatACredentialHasToLookLikeToNameSomebody(t *testing.T) {
iamd := withIAM(t)
alice := &iam.User{Owner: "acme", Name: "alice"}
key := iamd.asUser(t, alice)
t.Run("a key names its person", func(t *testing.T) {
if u := zapPrincipal(context.Background(), key); u == nil || u.Name != "alice" {
t.Errorf("resolved %+v, want alice", u)
}
})
t.Run("the space around it is not part of it", func(t *testing.T) {
if u := zapPrincipal(context.Background(), key+" "); u == nil || u.Name != "alice" {
t.Errorf("a trailing space cost the credential its person: %+v", u)
}
})
t.Run("nothing names nobody", func(t *testing.T) {
for _, auth := range []string{"", "Bearer ", "Bearer "} {
if u := zapPrincipal(context.Background(), auth); u != nil {
t.Errorf("%q resolved to %+v", auth, u)
}
}
})
t.Run("a key IAM does not know names nobody", func(t *testing.T) {
if u := zapPrincipal(context.Background(), "Bearer sk-not-a-key"); u != nil {
t.Errorf("an unknown key resolved to %+v", u)
}
})
// A value can be shaped like both — an sk- prefix and three long dot-separated
// parts — and it is read as the key it claims to be. Falling through to JWT
// validation after failing as a key would let a caller choose which of the two
// credentials to be judged as.
t.Run("a value shaped like both is judged as a key", func(t *testing.T) {
both := "sk-aaaaaaaaaaaaaa.bbbbbbbbbbbbbb.cccccccccccccc"
if !isIAMApiKey(both) || !isJwtToken(both) {
t.Fatalf("fixture is not shaped like both: key=%v jwt=%v", isIAMApiKey(both), isJwtToken(both))
}
if u := zapPrincipal(context.Background(), "Bearer "+both); u != nil {
t.Errorf("resolved to %+v; an unknown key must name nobody", u)
}
})
}