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.
This commit is contained in:
sam committed 2026-09-15 04:35:04 +10:00
1 parent 13ef3bd8ad
commit e3dddffd3e
4 files changed
+114

No files matched your search

+12
View File
@@ -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
+34
View File
@@ -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())
}
})
}
}
+9
View File
@@ -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
+59
View File
@@ -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())
}
})
}
}