From c3933c6628bbfbcba567f9dacca5a6fbfd2217b3 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 13 Oct 2026 16:00:37 +1100 Subject: [PATCH] fix(webui): a source create is authorized against the workspace it names Issue: archie-core-ed8tw --- internal/webui/api_source.go | 4 ++++ internal/webui/authorize.go | 18 ++++++++++++++++++ internal/webui/authorize_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/internal/webui/api_source.go b/internal/webui/api_source.go index 1c3c6d910..68ee86ccc 100644 --- a/internal/webui/api_source.go +++ b/internal/webui/api_source.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/domain/source" "github.com/samcharles93/archie-core/internal/domain/storecontract" @@ -55,6 +56,9 @@ func (s *Server) handleSourceCreate(w http.ResponseWriter, r *http.Request) { http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest) return } + if !s.authorizeCreateIn(w, r, access.KindSource, req.Workspace) { + return + } src, err := source.New(req.Path) if errors.Is(err, source.ErrInvalidPath) { http.Error(w, err.Error(), http.StatusBadRequest) diff --git a/internal/webui/authorize.go b/internal/webui/authorize.go index 33d193876..9befd16cf 100644 --- a/internal/webui/authorize.go +++ b/internal/webui/authorize.go @@ -73,6 +73,24 @@ func (s *Server) authorize(h http.Handler) http.Handler { }) } +// authorizeCreateIn checks a create against the workspace the body names, which +// the route-level check cannot see: no record exists yet to read it from. An +// empty workspace is the default one, which that check already covered. +func (s *Server) authorizeCreateIn(w http.ResponseWriter, r *http.Request, kind access.ResourceKind, workspace org.WorkspaceID) bool { + principal, ok := access.PrincipalFromContext(r.Context()) + if workspace == "" || !ok { + return true + } + resource := access.Resource{Kind: kind, Org: principal.Org, Workspace: workspace} + decision := s.Access.Authorize(principal, access.ActionCreate, resource, access.Context{}) + if decision.Allowed { + return true + } + s.recordDenial(r.Context(), principal, access.ActionCreate, resource, decision) + http.Error(w, "forbidden", http.StatusForbidden) + return false +} + // recordWorkspace returns the workspace of the record a request names, so // workspace policies and per-workspace roles decide for it. A request naming // no record, or one the caller's org does not hold, has none: the handler's diff --git a/internal/webui/authorize_test.go b/internal/webui/authorize_test.go index 9d12d2cdd..5a39581c6 100644 --- a/internal/webui/authorize_test.go +++ b/internal/webui/authorize_test.go @@ -319,3 +319,34 @@ func TestAuthorizeRecord(t *testing.T) { }) } } + +// TestAuthorizeCreateIn pins that a create is decided in the workspace its +// body names, where the role held there replaces the org role. +func TestAuthorizeCreateIn(t *testing.T) { + dev := access.Principal{IdentityID: "dev", Org: org.DefaultOrgID, Memberships: []org.Membership{ + {IdentityID: "dev", OrgID: org.DefaultOrgID, Role: org.RoleDeveloper}, + {IdentityID: "dev", OrgID: org.DefaultOrgID, WorkspaceID: "locked", Role: org.RoleViewer}, + }} + tests := []struct { + name string + workspace org.WorkspaceID + want int + }{ + {"the default workspace was decided at the route", "", http.StatusOK}, + {"a workspace the org role covers", "open", http.StatusOK}, + {"a workspace where the caller is only a viewer", "locked", http.StatusForbidden}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + s := &Server{Access: infraaccess.New(access.ShippedOrgPolicies(org.DefaultOrgID))} + req := httptest.NewRequestWithContext(access.WithPrincipal(t.Context(), dev), http.MethodPost, "/api/sources", nil) + rec := httptest.NewRecorder() + if s.authorizeCreateIn(rec, req, access.KindSource, tt.workspace) { + rec.WriteHeader(http.StatusOK) + } + if rec.Code != tt.want { + t.Fatalf("create in %q = %d, want %d", tt.workspace, rec.Code, tt.want) + } + }) + } +}