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
This commit is contained in:
sam committed 2026-11-22 22:19:07 +11:00
1 parent 240dc52226
commit faeaa0d7f0
2 files changed
+47 -6

No files matched your search

+41 -6
View File
@@ -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)
}
})
}
}
+6
View File
@@ -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))
}