merge: proxy-aware Origin validation for dashboard mutations (crew/proxyfix)

This commit is contained in:
sam committed 2026-08-25 02:06:05 +10:00
commit 45fbed4228
9 files changed
+406 -12

No files matched your search

+13
View File
@@ -179,6 +179,19 @@ class = "ollama"
# Set to "off" (or leave empty) to disable.
[web]
listen = "127.0.0.1:8484"
# Trust X-Forwarded-Proto and X-Forwarded-Host headers when validating Origin
# on mutating requests (CSRF protection).
#
# Off by default. When false, the expected Origin scheme is derived directly
# from the daemon's own network connection (http unless TLS is terminated by
# archied directly). Setting this to true causes archied to respect the
# X-Forwarded-Proto header sent by a reverse proxy (e.g. Traefik, Caddy, Nginx).
#
# SECURITY WARNING: Enable ONLY when archied is deployed behind a trusted
# reverse proxy that strips or overwrites any client-supplied X-Forwarded-*
# headers. Enabling this on a directly exposed daemon allows untrusted
# clients to spoof the Origin scheme and bypass CSRF protection.
# trust_forwarded_headers = false
# Where archied writes its logs. Without file set, output goes only to
# stderr -- which under systemd means journald holds the sole copy, and
+3
View File
@@ -58,6 +58,9 @@ pull_policy = "missing"
[web]
listen = "127.0.0.1:8484"
# Trust X-Forwarded-Proto when fronted by a reverse proxy (e.g. Traefik)
# terminating TLS so dashboard mutations succeed with external HTTPS Origin.
trust_forwarded_headers = true
[models]
triage = "deepseek/deepseek-v4-pro"
+7
View File
@@ -825,4 +825,11 @@ type Web struct {
// Listen is the dashboard address; "off" disables the web UI.
// Bind localhost (or a LAN/tailnet address) -- there is no auth.
Listen string `toml:"listen" yaml:"listen"`
// TrustForwardedHeaders enables deriving the effective request scheme and
// host from X-Forwarded-Proto and X-Forwarded-Host headers during mutation
// Origin validation. Off by default: trusting forwarded headers
// unconditionally allows untrusted clients to spoof Origin scheme checks
// and bypass CSRF protection. Enable ONLY when archied is deployed behind
// a trusted reverse proxy that overwrites/strips untrusted forwarded headers.
TrustForwardedHeaders bool `toml:"trust_forwarded_headers" yaml:"trust_forwarded_headers"`
}
+43
View File
@@ -418,3 +418,46 @@ func TestEcosystemCustom(t *testing.T) {
t.Errorf("ResolvedPreflight custom: got %v, want nil", got)
}
}
func TestWebConfigTOML(t *testing.T) {
tests := []struct {
name string
input string
wantListen string
wantTrustForwarded bool
}{
{
name: "default trust_forwarded_headers is false when absent",
input: `[web]` + "\n" + `listen = "127.0.0.1:8484"`,
wantListen: "127.0.0.1:8484",
wantTrustForwarded: false,
},
{
name: "explicit trust_forwarded_headers true",
input: `[web]` + "\n" + `listen = "127.0.0.1:8484"` + "\n" + `trust_forwarded_headers = true`,
wantListen: "127.0.0.1:8484",
wantTrustForwarded: true,
},
{
name: "explicit trust_forwarded_headers false",
input: `[web]` + "\n" + `listen = "127.0.0.1:8484"` + "\n" + `trust_forwarded_headers = false`,
wantListen: "127.0.0.1:8484",
wantTrustForwarded: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var cfg Config
if _, err := toml.Decode(tc.input, &cfg); err != nil {
t.Fatalf("decode: %v", err)
}
if cfg.Web.Listen != tc.wantListen {
t.Errorf("Web.Listen = %q, want %q", cfg.Web.Listen, tc.wantListen)
}
if cfg.Web.TrustForwardedHeaders != tc.wantTrustForwarded {
t.Errorf("Web.TrustForwardedHeaders = %v, want %v", cfg.Web.TrustForwardedHeaders, tc.wantTrustForwarded)
}
})
}
}
+8 -4
View File
@@ -70,7 +70,7 @@ func (s *Server) handleChannels(w http.ResponseWriter, r *http.Request) {
}
func (s *Server) handleChannelReload(w http.ResponseWriter, r *http.Request) {
if !authorizeTaskMutation(w, r) {
if !s.authorizeTaskMutation(w, r) {
return
}
id := r.PathValue("id")
@@ -290,9 +290,10 @@ type ContainersView struct {
Network string `json:"network,omitempty"`
}
// WebView is the dashboard's own listen address.
// WebView is the dashboard's own listen address and proxy header trust settings.
type WebView struct {
Listen string `json:"listen"`
Listen string `json:"listen"`
TrustForwardedHeaders bool `json:"trust_forwarded_headers"`
}
// handleConfig returns a read-only, secret-free view of the running
@@ -353,7 +354,10 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
PullPolicy: cfg.Containers.PullPolicy,
Network: cfg.Containers.Network,
},
Web: WebView{Listen: cfg.Web.Listen},
Web: WebView{
Listen: cfg.Web.Listen,
TrustForwardedHeaders: cfg.Web.TrustForwardedHeaders,
},
Provenance: provenance,
Locked: lockedConfigKeys(),
}
+50 -7
View File
@@ -82,7 +82,7 @@ type taskActionRequest struct {
}
func (s *Server) handleTaskAction(w http.ResponseWriter, r *http.Request) {
if !authorizeTaskMutation(w, r) {
if !s.authorizeTaskMutation(w, r) {
return
}
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
@@ -194,7 +194,14 @@ func taskMutation(action taskstate.Action) bool {
// boundary: JSON only, a non-simple custom header, and a matching Origin when
// the browser supplies one. The custom header forces cross-origin callers
// through a CORS preflight, which this server never permits.
func authorizeTaskMutation(w http.ResponseWriter, r *http.Request) bool {
//
// Origin comparison previously validated against the daemon's own direct network
// connection (r.TLS). It now validates against the effective external scheme
// and host, deriving them from X-Forwarded-Proto and X-Forwarded-Host only when
// explicit proxy header trust is enabled (Web.TrustForwardedHeaders). When trust
// is disabled (the default), forwarded headers are ignored to prevent untrusted
// clients from forging Origin scheme checks on exposed daemons.
func (s *Server) authorizeTaskMutation(w http.ResponseWriter, r *http.Request) bool {
if r.Header.Get("X-Archie-CSRF") != "1" {
http.Error(w, "missing CSRF header", http.StatusForbidden)
return false
@@ -209,18 +216,54 @@ func authorizeTaskMutation(w http.ResponseWriter, r *http.Request) bool {
return true
}
u, err := url.Parse(origin)
wantScheme := "http"
if r.TLS != nil {
wantScheme = "https"
if err != nil {
http.Error(w, "cross-origin mutation refused", http.StatusForbidden)
return false
}
if err != nil || u.User != nil || u.Path != "" || u.RawQuery != "" || u.Fragment != "" ||
!strings.EqualFold(u.Scheme, wantScheme) || !strings.EqualFold(u.Host, r.Host) {
wantScheme, wantHost := s.expectedOrigin(r)
if !validOrigin(u, wantScheme, wantHost) {
http.Error(w, "cross-origin mutation refused", http.StatusForbidden)
return false
}
return true
}
func (s *Server) expectedOrigin(r *http.Request) (scheme, host string) {
scheme = "http"
if r.TLS != nil {
scheme = "https"
}
host = r.Host
if s == nil || !s.trustForwardedHeaders() {
return scheme, host
}
if proto := firstForwardedValue(r.Header.Get("X-Forwarded-Proto")); proto != "" {
scheme = strings.ToLower(proto)
}
if fHost := firstForwardedValue(r.Header.Get("X-Forwarded-Host")); fHost != "" {
host = fHost
}
return scheme, host
}
func firstForwardedValue(raw string) string {
raw = strings.TrimSpace(raw)
if raw == "" {
return ""
}
if first, _, ok := strings.Cut(raw, ","); ok {
raw = first
}
return strings.TrimSpace(raw)
}
func validOrigin(u *url.URL, wantScheme, wantHost string) bool {
if u == nil || u.User != nil || u.Path != "" || u.RawQuery != "" || u.Fragment != "" {
return false
}
return strings.EqualFold(u.Scheme, wantScheme) && strings.EqualFold(u.Host, wantHost)
}
// actionOutcome is what a completed operator action should record.
type actionOutcome struct {
kind string
+265
View File
@@ -0,0 +1,265 @@
package webui
import (
"crypto/tls"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
func TestAuthorizeTaskMutationOrigin(t *testing.T) {
tests := []struct {
name string
trustForwardedDirect bool
trustForwardedCfg *bool
forwardedProto string
forwardedHost string
requestHost string
tls bool
origin string
csrfHeader string
contentType string
wantStatus int
wantBodyContains string
}{
{
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: "1b. proxied https: trust enabled via Cfg holder, X-Forwarded-Proto https, plain HTTP, Origin https",
trustForwardedCfg: boolPtr(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,
wantBodyContains: "cross-origin mutation refused",
},
{
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,
wantBodyContains: "cross-origin mutation refused",
},
{
name: "5. missing CSRF header: returns 403 with distinct message",
trustForwardedDirect: true,
forwardedProto: "https",
requestHost: "archie.catlow.cloud",
tls: false,
origin: "https://archie.catlow.cloud",
csrfHeader: "",
contentType: "application/json",
wantStatus: http.StatusForbidden,
wantBodyContains: "missing CSRF header",
},
{
name: "6. genuinely cross-origin request: returns 403 cross-origin mutation refused",
trustForwardedDirect: true,
forwardedProto: "https",
requestHost: "archie.catlow.cloud",
tls: false,
origin: "https://evil.example.com",
csrfHeader: "1",
contentType: "application/json",
wantStatus: http.StatusForbidden,
wantBodyContains: "cross-origin mutation refused",
},
{
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,
wantBodyContains: "cross-origin mutation refused",
},
{
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,
wantBodyContains: "cross-origin mutation refused",
},
{
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,
wantBodyContains: "Content-Type must be application/json",
},
}
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
if tc.trustForwardedCfg != nil {
srv.Cfg = config.NewHolder(config.Config{
Web: config.Web{
TrustForwardedHeaders: *tc.trustForwardedCfg,
},
})
}
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.wantBodyContains != "" && !strings.Contains(w.Body.String(), tc.wantBodyContains) {
t.Fatalf("body %q does not contain %q", w.Body.String(), tc.wantBodyContains)
}
})
}
}
func boolPtr(b bool) *bool {
return &b
}
func TestWorkRequestOriginProxy(t *testing.T) {
srv := newTestServer(t)
creator := &recordingWorkRequestCreator{}
srv.WorkRequests = creator
srv.Workflows = []workflow.Definition{{ID: "implement", Name: "Implement", Enabled: true}}
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())
}
}
+1 -1
View File
@@ -42,7 +42,7 @@ type workRequest struct {
}
func (s *Server) handleWorkRequest(w http.ResponseWriter, r *http.Request) {
if !authorizeTaskMutation(w, r) {
if !s.authorizeTaskMutation(w, r) {
return
}
if s.WorkRequests == nil {
+16
View File
@@ -169,10 +169,26 @@ type Server struct {
// than Confirmed.
RunningVersions func() map[string]string
// TrustForwardedHeaders controls whether X-Forwarded-Proto and
// X-Forwarded-Host are trusted when validating Origin on mutating
// requests. When false, Origin scheme must match the direct connection (r.TLS).
// When Cfg is present, Cfg.Get().Web.TrustForwardedHeaders is also checked.
TrustForwardedHeaders bool
mu sync.Mutex
conns map[chan events.Event]struct{}
}
func (s *Server) trustForwardedHeaders() bool {
if s.TrustForwardedHeaders {
return true
}
if s.Cfg == nil {
return false
}
return s.Cfg.Get().Web.TrustForwardedHeaders
}
// ConfigOrigin explains one source file contributing to the effective
// configuration. Sources are ordered from lowest to highest precedence.
type ConfigOrigin struct {