From 97a9ed7ea557dcb055e9608913dce7e91879d08c Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 15 Sep 2026 04:35:04 +1000 Subject: [PATCH] fix(webui): enforce CSRF/origin gate on binding and mapping mutations handleTaskAction, the config and workflow write handlers all call authorizeTaskMutation before mutating, but the binding and mapping create/update/delete/approve handlers never did, leaving those dashboard endpoints reachable by a cross-origin request with no CSRF header - contradicting dynamic-curators.md's documented mutation contract. --- internal/webui/api_binding.go | 12 ++++++ internal/webui/api_binding_test.go | 34 +++++++++++++++++ internal/webui/api_mapping.go | 9 +++++ internal/webui/api_mapping_test.go | 59 ++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+) diff --git a/internal/webui/api_binding.go b/internal/webui/api_binding.go index d43cbe8bf..1e44e46c5 100644 --- a/internal/webui/api_binding.go +++ b/internal/webui/api_binding.go @@ -43,6 +43,9 @@ func (s *Server) handleBindingsList(w http.ResponseWriter, r *http.Request) { } func (s *Server) handleBindingCreate(w http.ResponseWriter, r *http.Request) { + if !s.authorizeTaskMutation(w, r) { + return + } if s.Bindings == nil { http.Error(w, "bindings not configured", http.StatusServiceUnavailable) return @@ -116,6 +119,9 @@ func (s *Server) handleBindingGet(w http.ResponseWriter, r *http.Request) { } func (s *Server) handleBindingUpdate(w http.ResponseWriter, r *http.Request) { + if !s.authorizeTaskMutation(w, r) { + return + } if s.Bindings == nil { http.Error(w, "bindings not configured", http.StatusServiceUnavailable) return @@ -171,6 +177,9 @@ func (s *Server) handleBindingUpdate(w http.ResponseWriter, r *http.Request) { } func (s *Server) handleBindingDelete(w http.ResponseWriter, r *http.Request) { + if !s.authorizeTaskMutation(w, r) { + return + } if s.Bindings == nil { http.Error(w, "bindings not configured", http.StatusServiceUnavailable) return @@ -193,6 +202,9 @@ func (s *Server) handleBindingDelete(w http.ResponseWriter, r *http.Request) { } func (s *Server) handleBindingApprove(w http.ResponseWriter, r *http.Request) { + if !s.authorizeTaskMutation(w, r) { + return + } if s.Bindings == nil { http.Error(w, "bindings not configured", http.StatusServiceUnavailable) return diff --git a/internal/webui/api_binding_test.go b/internal/webui/api_binding_test.go index 885ef9ea8..129ec0257 100644 --- a/internal/webui/api_binding_test.go +++ b/internal/webui/api_binding_test.go @@ -414,3 +414,37 @@ func TestHandleBindingsUnavailableWhenNotConfigured(t *testing.T) { t.Fatalf("status = %d, want %d", w.Code, http.StatusServiceUnavailable) } } + +// TestHandleBindingMutationsRequireCSRFHeader pins the same browser +// mutation contract handleTaskAction enforces (docs/prds/dynamic-curators.md: +// "All mutations behind authorizeTaskMutation, same as bindings and +// mappings") onto every binding write handler. A request with no +// X-Archie-CSRF header must be refused, not silently mutate the store. +func TestHandleBindingMutationsRequireCSRFHeader(t *testing.T) { + srv := bindingTestServer(t) + mappingID := seedMapping(t, srv, "m") + w := doJSON(t, srv, http.MethodPost, "/api/bindings", validBindingRequest("a", "sentry", mappingID)) + var created binding.Binding + if err := json.Unmarshal(w.Body.Bytes(), &created); err != nil { + t.Fatalf("unmarshal created binding: %v; body = %s", err, w.Body.String()) + } + id := strconv.FormatInt(created.ID, 10) + + tests := []struct { + name, method, path string + body any + }{ + {"create", http.MethodPost, "/api/bindings", validBindingRequest("b", "other", mappingID)}, + {"update", http.MethodPatch, "/api/bindings/" + id, validBindingRequest("a", "sentry", mappingID)}, + {"approve", http.MethodPost, "/api/bindings/" + id + "/approve", nil}, + {"delete", http.MethodDelete, "/api/bindings/" + id, nil}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + w := doJSONNoCSRF(t, srv, tc.method, tc.path, tc.body) + if w.Code != http.StatusForbidden { + t.Fatalf("status = %d, want %d without a CSRF header; body = %s", w.Code, http.StatusForbidden, w.Body.String()) + } + }) + } +} diff --git a/internal/webui/api_mapping.go b/internal/webui/api_mapping.go index 5d5d2b070..2b5f62ab4 100644 --- a/internal/webui/api_mapping.go +++ b/internal/webui/api_mapping.go @@ -35,6 +35,9 @@ func (s *Server) handleMappingsList(w http.ResponseWriter, r *http.Request) { } func (s *Server) handleMappingCreate(w http.ResponseWriter, r *http.Request) { + if !s.authorizeTaskMutation(w, r) { + return + } if s.Mappings == nil { http.Error(w, "mappings not configured", http.StatusServiceUnavailable) return @@ -89,6 +92,9 @@ func (s *Server) handleMappingGet(w http.ResponseWriter, r *http.Request) { } func (s *Server) handleMappingUpdate(w http.ResponseWriter, r *http.Request) { + if !s.authorizeTaskMutation(w, r) { + return + } if s.Mappings == nil { http.Error(w, "mappings not configured", http.StatusServiceUnavailable) return @@ -127,6 +133,9 @@ func (s *Server) handleMappingUpdate(w http.ResponseWriter, r *http.Request) { } func (s *Server) handleMappingDelete(w http.ResponseWriter, r *http.Request) { + if !s.authorizeTaskMutation(w, r) { + return + } if s.Mappings == nil { http.Error(w, "mappings not configured", http.StatusServiceUnavailable) return diff --git a/internal/webui/api_mapping_test.go b/internal/webui/api_mapping_test.go index c1c203d30..bc0896e65 100644 --- a/internal/webui/api_mapping_test.go +++ b/internal/webui/api_mapping_test.go @@ -32,7 +32,24 @@ func mappingTestServer(t *testing.T) *Server { } } +// doJSON issues a request carrying the browser mutation headers +// (Content-Type, X-Archie-CSRF) every write handler requires via +// authorizeTaskMutation, so GET/POST/PATCH/DELETE calls in this package's +// tests exercise the handler logic rather than the CSRF gate. Use +// doJSONNoCSRF to test the gate itself. func doJSON(t *testing.T, srv *Server, method, path string, body any) *httptest.ResponseRecorder { + t.Helper() + return doJSONWithHeaders(t, srv, method, path, body, true) +} + +// doJSONNoCSRF is doJSON without the X-Archie-CSRF header, for asserting +// that a mutation handler refuses an unmarked cross-origin-shaped request. +func doJSONNoCSRF(t *testing.T, srv *Server, method, path string, body any) *httptest.ResponseRecorder { + t.Helper() + return doJSONWithHeaders(t, srv, method, path, body, false) +} + +func doJSONWithHeaders(t *testing.T, srv *Server, method, path string, body any, csrf bool) *httptest.ResponseRecorder { t.Helper() var r *http.Request if body != nil { @@ -44,6 +61,10 @@ func doJSON(t *testing.T, srv *Server, method, path string, body any) *httptest. } else { r = httptest.NewRequestWithContext(t.Context(), method, path, nil) } + r.Header.Set("Content-Type", "application/json") + if csrf { + r.Header.Set("X-Archie-CSRF", "1") + } w := httptest.NewRecorder() srv.Handler().ServeHTTP(w, r) return w @@ -244,3 +265,41 @@ func TestHandleMappingsUnavailableWhenNotConfigured(t *testing.T) { t.Fatalf("status = %d, want %d", w.Code, http.StatusServiceUnavailable) } } + +// TestHandleMappingMutationsRequireCSRFHeader mirrors +// TestHandleBindingMutationsRequireCSRFHeader: every mapping write handler +// must enforce the same browser mutation contract as handleTaskAction +// (docs/prds/dynamic-curators.md), not just bindings. +func TestHandleMappingMutationsRequireCSRFHeader(t *testing.T) { + srv := mappingTestServer(t) + newMapping := map[string]any{ + "name": "sentry issue opened", + "source_hint": "sentry", + "fields": []mapping.Field{ + {Name: "title", Path: "issue.title", Type: mapping.TypeString, Required: true}, + }, + } + w := doJSON(t, srv, http.MethodPost, "/api/mappings", newMapping) + var created mapping.Mapping + if err := json.Unmarshal(w.Body.Bytes(), &created); err != nil { + t.Fatalf("unmarshal created mapping: %v; body = %s", err, w.Body.String()) + } + id := strconv.FormatInt(created.ID, 10) + + tests := []struct { + name, method, path string + body any + }{ + {"create", http.MethodPost, "/api/mappings", newMapping}, + {"update", http.MethodPatch, "/api/mappings/" + id, newMapping}, + {"delete", http.MethodDelete, "/api/mappings/" + id, nil}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + w := doJSONNoCSRF(t, srv, tc.method, tc.path, tc.body) + if w.Code != http.StatusForbidden { + t.Fatalf("status = %d, want %d without a CSRF header; body = %s", w.Code, http.StatusForbidden, w.Body.String()) + } + }) + } +}