Files
archie/internal/webui/access_denials_test.go
sam db346b159d fix(webui): mark cookies Secure behind a TLS proxy; run gosec in the gate (#1919)
Cookies took Secure from r.TLS, which is nil behind a proxy that terminates
TLS, so sign-in cookies went out without it. Every dashboard cookie now goes
through one builder that reads the scheme the way the origin check does.

The worktree reset and the agent worker's chown walk use os.Root, so a
symlink swapped in mid-walk can't point them outside the worktree.

gosec runs in golangci-lint, tests included. G104, G115, G204 and G304 are
excluded in config; each remaining finding is fixed or suppressed at the site
with its reason.
2026-11-13 03:16:15 +11:00

137 lines
4.7 KiB
Go

package webui
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/org"
infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access"
)
// fakeDenialStore records the scope it was asked for and serves fixed rows.
type fakeDenialStore struct {
orgID org.OrgID
limit int
denials []access.Denial
}
func (f *fakeDenialStore) RecordDenial(context.Context, access.Denial) error { return nil }
func (f *fakeDenialStore) ListDenials(_ context.Context, orgID org.OrgID, limit int) ([]access.Denial, error) {
f.orgID, f.limit = orgID, limit
out := make([]access.Denial, 0, len(f.denials))
for _, d := range f.denials {
if d.Org == orgID {
out = append(out, d)
}
}
return out, nil
}
// TestDenialsRouteRequiresAdmin pins the denials route's grant: reading the
// org's denial records needs the same read_audit grant as the audit feed, so a
// viewer and a developer are refused and an admin reaches the handler.
func TestDenialsRouteRequiresAdmin(t *testing.T) {
const path = "/api/access/denials"
tests := []struct {
role org.Role
want int
}{
{org.RoleViewer, http.StatusForbidden},
{org.RoleDeveloper, http.StatusForbidden},
{org.RoleAdmin, http.StatusOK},
{org.RoleOwner, http.StatusOK},
}
for _, tt := range tests {
t.Run(string(tt.role), func(t *testing.T) {
principal := memberOf("alice", "acme", tt.role)
srv := &Server{
Accounts: accountBoundaryStore{},
Access: infraaccess.New(access.ShippedOrgPolicies("acme")),
Principals: principalOf(principal),
DenialLister: &fakeDenialStore{},
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://archie.example"+path, nil)
req.Header.Set("X-Archie-CSRF", "1")
sendCookie(req, "archie_session", "valid")
rec := httptest.NewRecorder()
srv.Handler().ServeHTTP(rec, req)
if rec.Code != tt.want {
t.Fatalf("status = %d, want %d body=%s", rec.Code, tt.want, rec.Body.String())
}
})
}
}
// TestHandleListDenialsScopesToCallersOrg pins the handler's scoping: the list
// is asked for the caller's org only, so another org's records are never
// fetched, and the limit comes from the query, clamped to the default.
func TestHandleListDenialsScopesToCallersOrg(t *testing.T) {
store := &fakeDenialStore{denials: []access.Denial{
{Org: "acme", Principal: "alice", Action: access.ActionRead, Kind: access.KindTask, ResourceID: "7", Level: access.LevelOrg},
{Org: "other", Principal: "bob", Action: access.ActionRead, Kind: access.KindTask, ResourceID: "9", Level: access.LevelOrg},
}}
srv := &Server{DenialLister: store}
tests := []struct {
name string
caller org.OrgID
query string
wantOrg org.OrgID
wantRows int
wantCap int
}{
{"the caller's org is the scope, with the query's limit", "acme", "?limit=5", "acme", 1, 5},
{"another org's caller gets that org, not acme's rows", "other", "", "other", 1, defaultDenialsListLimit},
{"an oversized limit falls back to the default", "acme", "?limit=5000", "acme", 1, defaultDenialsListLimit},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
principal := memberOf("alice", tt.caller, org.RoleAdmin)
ctx := org.WithOrg(access.WithPrincipal(t.Context(), principal), tt.caller)
req := httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/access/denials"+tt.query, nil)
rec := httptest.NewRecorder()
newAccessAPI(srv).handleListDenials(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200 body=%s", rec.Code, rec.Body.String())
}
if store.orgID != tt.wantOrg {
t.Fatalf("listed org = %q, want %q", store.orgID, tt.wantOrg)
}
if store.limit != tt.wantCap {
t.Fatalf("limit = %d, want %d", store.limit, tt.wantCap)
}
var body struct {
Denials []access.Denial `json:"denials"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
t.Fatalf("decode body: %v", err)
}
if len(body.Denials) != tt.wantRows {
t.Fatalf("rows = %d, want %d", len(body.Denials), tt.wantRows)
}
for _, d := range body.Denials {
if d.Org != tt.wantOrg {
t.Fatalf("row from org %q in a %q list", d.Org, tt.wantOrg)
}
}
})
}
}
// TestHandleListDenialsUnwired pins the unwired answer: a dashboard without
// the denial list answers 503 rather than an empty list.
func TestHandleListDenialsUnwired(t *testing.T) {
srv := &Server{}
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/access/denials", nil)
rec := httptest.NewRecorder()
newAccessAPI(srv).handleListDenials(rec, req)
if rec.Code != http.StatusServiceUnavailable {
t.Fatalf("status = %d, want 503", rec.Code)
}
}