mirror of
https://github.com/hanzoai/authz.git
synced 2026-11-18 13:25:20 +00:00
IAM signs `type: application` for a client_credentials token and leaves it empty for a person, in exactly one place. Claims did not declare the field, so the fact was dropped at decode and Machine had to infer from what the token lacked. Machine now takes the union, which is strictly more conservative: a program that carries a membership set no longer reads as a person, and a person whose memberships failed to resolve still loses authority rather than gaining it. Program is the positive predicate, for consumers that hand out reach and should refuse what cannot prove itself. The claim is `type`. The predicate this replaces read `tokenType`, which IAM assigns only "access-token" and "id-token", so it never fired — and its test passed because the fixture used a value IAM never mints. Verified at the issuer instead: pkg/schema/user.go names the constant, internal/oidc/jwt.go declares the claim, internal/oidc/token.go sets it inside clientCredentialsGrant alone.
215 lines
8.3 KiB
Go
215 lines
8.3 KiB
Go
package authz
|
|
|
|
import "testing"
|
|
|
|
// iamClientCredentials builds the claim set IAM's client_credentials grant ACTUALLY
|
|
// signs, field for field, from internal/oidc: Sign() stamps
|
|
//
|
|
// Subject: app.GetId() // "<appOwner>/<appName>"
|
|
// Owner: app.Organization
|
|
// TokenType: "access-token" // NEVER "application"
|
|
// Orgs: nil // "a machine token has no user and therefore
|
|
// // no membership set" — token.go, verbatim
|
|
//
|
|
// Every existing test in this package built its machine with TokenType:"application",
|
|
// a value no IAM code path assigns — so the machine narrowing was only ever exercised
|
|
// against a token shape that does not exist, and passed.
|
|
func iamClientCredentials(org string) *Claims {
|
|
c := &Claims{Owner: org, TokenType: "access-token", Orgs: nil}
|
|
c.Subject = org + "/kms-sync"
|
|
c.Audience = []string{"kms-sync"} // audienceFor(app, "") — the client id
|
|
return c
|
|
}
|
|
|
|
// A confidential client owned by the reserved admin org must hold NO platform
|
|
// authority. It is the case the doc comment on Sudo names outright: "any
|
|
// admin-org client_credentials identity — the KMS sync app, say — could name a
|
|
// victim org and the edge would write it, handing every backend that trusts that
|
|
// header a cross-tenant read."
|
|
//
|
|
// The narrowing has to bite on the token IAM signs, not on a token shape invented
|
|
// by the test.
|
|
func TestAdminOrgMachineHoldsNoPlatformAuthority(t *testing.T) {
|
|
c := iamClientCredentials(AdminOrg)
|
|
|
|
if !c.Machine() {
|
|
t.Error("an IAM client_credentials token is not recognized as a machine")
|
|
}
|
|
if c.Sudo() {
|
|
t.Error("an admin-org machine holds platform sudo")
|
|
}
|
|
// It resolves NO org at all, which is stricter than pinning it to its own: the only
|
|
// claim carrying a machine's org is `owner`, and that claim is indistinguishable
|
|
// from the app a HUMAN signed in through. A consumer that positively identifies a
|
|
// machine — cloud, from the API key it authenticated or the owner-bound KMS
|
|
// audience — supplies that org itself rather than reading it out of a claim.
|
|
if org, switched := c.EffectiveOrg("victim"); switched || org != "" {
|
|
t.Errorf("an admin-org machine resolved %q (switched=%v), want no org", org, switched)
|
|
}
|
|
if c.Can(Read, Path{"victim", "prod"}, nil) {
|
|
t.Error("an admin-org machine may read another tenant")
|
|
}
|
|
}
|
|
|
|
// A real platform operator keeps sudo. The narrowing must separate a person from a
|
|
// machine, not deny both — IAM mints every USER token with a membership set whose
|
|
// first entry is the home org (store.MemberOrgRefs), so a human in the admin org is
|
|
// distinguishable from an app there by what IAM signed.
|
|
func TestAdminOrgHumanKeepsSudo(t *testing.T) {
|
|
c := &Claims{
|
|
Owner: AdminOrg,
|
|
PreferredUsername: "z",
|
|
Orgs: []Membership{{Org: AdminOrg, Role: Admin}},
|
|
}
|
|
c.Subject = "3a1f-uuid"
|
|
c.TokenType = "access-token"
|
|
|
|
if c.Machine() {
|
|
t.Fatal("a human platform operator is misread as a machine")
|
|
}
|
|
if !c.Sudo() {
|
|
t.Fatal("a human platform operator lost platform sudo")
|
|
}
|
|
if org, switched := c.EffectiveOrg("acme"); !switched || org != "acme" {
|
|
t.Errorf("a platform operator cannot view another tenant: got %q switched=%v", org, switched)
|
|
}
|
|
}
|
|
|
|
// A tenant's own confidential client is a machine too, and holds neither scope.
|
|
// Its authority is its capability allowlist, never an org's self-service surface.
|
|
func TestTenantMachineIsNeitherAdminScope(t *testing.T) {
|
|
c := iamClientCredentials("acme")
|
|
c.IsAdmin = true // IAM's ORG-role bit, carried on the application row itself
|
|
|
|
if !c.Machine() {
|
|
t.Error("a tenant's client_credentials token is not recognized as a machine")
|
|
}
|
|
if c.Sudo() {
|
|
t.Error("a tenant machine holds platform sudo")
|
|
}
|
|
if c.OrgAdmin("acme") {
|
|
t.Error("a tenant machine administers its own org")
|
|
}
|
|
}
|
|
|
|
// ACTING and PAYING are different questions. An operator viewing a customer's org
|
|
// reads the customer's data and spends the OPERATOR's ledger; a member who switches
|
|
// to a team org spends THAT org's. One function answering both would have to pick,
|
|
// and either pick is wrong for the other case.
|
|
func TestPayingIsNotActing(t *testing.T) {
|
|
operator := &Claims{Owner: AdminOrg, PreferredUsername: "z",
|
|
Orgs: []Membership{{Org: AdminOrg, Role: Admin}}}
|
|
member := &Claims{Owner: "acme", PreferredUsername: "alice",
|
|
Orgs: []Membership{{Org: "acme", Role: Member}, {Org: "beta", Role: Member}}}
|
|
|
|
if org, _ := operator.EffectiveOrg("customer"); org != "customer" {
|
|
t.Errorf("the operator acts in %q, want the viewed tenant", org)
|
|
}
|
|
if payer := operator.LedgerOrg("customer"); payer != AdminOrg {
|
|
t.Errorf("the operator spends %q — a viewed tenant must not fund the visit", payer)
|
|
}
|
|
|
|
if org, _ := member.EffectiveOrg("beta"); org != "beta" {
|
|
t.Errorf("the member acts in %q, want the selected org", org)
|
|
}
|
|
if payer := member.LedgerOrg("beta"); payer != "beta" {
|
|
t.Errorf("the member spends %q, want the org acted in", payer)
|
|
}
|
|
// A selection outside the membership set funds nothing but the caller's own org.
|
|
if payer := member.LedgerOrg("victim"); payer != "acme" {
|
|
t.Errorf("an ungranted selection billed %q", payer)
|
|
}
|
|
}
|
|
|
|
// A machine cannot move the ledger. It reads as a machine, so Sudo is
|
|
// false, so LedgerOrg takes the ordinary branch and EffectiveOrg has already
|
|
// refused the selection — the two compose to "your own org pays" with no extra rule.
|
|
func TestMachineCannotMoveTheLedger(t *testing.T) {
|
|
m := iamClientCredentials(AdminOrg)
|
|
if payer := m.LedgerOrg("victim"); payer != "" {
|
|
t.Errorf("an admin-org machine billed %q, want nothing", payer)
|
|
}
|
|
}
|
|
|
|
// Location stops at the first absent segment, so a project under no workspace can
|
|
// never print as a workspace of the same name.
|
|
func TestLocationStopsAtTheFirstGap(t *testing.T) {
|
|
base := func() *Claims {
|
|
return &Claims{Owner: "acme", PreferredUsername: "alice",
|
|
Orgs: []Membership{{Org: "acme", Role: Member}}}
|
|
}
|
|
for _, c := range []struct {
|
|
name string
|
|
workspace string
|
|
project string
|
|
want string
|
|
}{
|
|
{"org only", "", "", "acme"},
|
|
{"workspace", "prod", "", "acme/prod"},
|
|
{"full", "prod", "web", "acme/prod/web"},
|
|
{"project with no workspace stops at the org", "", "web", "acme"},
|
|
{"a non-injective workspace ends the path", "prod ", "web", "acme"},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
cl := base()
|
|
cl.Workspace, cl.Project = c.workspace, c.project
|
|
if got := cl.Location("").String(); got != c.want {
|
|
t.Errorf("Location = %q, want %q", got, c.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A PROGRAM SAYS SO, AND A MEMBERSHIP SET DOES NOT UNSAY IT.
|
|
//
|
|
// Machine's older reading was len(Orgs)==0 alone, so a program that carried any
|
|
// membership read as a person — and a person in the reserved org is an operator.
|
|
// The claim IAM signs settles it in the safe direction whatever the set holds.
|
|
func TestAProgramInTheAdminOrgIsNeverAnOperator(t *testing.T) {
|
|
program := &Claims{
|
|
Owner: AdminOrg,
|
|
TokenType: "access-token",
|
|
Type: Program,
|
|
Orgs: []Membership{{Org: AdminOrg, Role: Admin}},
|
|
}
|
|
if !program.Program() {
|
|
t.Fatal("IAM signed type=application and the claim did not survive the decode")
|
|
}
|
|
if !program.Machine() {
|
|
t.Fatal("a program carrying a membership read as a person — the set must not outvote the signed claim")
|
|
}
|
|
if program.Sudo() {
|
|
t.Fatal("a client_credentials token in the reserved org held platform authority")
|
|
}
|
|
|
|
person := &Claims{
|
|
Owner: AdminOrg,
|
|
PreferredUsername: "z",
|
|
TokenType: "access-token",
|
|
Orgs: []Membership{{Org: AdminOrg, Role: Admin}},
|
|
}
|
|
if person.Program() {
|
|
t.Fatal("a person's token claimed to be a program; IAM leaves type empty for people")
|
|
}
|
|
if person.Machine() {
|
|
t.Fatal("an operator read as a machine")
|
|
}
|
|
if !person.Sudo() {
|
|
t.Fatal("precondition: a member of the reserved org is the operator this test contrasts against")
|
|
}
|
|
}
|
|
|
|
// The claim is `type`. An earlier predicate read `tokenType`, which IAM assigns
|
|
// only "access-token" and "id-token", so it could never fire.
|
|
func TestTokenTypeIsNotTheKindOfSubject(t *testing.T) {
|
|
for _, tt := range []string{"access-token", "id-token"} {
|
|
c := &Claims{TokenType: tt, Orgs: []Membership{{Org: "acme", Role: Admin}}}
|
|
if c.Program() {
|
|
t.Fatalf("tokenType=%q was read as the subject kind", tt)
|
|
}
|
|
}
|
|
if (&Claims{TokenType: Program}).Program() {
|
|
t.Fatal("the predicate read tokenType instead of type — the exact bug this replaces")
|
|
}
|
|
}
|