Files
archie/internal/webui/api_tasks_origin_test.go
T
sam 924b02a1b0 feat(controlplane): runtime administration over the State Store's gRPC server
The Web UI and messaging channels now read and write Archie's runtime
settings through one versioned, audited control-plane API hosted on the
State Store's existing gRPC server: catalog, query, command, watch. Each
feature owns its resource, validation and commands; storage only writes
versions and audit records.

Workflows move with it. Definitions become portable YAML compiled through
a closed, typed step registry and stored as a control-plane resource, so
the yaegi interpretation paths (internal/gate, wfeval, wfextract,
gateeval, gateextract, skillbuild) are deleted rather than carried.

The UI process reads definitions as data: the JSON projection lives in
internal/domain/workflow/task so a dashboard decodes the stored
collection without linking the workflow engine, which cmd/archie-ui's
deletion gate forbids. Shipped definitions ride the catalog descriptor
rather than a bespoke REST route, keeping the API to its four operations.

archie-messaging gains -state-store-target/-state-store-token and reads
channel settings from the database, failing closed when the State Store
is unreachable: the database is authoritative for those settings.
2026-09-21 08:17:42 +10:00

239 lines
8.1 KiB
Go

package webui
import (
"crypto/tls"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
func TestAuthorizeTaskMutationOrigin(t *testing.T) {
tests := []struct {
name string
trustForwardedDirect bool
forwardedProto string
forwardedHost string
requestHost string
tls bool
origin string
csrfHeader string
contentType string
wantStatus int
}{
{
name: "1. proxied https: trust enabled directly, X-Forwarded-Proto https, plain HTTP, Origin https",
trustForwardedDirect: true,
forwardedProto: "https",
requestHost: "archie.catlow.cloud",
tls: false,
origin: "https://archie.catlow.cloud",
csrfHeader: "1",
contentType: "application/json",
wantStatus: http.StatusOK,
},
{
name: "1c. proxied https: comma-separated X-Forwarded-Proto list",
trustForwardedDirect: true,
forwardedProto: "https, http",
requestHost: "archie.catlow.cloud",
tls: false,
origin: "https://archie.catlow.cloud",
csrfHeader: "1",
contentType: "application/json",
wantStatus: http.StatusOK,
},
{
name: "1d. proxied https: X-Forwarded-Host trusted and matched",
trustForwardedDirect: true,
forwardedProto: "https",
forwardedHost: "archie.catlow.cloud",
requestHost: "10.36.25.188:8484",
tls: false,
origin: "https://archie.catlow.cloud",
csrfHeader: "1",
contentType: "application/json",
wantStatus: http.StatusOK,
},
{
name: "2. direct http: trust disabled, no forwarded header, Origin http",
trustForwardedDirect: false,
requestHost: "localhost:8484",
tls: false,
origin: "http://localhost:8484",
csrfHeader: "1",
contentType: "application/json",
wantStatus: http.StatusOK,
},
{
name: "3. direct https: trust disabled, r.TLS set, Origin https",
trustForwardedDirect: false,
requestHost: "localhost:8484",
tls: true,
origin: "https://localhost:8484",
csrfHeader: "1",
contentType: "application/json",
wantStatus: http.StatusOK,
},
{
name: "4. SPOOFED: trust disabled, attacker sends X-Forwarded-Proto https, plain HTTP, Origin https",
trustForwardedDirect: false,
forwardedProto: "https",
requestHost: "localhost:8484",
tls: false,
origin: "https://localhost:8484",
csrfHeader: "1",
contentType: "application/json",
wantStatus: http.StatusForbidden,
},
{
name: "4b. SPOOFED HOST: trust disabled, attacker sends X-Forwarded-Host, plain HTTP",
trustForwardedDirect: false,
forwardedHost: "target.example.com",
requestHost: "localhost:8484",
tls: false,
origin: "http://target.example.com",
csrfHeader: "1",
contentType: "application/json",
wantStatus: http.StatusForbidden,
},
{
name: "5. missing CSRF header: request rejected",
trustForwardedDirect: true,
forwardedProto: "https",
requestHost: "archie.catlow.cloud",
tls: false,
origin: "https://archie.catlow.cloud",
csrfHeader: "",
contentType: "application/json",
wantStatus: http.StatusForbidden,
},
{
name: "6. genuinely cross-origin request: request rejected",
trustForwardedDirect: true,
forwardedProto: "https",
requestHost: "archie.catlow.cloud",
tls: false,
origin: "https://evil.example.com",
csrfHeader: "1",
contentType: "application/json",
wantStatus: http.StatusForbidden,
},
{
name: "7. invalid Origin URI: containing userinfo",
trustForwardedDirect: true,
forwardedProto: "https",
requestHost: "archie.catlow.cloud",
tls: false,
origin: "https://user:pass@archie.catlow.cloud",
csrfHeader: "1",
contentType: "application/json",
wantStatus: http.StatusForbidden,
},
{
name: "8. invalid Origin URI: containing path",
trustForwardedDirect: true,
forwardedProto: "https",
requestHost: "archie.catlow.cloud",
tls: false,
origin: "https://archie.catlow.cloud/path",
csrfHeader: "1",
contentType: "application/json",
wantStatus: http.StatusForbidden,
},
{
name: "9. unsupported media type: non-JSON body",
trustForwardedDirect: true,
forwardedProto: "https",
requestHost: "archie.catlow.cloud",
tls: false,
origin: "https://archie.catlow.cloud",
csrfHeader: "1",
contentType: "text/plain",
wantStatus: http.StatusUnsupportedMediaType,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
srv := newTestServer(t)
ctx := t.Context()
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "queued", "", "", ""); err != nil {
t.Fatal(err)
}
task, err := srv.Store.TaskByIssue(ctx, "acme", "widget", 1)
if err != nil || task == nil {
t.Fatalf("failed to fetch enqueued task: %v", err)
}
srv.TrustForwardedHeaders = tc.trustForwardedDirect
path := "/api/tasks/" + strconv.FormatInt(task.ID, 10) + "/action"
req := httptest.NewRequestWithContext(ctx, http.MethodPost, path, strings.NewReader(`{"action":"cancel"}`))
if tc.requestHost != "" {
req.Host = tc.requestHost
}
if tc.tls {
req.TLS = &tls.ConnectionState{}
}
if tc.forwardedProto != "" {
req.Header.Set("X-Forwarded-Proto", tc.forwardedProto)
}
if tc.forwardedHost != "" {
req.Header.Set("X-Forwarded-Host", tc.forwardedHost)
}
if tc.origin != "" {
req.Header.Set("Origin", tc.origin)
}
if tc.csrfHeader != "" {
req.Header.Set("X-Archie-CSRF", tc.csrfHeader)
}
if tc.contentType != "" {
req.Header.Set("Content-Type", tc.contentType)
}
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != tc.wantStatus {
t.Fatalf("status = %d, want %d (body %q)", w.Code, tc.wantStatus, w.Body.String())
}
if tc.wantStatus >= http.StatusBadRequest {
if got := w.Header().Get("Content-Type"); got != "text/plain; charset=utf-8" {
t.Errorf("Content-Type = %q, want text/plain error response", got)
}
if got := w.Header().Get("X-Content-Type-Options"); got != "nosniff" {
t.Errorf("X-Content-Type-Options = %q, want nosniff", got)
}
}
})
}
}
func TestWorkRequestOriginProxy(t *testing.T) {
srv := newTestServer(t)
creator := &recordingWorkRequestCreator{}
srv.WorkRequests = creator
srv.ControlPlane = workflowControlPlane(t, workflow.WorkflowDefinitionCollection{Definitions: []workflow.WorkflowDefinitionEntry{{ID: "implement", YAML: "id: implement\nsteps:\n - type: implement.prepare\n"}}})
srv.TrustForwardedHeaders = true
body := `{"identity":"archie","repository":"acme/widget","workflow":"implement","title":"Fix login","instructions":"Reproduce and fix it."}`
req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/api/work-requests", strings.NewReader(body))
req.Host = "archie.catlow.cloud"
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Archie-CSRF", "1")
req.Header.Set("X-Forwarded-Proto", "https")
req.Header.Set("Origin", "https://archie.catlow.cloud")
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusCreated {
t.Fatalf("status = %d, want 201 (body %q)", w.Code, w.Body.String())
}
}