Files
iam/internal/applications/scope_test.go
zeekay 2bbb0db654 A brand org is no tenant of its plain members, and a key dies with its holder
A key resolves only while its holder may sign in: a suspended or deleted
account's keys answer key_holder_disabled, its org's default key included. An
org the platform runs (authz.IsPlatformOrg) is a tenant only of its owners and
admins, its own machines and a SuperAdmin, and a brand's directory of no one:
store.Acts is that rule asked of one row, and the key resolver
(key_not_tenant), the user-key mint (403, no key filed that acts for no one) and
the delegation read it. principal.Scope and ScopeRead require MemberOf, the org
listing filters by it, and the registry gives a person a token only as an admin
of a candidate org.

A delegated token (as(), naming its actor in act) founds no org and writes no
account. A suspended account resolves nobody on its session or its bearer, and
founds nothing. An act key names its holder and mints only while that holder
runs the key's org. A machine signs in as an application or with its key: it is
refused a password grant, a form sign-in and an invitation. The groups claim
carries the orgs a person is a tenant of, so a relying party that maps groups
onto access sees no directory and no brand org held as a plain member. A name a
removed org still holds draws the answer a standing one does.

authz v1.10.47.
2026-10-17 04:07:47 -07:00

80 lines
2.6 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package applications
// A listing is scoped by the CALLER, never by the input.
//
// The op's Authorize hook re-checks the decoded target, so a foreign owner is
// already refused before this handler runs — but that is a second gate, not this
// one's. The handler filters on the owner principal.Scope returns, so the rows it
// reads are the caller's own whatever the input asked for, and it stands alone if
// it is ever called from anywhere but a guarded route.
import (
"context"
"testing"
policy "github.com/hanzoai/authz"
"github.com/hanzoai/orm"
"github.com/hanzoai/iam/internal/principal"
"github.com/hanzoai/iam/pkg/schema"
)
func asTenant(org string) context.Context {
return principal.Bind(context.Background(), &policy.Principal{Org: org, User: "someone"})
}
func seed(t *testing.T, db orm.DB, owner, name string) {
t.Helper()
if _, err := Create(db)(context.Background(),
&schema.Application{Owner: owner, Name: name, ClientId: owner + "-" + name}); err != nil {
t.Fatalf("seed %s/%s: %v", owner, name, err)
}
}
// A tenant asking for another tenant's applications is refused by this handler,
// not merely by the gate above it. An application row carries the client
// credential the tenant authenticates with, so its listing is a credential index.
func TestList_refusesAForeignOwner(t *testing.T) {
db := memDB(t)
seed(t, db, "victim", "console")
if _, err := listApplications(db)(asTenant("attacker"), &ApplicationQuery{Owner: "victim"}); err == nil {
t.Fatal("a tenant listed another tenant's applications")
}
}
// With no principal at all there is no scope to read, so the listing is refused
// rather than answered.
func TestList_refusesWithoutPrincipal(t *testing.T) {
db := memDB(t)
seed(t, db, "hanzo", "console")
if _, err := listApplications(db)(context.Background(), &ApplicationQuery{Owner: "hanzo"}); err == nil {
t.Fatal("a listing with no principal was answered")
}
}
// The rows a caller gets are its own: the owner the handler filters on is the one
// Scope returned.
func TestList_filtersOnTheScopedOwner(t *testing.T) {
db := memDB(t)
seed(t, db, "acme", "hanzo-console")
seed(t, db, "victim", "victim-console")
out, err := listApplications(db)(asTenant("acme"), &ApplicationQuery{Owner: "acme"})
if err != nil {
t.Fatalf("list own org: %v", err)
}
for _, a := range out.Applications {
if a.Owner != "acme" {
t.Errorf("listing leaked %s/%s", a.Owner, a.Name)
}
}
if len(out.Applications) != 1 {
t.Errorf("got %d applications, want 1", len(out.Applications))
}
}