fix: exempt the runner bearer API from cookie CSRF
4 files changed,
+133
−3
docs/superpowers/plans/CONTROLLER-REPORT-plan-2b.md+37 −0internal/equipo/app.go+4 −2internal/equipo/runner_api.go+15 −1internal/equipotest/runner_csrf_test.go+77 −0
CI failed — run details
diff --git a/docs/superpowers/plans/CONTROLLER-REPORT-plan-2b.md b/docs/superpowers/plans/CONTROLLER-REPORT-plan-2b.md| index 7f243371..216e2cff 100644 |
| --- a/docs/superpowers/plans/CONTROLLER-REPORT-plan-2b.md |
| +++ b/docs/superpowers/plans/CONTROLLER-REPORT-plan-2b.md |
| @@ -24582,3 +24582,40 @@ CGO_ENABLED=0 TMPDIR=/tmp/e2b-ci.jpfjpa3w go test ./internal/runner -run '^TestD |
| ``` |
| Full cause analysis, exact `/tmp` wrapper, mutation edits/output, copy inventory and cleanup: `.superpowers/sdd/2026-11-03-equipo-plan-2b-direct/ci-fix2-report.md`. No live gate or push. |
| + |
| +## CI fix 2 C (2026-11-03) |
| + |
| +Derived exact CSRF exemptions from the registered runner router. Real `AppWithExecution` coverage checks every runner POST, a valid originless heartbeat, runner cookie refusal, ordinary form CSRF and existing enrolment exemptions. Audited non-browser POST clients: only runner HTTP and enrolment target this app; no cookie route was exempted. |
| + |
| +RED commands and results: |
| + |
| +```sh |
| +CGO_ENABLED=0 go test ./internal/equipotest -run '^TestRunnerAppCSRF$' -count=1 |
| +# Exit 1: all runner POSTs refused by cookie CSRF (403). |
| +``` |
| + |
| +Final GREEN commands and results (all exit 0): |
| + |
| +```sh |
| +CGO_ENABLED=0 TMPDIR=/home/paulca/t/e2b go test ./internal/equipo ./internal/equipotest -run 'TestRunnerAppCSRF|TestRunner|TestReleaseStampContract|TestDirectReleaseRequiresCertification|TestBugPreparationHostTmp' -count=1 |
| +# ok amadan.net/oficina/equipo/internal/equipo 0.053s; ok amadan.net/oficina/equipo/internal/equipotest 53.678s |
| +CGO_ENABLED=0 TMPDIR=/home/paulca/t/e2b go vet ./... |
| +# no output |
| +CGO_ENABLED=0 TMPDIR=/home/paulca/t/e2b git diff --check |
| +# no output |
| +``` |
| + |
| +Mutations, each compiled, failed the named test, then restored: |
| + |
| +```sh |
| +# M10-runner-exemption: internal/equipo/runner_api.go; --- FAIL: TestRunnerAppCSRF (0.42s) |
| +CGO_ENABLED=0 TMPDIR=/home/paulca/t/e2b go test ./internal/equipotest -run '^TestRunnerAppCSRF$' -count=1 |
| +# M11-form-csrf: internal/equipo/app.go; --- FAIL: TestRunnerAppCSRF (0.40s) |
| +CGO_ENABLED=0 TMPDIR=/home/paulca/t/e2b go test ./internal/equipotest -run '^TestRunnerAppCSRF$' -count=1 |
| +# M12-runner-cookie-auth: internal/equipo/runner_api.go; --- FAIL: TestRunnerAppCSRF (0.58s) |
| +CGO_ENABLED=0 TMPDIR=/home/paulca/t/e2b go test ./internal/equipotest -run '^TestRunnerAppCSRF$' -count=1 |
| +``` |
| + |
| +The first form-CSRF mutation survived the original status-only check; requiring the middleware response made it fail. |
| + |
| +Full cause analysis, exact `/tmp` wrapper, mutation edits/output, copy inventory and cleanup: `.superpowers/sdd/2026-11-03-equipo-plan-2b-direct/ci-fix2-report.md`. No live gate or push. |
diff --git a/internal/equipo/app.go b/internal/equipo/app.go| index 172f25f5..f782cc99 100644 |
| --- a/internal/equipo/app.go |
| +++ b/internal/equipo/app.go |
| @@ -143,10 +143,12 @@ func RouterWithStore(d *db.DB, cfg Config, mailer mail.Sender, logger *slog.Logg |
| } |
| } |
| a.Use(a.live.Middleware) |
| - a.Use(csrf.Protect(cfg.Origin, csrf.Exempt("/api/enrolments", "/api/enrolments/token"))) |
| + runner := a.store.runnerRouter() |
| + exempt := append([]string{"/api/enrolments", "/api/enrolments/token"}, runnerCSRFExemptPaths(runner)...) |
| + a.Use(csrf.Protect(cfg.Origin, csrf.Exempt(exempt...))) |
| a.Post("/api/enrolments", a.beginEnrolment) |
| a.Post("/api/enrolments/token", a.exchangeEnrolment) |
| - a.Mount("/api/runner/v2", a.store.RunnerHandler()) |
| + a.Mount("/api/runner/v2", runner) |
| a.Get("/signin", func(w http.ResponseWriter, r *http.Request) { renderSignin(a.auth, w, r) }) |
| a.Post("/auth/begin", a.auth.Begin) |
| a.Get("/auth/callback", a.auth.Callback) |
diff --git a/internal/equipo/runner_api.go b/internal/equipo/runner_api.go| index ef676b01..afd81233 100644 |
| --- a/internal/equipo/runner_api.go |
| +++ b/internal/equipo/runner_api.go |
| @@ -13,7 +13,9 @@ import ( |
| // RunnerHandler exposes only outbound runner protocol endpoints. Normal app |
| // construction leaves execution unattached and these endpoints fail closed. |
| -func (s *Store) RunnerHandler() http.Handler { |
| +func (s *Store) RunnerHandler() http.Handler { return s.runnerRouter() } |
| + |
| +func (s *Store) runnerRouter() chi.Router { |
| r := chi.NewRouter() |
| r.Post("/poll", s.runnerPoll) |
| r.Post("/blobs", s.runnerPutBlob) |
| @@ -28,6 +30,18 @@ func (s *Store) RunnerHandler() http.Handler { |
| r.Get("/artifacts/{assignment}/{artifact}", s.runnerArtifact) |
| return r |
| } |
| +func runnerCSRFExemptPaths(r chi.Routes) []string { |
| + var paths []string |
| + // Use the registered bearer routes so additions inherit the exemption. |
| + _ = chi.Walk(r, func(method, route string, _ http.Handler, _ ...func(http.Handler) http.Handler) error { |
| + if method != http.MethodGet && method != http.MethodHead && method != http.MethodOptions { |
| + paths = append(paths, "/api/runner/v2"+route) |
| + } |
| + return nil |
| + }) |
| + return paths |
| +} |
| + |
| func runnerBody(w http.ResponseWriter, r *http.Request, v any, limit int64) bool { |
| content, _, err := mime.ParseMediaType(r.Header.Get("Content-Type")) |
| if err != nil || content != "application/json" || runnerproto.DecodeLimit(r.Body, v, limit) != nil { |
diff --git a/internal/equipotest/runner_csrf_test.go b/internal/equipotest/runner_csrf_test.go| new file mode 100644 |
| index 00000000..756f4dc7 |
| --- /dev/null |
| +++ b/internal/equipotest/runner_csrf_test.go |
| @@ -0,0 +1,77 @@ |
| +package equipotest |
| + |
| +import ( |
| + "net/http" |
| + "net/http/httptest" |
| + "strings" |
| + "testing" |
| + |
| + "amadan.net/oficina/equipo/internal/equipo" |
| + "github.com/go-chi/chi/v5" |
| +) |
| + |
| +func TestRunnerAppCSRF(t *testing.T) { |
| + f := runnerFixtureNew(t, false) |
| + token := runnerBearerFor(t, f) |
| + cfg := testConfig(testOrigin) |
| + cfg.ExecutionEnabled = true |
| + app, err := equipo.AppWithExecution(f.d, cfg, &capturingMailer{}, nil, f.x) |
| + executionMust(t, err) |
| + request := func(path, body, bearer, cookie, origin string) *httptest.ResponseRecorder { |
| + t.Helper() |
| + req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body)) |
| + req.Header.Set("Content-Type", "application/json") |
| + if bearer != "" { |
| + req.Header.Set("Authorization", "Bearer "+bearer) |
| + } |
| + if cookie != "" { |
| + req.Header.Set("Cookie", cookie) |
| + } |
| + if origin != "" { |
| + req.Header.Set("Origin", origin) |
| + } |
| + response := httptest.NewRecorder() |
| + app.ServeHTTP(response, req) |
| + return response |
| + } |
| + executionMust(t, chi.Walk(f.s.RunnerHandler().(chi.Routes), func(method, route string, _ http.Handler, _ ...func(http.Handler) http.Handler) error { |
| + if method != http.MethodPost { |
| + return nil |
| + } |
| + t.Run(route, func(t *testing.T) { |
| + response := request("/api/runner/v2"+route, "{", token, "", "") |
| + if response.Code != http.StatusUnprocessableEntity || !strings.Contains(response.Body.String(), "form.invalid") { |
| + t.Fatalf("bearer request blocked before runner body validation: %d %s", response.Code, response.Body) |
| + } |
| + for _, bearer := range []string{"", token} { |
| + response = request("/api/runner/v2"+route, "{}", bearer, "rastrillo_session=browser", "") |
| + if response.Code != http.StatusForbidden || !strings.Contains(response.Body.String(), "form.forbidden") { |
| + t.Fatalf("cookie request bypassed runner authentication: %d %s", response.Code, response.Body) |
| + } |
| + } |
| + }) |
| + return nil |
| + })) |
| + t.Run("heartbeat", func(t *testing.T) { |
| + response := request("/api/runner/v2/heartbeat", `{"version":2,"request_id":"csrf-heartbeat"}`, token, "", "") |
| + if response.Code != http.StatusOK { |
| + t.Fatalf("heartbeat: %d %s", response.Code, response.Body) |
| + } |
| + }) |
| + for _, path := range []string{"/projects", "/auth/begin", "/runners/enrol", "/api/runner/v2/poll/extra"} { |
| + t.Run("browser"+path, func(t *testing.T) { |
| + response := request(path, "{}", "", "rastrillo_session=browser", "https://other.example") |
| + if response.Code != http.StatusForbidden || !strings.Contains(response.Body.String(), "cross-origin form submission refused") { |
| + t.Fatalf("cross-origin form accepted: %d %s", response.Code, response.Body) |
| + } |
| + }) |
| + } |
| + for _, path := range []string{"/api/enrolments", "/api/enrolments/token"} { |
| + t.Run(path, func(t *testing.T) { |
| + response := request(path, "{", "", "", "") |
| + if response.Code != http.StatusUnprocessableEntity { |
| + t.Fatalf("device client blocked: %d %s", response.Code, response.Body) |
| + } |
| + }) |
| + } |
| +} |