fix(webui): a source create is authorized against the workspace it names

Issue: archie-core-ed8tw
This commit is contained in:
sam committed 2026-10-13 16:00:37 +11:00
1 parent ed5dae1017
commit c3933c6628
3 files changed
+53

No files matched your search

+4
View File
@@ -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)
+18
View File
@@ -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
+31
View File
@@ -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)
}
})
}
}