From faeaa0d7f067ba0e1fd3309c81d3e366c8c8293d Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 22 Nov 2026 22:19:07 +1100 Subject: [PATCH] fix: deny orgs without active org policies - refuse authorization when an org has no stored org-level policies instead of falling through the narrowed chain - seed tests with shipped org policies and cover the deny-by-default cases --- modules/governance/access_test.go | 47 +++++++++++++++++++++++++++---- modules/governance/engine.go | 6 ++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/modules/governance/access_test.go b/modules/governance/access_test.go index fbc9fd2ab..39f3dee46 100644 --- a/modules/governance/access_test.go +++ b/modules/governance/access_test.go @@ -125,7 +125,7 @@ func TestPendingChainRefusesUntilLoaded(t *testing.T) { if !loaded { return nil, errors.New("store down") } - return nil, nil + return ShippedOrgPolicies(DefaultOrgID), nil }, slog.New(slog.DiscardHandler)) request := func() Decision { @@ -199,17 +199,20 @@ func TestLiveAppliesAPolicyEditBeforeTheNextDecision(t *testing.T) { }{ { name: "an unchanged version keeps the chain", - write: func(store *fakePolicyStore) { store.policies = []Policy{forbidAll} }, + write: func(store *fakePolicyStore) { store.policies = append(ShippedOrgPolicies(DefaultOrgID), forbidAll) }, wantAllowed: true, }, { - name: "a bumped version applies the forbid", - write: func(store *fakePolicyStore) { store.policies = []Policy{forbidAll}; store.version++ }, + name: "a bumped version applies the forbid", + write: func(store *fakePolicyStore) { + store.policies = append(ShippedOrgPolicies(DefaultOrgID), forbidAll) + store.version++ + }, }, { name: "a failed version read refuses", write: func(store *fakePolicyStore) { - store.policies = []Policy{forbidAll} + store.policies = append(ShippedOrgPolicies(DefaultOrgID), forbidAll) store.version++ store.versionErr = storeDown }, @@ -223,7 +226,7 @@ func TestLiveAppliesAPolicyEditBeforeTheNextDecision(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - store := &fakePolicyStore{version: 1} + store := &fakePolicyStore{version: 1, policies: ShippedOrgPolicies(DefaultOrgID)} live := NewPending(store.PoliciesVersion, store.ListPolicies, slog.New(slog.DiscardHandler)) if err := live.Reload(t.Context()); err != nil { t.Fatal(err) @@ -496,3 +499,35 @@ func TestSeedShippedPolicies(t *testing.T) { t.Fatal("a failed seed returned no error") } } + +// TestAuthorizeWithoutOrgPolicies guards the deny-by-default rule: an org +// with no stored org policies, or only narrowing ones, must refuse every +// request instead of falling through to allow. +func TestAuthorizeWithoutOrgPolicies(t *testing.T) { + const org = OrgID("acme") + owner := Principal{IdentityID: "o", Org: org, Memberships: []Membership{{IdentityID: "o", OrgID: org, Role: RoleOwner}}} + permitAllInstance := Policy{ID: "all", Level: LevelInstance, Text: `permit(principal, action, resource);`} + spaceOnly := Policy{ + ID: "space", Level: LevelWorkspace, OrgID: org, WorkspaceID: "prod", + Text: `permit(principal, action, resource);`, + } + tests := []struct { + name string + policies []Policy + allowed bool + }{ + {name: "no policies at all"}, + {name: "another org is seeded", policies: ShippedOrgPolicies(DefaultOrgID)}, + {name: "instance policy only", policies: []Policy{permitAllInstance}}, + {name: "workspace policy only", policies: []Policy{spaceOnly}}, + {name: "shipped org policies", policies: ShippedOrgPolicies(org), allowed: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewEngine(tt.policies).Authorize(t.Context(), owner, ActionRead, Resource{Kind: KindDashboard, Org: org}, AccessContext{}) + if got.Allowed != tt.allowed { + t.Fatalf("allowed = %v, want %v (%+v)", got.Allowed, tt.allowed, got) + } + }) + } +} diff --git a/modules/governance/engine.go b/modules/governance/engine.go index 6658a05f1..05c11f49b 100644 --- a/modules/governance/engine.go +++ b/modules/governance/engine.go @@ -180,6 +180,12 @@ func (e *Engine) Authorize(_ context.Context, p Principal, a Action, r Resource, if p.Org == "" || resOrg != p.Org { return DeniedAt(LevelInstance, []string{CrossOrgForbidID}) } + // Lower levels only narrow, so an org with no policies of its own has + // nothing that grants access. Evaluating the chain alone would skip every + // empty level and allow the request. + if !e.orgs[resOrg].active() { + return DeniedAt(LevelOrg, nil) + } return evaluate(e.chain(p, r), buildEntities(p, r), buildRequest(p, a, r, c)) }