Files
zeekayandHanzo Dev 093a85851e Take zip v1.36.46, where a rule may hold as well as allow or refuse
zip's authorization rule has answered three ways since v1.36.45 — allow, refuse,
hold — and this module was still on v1.34.2, one contract behind a released
breaking change. Nothing else in the bump needed touching; the seam is one
function.

Its refusals become Deny rather than a bare 403, so each carries the clause that
made it: no principal, or an entity the caller may not reach. The error lane is
left for a check that could not be made at all, which is what it is for.

The tests asked "did this return an error", and a refusal no longer does. One
helper now asks the question they were asking — was this call let through —
reading both halves of the answer, so a Deny cannot read as an admission.

The SCIM discovery document gained a 202 on every governed path and that is the
framework's, not this package's: a held op answers 202, so a governed contract
declares one. Our rule never holds. The document states what a governed contract
may answer, not what this rule chooses.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-10 17:41:58 -07:00

81 lines
3.7 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package authz
import (
"context"
"testing"
policy "github.com/hanzoai/authz"
"github.com/zap-proto/zip"
"github.com/hanzoai/iam/internal/principal"
)
// A HANDLER-AUTHORIZED READ STILL NEEDS A CALLER.
//
// pathAuthorized reads (SCIM, memberships, the project/workspace switcher lists)
// carry their target in the path and let the HANDLER authorize the row, so the
// op-invoke seam skips the target check. Skipping the target is not skipping the
// caller: reaching this seam with no principal means a door let an unauthenticated
// request through, and a discovery handler that discards ctx then runs for nobody.
// The principal is required BEFORE the early return, not after.
func TestAuthorize_HandlerAuthorizedReadStillRequiresAPrincipal(t *testing.T) {
// A SCIM read — a handler-authorized subtree.
op := zip.Op{Method: "GET", Path: "/v1/iam/scim/v2/Users"}
in := &struct{ Owner, Name string }{}
if !pathAuthorized(op.Path) {
t.Fatalf("%s is not handler-authorized — the test no longer exercises the early return", op.Path)
}
if !refused(context.Background(), op, in) {
t.Fatal("a handler-authorized read was admitted with no principal attached")
}
// With a principal, the early return admits it — the handler authorizes the row.
p := &principal.Principal{Org: "acme", User: "alice"}
if refused(principal.Bind(context.Background(), p), op, in) {
t.Fatal("a handler-authorized read refused a present principal")
}
}
// GATING A DOOR DOES NOT LET A CAPABILITY APP BIND A FOREIGN ROW.
//
// Once authz.Control gates the graph, MCP and call-plane doors, an authenticated
// capability app reaches the op-invoke seam with the target decoded from the body
// — the one place a by-name transport carries it, since no URL binds it. The
// entity pin is what keeps that from authorizing another tenant's row: a NAMED read
// is admitted only for the tenant the app serves. An empty target names no row, so
// it can never be a foreign one — it reaches the collection, whose tenant the list
// handler pins from the same served org (principal.Scope), never this seam.
func TestAuthorize_CapAppCannotBindAForeignRowOverADoor(t *testing.T) {
t.Setenv(policy.CapKeyMint.Env, "hanzo-console")
// The credential administrator: admin-owned (the pin), serving "hanzo".
console := &principal.Principal{App: &policy.App{Name: "hanzo-console", Owner: "admin"}, Org: "hanzo"}
ctx := principal.Bind(context.Background(), console)
type ref struct{ Owner, Name string }
item := zip.Op{Method: "GET", Path: "/v1/iam/keys/:owner/:name"}
list := zip.Op{Method: "GET", Path: "/v1/iam/keys"}
// The empty target the coupling warns of: a body that binds nothing decodes to
// ("",""). It names no row.
if o, n := decodedTarget(&ref{}); o != "" || n != "" {
t.Fatalf("an empty body decoded to a bound target %q/%q", o, n)
}
// The exploit shape a call plane makes reachable: a NAMED foreign row in the body.
// The pin refuses it, so gating the door binds no other tenant's row.
if !refused(ctx, item, &ref{Owner: "lux", Name: "x"}) {
t.Fatal("the console bound a NAMED foreign key row over the op seam")
}
// Its own served tenant's named row it may bind.
if refused(ctx, item, &ref{Owner: "hanzo", Name: "x"}) {
t.Fatal("the console cannot bind a named row of the tenant it serves")
}
// The collection is admitted here and pinned in the handler; gating the door does
// not turn an empty target into a foreign LIST either — principal.Scope refuses a
// foreign owner there, and defaults an empty one to the served org.
if refused(ctx, list, &ref{}) {
t.Fatal("the console was refused a collection the list handler pins")
}
}