From 7e1bce7ce4c3ee3d13c8bffc3d6680262cd03c77 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 5 Oct 2026 20:47:18 +1100 Subject: [PATCH] fix(webui): the org-sys owner is the instance owner --- internal/webui/api_access_policies.go | 9 ++-- internal/webui/api_access_policies_test.go | 61 ++++++++++++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 internal/webui/api_access_policies_test.go diff --git a/internal/webui/api_access_policies.go b/internal/webui/api_access_policies.go index 9ad4a8040..0765e2292 100644 --- a/internal/webui/api_access_policies.go +++ b/internal/webui/api_access_policies.go @@ -112,12 +112,15 @@ func shippedPolicyIDs() []string { return []string{access.PolicyOrgRead, access.PolicyOrgEdit, access.PolicyOrgAdmin, access.PolicyOrgOwner} } -// instanceOwner reports whether the request acts as the instance owner: the -// shared-token principal of a single-operator install. +// instanceOwner reports whether the request acts as the instance owner: a +// principal holding the owner role of the default org. The shared token acts +// as the org-sys operator person, and the system fallback is an owner of +// org-sys too, so both answer true; an owner of any other org, or a member of +// org-sys with a lesser role, does not. func instanceOwner(r *http.Request) bool { principal, ok := access.PrincipalFromContext(r.Context()) if !ok { principal = access.SharedTokenOwner() } - return principal.IdentityID == access.SharedTokenOwner().IdentityID + return principal.Org == org.DefaultOrgID && principal.Role("") == org.RoleOwner } diff --git a/internal/webui/api_access_policies_test.go b/internal/webui/api_access_policies_test.go new file mode 100644 index 000000000..dfec38608 --- /dev/null +++ b/internal/webui/api_access_policies_test.go @@ -0,0 +1,61 @@ +package webui + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/samcharles93/archie-core/internal/domain/access" + "github.com/samcharles93/archie-core/internal/domain/identity" + "github.com/samcharles93/archie-core/internal/domain/org" +) + +// memberOf builds a principal serving orgID with one org-wide membership. +func memberOf(id identity.IdentityID, orgID org.OrgID, role org.Role) access.Principal { + return access.Principal{ + IdentityID: id, + Org: orgID, + Memberships: []org.Membership{{IdentityID: id, OrgID: orgID, Role: role}}, + } +} + +// TestInstanceOwner pins who may see and edit instance policies. Since kgxw.4 +// the shared token acts as the org-sys operator person, so ownership of org-sys +// is the test, not equality with the system identity; an owner of any other org +// and a lesser member of org-sys are refused. +func TestInstanceOwner(t *testing.T) { + tests := []struct { + name string + principal access.Principal + has bool + want bool + }{ + {"the operator person owning org-sys is the instance owner", operator(), true, true}, + {"the system fallback owning org-sys is the instance owner", access.SharedTokenOwner(), true, true}, + {"no principal falls back to the system owner", access.Principal{}, false, true}, + {"an admin of org-sys is not the instance owner", memberOf("admin", org.DefaultOrgID, org.RoleAdmin), true, false}, + {"a developer of org-sys is not the instance owner", memberOf("dev", org.DefaultOrgID, org.RoleDeveloper), true, false}, + {"an owner of another org is not the instance owner", memberOf("other", "acme", org.RoleOwner), true, false}, + { + "an org-sys owner membership while serving another org is not the instance owner", + access.Principal{ + IdentityID: "other", + Org: "acme", + Memberships: []org.Membership{{IdentityID: "other", OrgID: org.DefaultOrgID, Role: org.RoleOwner}}, + }, + true, false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := t.Context() + if tt.has { + ctx = access.WithPrincipal(ctx, tt.principal) + } + r := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/access/policies", nil) + if got := instanceOwner(r); got != tt.want { + t.Fatalf("instanceOwner = %v, want %v", got, tt.want) + } + }) + } +}