package webui import ( "context" "net/http" "net/http/httptest" "strings" "testing" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/workflow/task" infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access" ) type authorizationTasks struct{ storecontract.TaskStore } func (authorizationTasks) TaskByID(context.Context, int64) (*task.Task, error) { return &task.Task{ID: 7}, nil } // These are the policy-governed HTTP surfaces. Protocol entry points and // private account resources have separate credential/ownership behavior tests. func TestHTTPPolicyCannotBeBypassed(t *testing.T) { routes := []struct{ method, path string }{ {"DELETE", "/api/access/policies"}, {"DELETE", "/api/bindings/7"}, {"DELETE", "/api/event-types/7"}, {"DELETE", "/api/extensions/sample"}, {"DELETE", "/api/mappings/7"}, {"DELETE", "/api/orgs/7/members/sample"}, {"DELETE", "/api/sources/sample"}, {"DELETE", "/api/tokens/7"}, {"GET", "/api/access/policies"}, {"GET", "/api/alerts"}, {"GET", "/api/authentication"}, {"GET", "/api/bindings"}, {"GET", "/api/bindings/7"}, {"GET", "/api/bindings/7/dispatches"}, {"GET", "/api/capabilities"}, {"GET", "/api/captures"}, {"GET", "/api/captures/7/dispatches"}, {"GET", "/api/channels"}, {"GET", "/api/chat/file/sample"}, {"GET", "/api/chat/sessions"}, {"GET", "/api/chat/sessions/7/messages"}, {"GET", "/api/chat/sessions/7/turns"}, {"GET", "/api/chat/update"}, {"GET", "/api/config"}, {"GET", "/api/control-plane/apply-status"}, {"GET", "/api/control-plane/audit"}, {"GET", "/api/control-plane/catalog"}, {"GET", "/api/control-plane/resources/sample"}, {"GET", "/api/control-plane/resources/sample/history"}, {"GET", "/api/curators"}, {"GET", "/api/event-types"}, {"GET", "/api/extensions"}, {"GET", "/api/extensions/catalogue"}, {"GET", "/api/harness/bindings"}, {"GET", "/api/harness/terminal"}, {"GET", "/api/identities"}, {"GET", "/api/logs"}, {"GET", "/api/repositories"}, {"GET", "/api/mappings"}, {"GET", "/api/mappings/7"}, {"GET", "/api/orgs"}, {"GET", "/api/orgs/7"}, {"GET", "/api/orgs/7/members"}, {"GET", "/api/orgs/7/workspaces"}, {"GET", "/api/secret-engines"}, {"GET", "/api/services"}, {"GET", "/api/setup"}, {"GET", "/api/skills"}, {"GET", "/api/sources"}, {"GET", "/api/stream"}, {"GET", "/api/summary"}, {"GET", "/api/task-meta"}, {"GET", "/api/tasks"}, {"GET", "/api/tasks/7"}, {"GET", "/api/tasks/7/attempts"}, {"GET", "/api/tasks/7/changes"}, {"GET", "/api/tasks/7/debug"}, {"GET", "/api/tasks/7/dispatches"}, {"GET", "/api/tasks/7/logs"}, {"GET", "/api/tasks/7/logs/download"}, {"GET", "/api/tasks/7/sandbox"}, {"GET", "/api/tokens"}, {"GET", "/api/version"}, {"GET", "/api/workflows"}, {"PATCH", "/api/bindings/7"}, {"PATCH", "/api/mappings/7"}, {"POST", "/api/accounts"}, {"POST", "/api/alerts/7/dismiss"}, {"POST", "/api/bindings"}, {"POST", "/api/bindings/7/approve"}, {"POST", "/api/bindings/7/pause"}, {"POST", "/api/bindings/7/resume"}, {"POST", "/api/channels/7/reload"}, {"POST", "/api/chat/answer"}, {"POST", "/api/chat/cancel"}, {"POST", "/api/chat/message"}, {"POST", "/api/chat/stream"}, {"POST", "/api/chat/update/defer"}, {"POST", "/api/chat/update/install"}, {"POST", "/api/control-plane/resources/sample/commands/sample"}, {"POST", "/api/event-types"}, {"POST", "/api/extensions"}, {"POST", "/api/extensions/catalogue/sample/install"}, {"POST", "/api/extensions/sample/accept"}, {"POST", "/api/extensions/sample/approve-update"}, {"POST", "/api/extensions/sample/rollback"}, {"POST", "/api/extensions/sample/update"}, {"POST", "/api/identities"}, {"POST", "/api/identities/7/sample"}, {"POST", "/api/mappings"}, {"POST", "/api/mappings/preview"}, {"POST", "/api/orgs"}, {"POST", "/api/orgs/7/members"}, {"POST", "/api/orgs/7/workspaces"}, {"POST", "/api/secret-references/check"}, {"POST", "/api/sources"}, {"POST", "/api/sources/sample/approve-unsigned"}, {"POST", "/api/sources/sample/secret"}, {"POST", "/api/sources/sample/signing"}, {"POST", "/api/tasks/7/action"}, {"POST", "/api/tokens"}, {"POST", "/api/work-requests"}, {"POST", "/api/control-plane/resources/repository-policies/check"}, {"PUT", "/api/access/policies"}, {"PUT", "/api/authentication"}, {"PUT", "/api/event-types/7"}, {"PUT", "/api/extensions/sample/enabled"}, {"PUT", "/api/extensions/sample/update-policy"}, {"PUT", "/api/orgs/7/agents/sample"}, {"PUT", "/api/orgs/7/members/sample"}, {"PUT", "/api/sources/sample/delivery-header"}, {"PUT", "/api/sources/sample/name"}, {"PUT", "/api/workflows/7/enabled"}, } principal := access.Principal{IdentityID: "alice", Org: "acme", Memberships: []org.Membership{{IdentityID: "alice", OrgID: "acme", Role: org.RoleAdmin}}} policy := access.Policy{ID: "deny-all", Level: access.LevelInstance, Text: `forbid(principal,action,resource);`} for _, route := range routes { t.Run(route.method+" "+route.path, func(t *testing.T) { for _, signedIn := range []bool{false, true} { srv := &Server{Store: authorizationTasks{}, Accounts: accountBoundaryStore{}, Access: infraaccess.New(append(access.ShippedOrgPolicies("acme"), policy)), Principals: principalOf(principal)} req := httptest.NewRequestWithContext(t.Context(), route.method, "https://archie.example"+route.path, strings.NewReader(`{}`)) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Archie-CSRF", "1") want := http.StatusUnauthorized if signedIn { req.AddCookie(&http.Cookie{Name: "archie_session", Value: "valid"}) want = http.StatusForbidden } rec := httptest.NewRecorder() srv.Handler().ServeHTTP(rec, req) if rec.Code != want { t.Fatalf("signedIn=%v status=%d want=%d body=%s", signedIn, rec.Code, want, rec.Body.String()) } } }) } }