fix(cabana): allow in-process backend dispatch through CSRF guard

This commit is contained in:
Jakub Zych
2026-11-27 14:36:34 +01:00
parent 9dffab9af7
commit e26aeceb2b
2 changed files with 30 additions and 0 deletions

View File

@@ -3,6 +3,8 @@ package cabana
import (
"net/http"
"strings"
"git.golem15.com/golem15/summercms/modules/bouncer"
)
const (
@@ -34,6 +36,9 @@ func csrfSafe(r *http.Request) bool {
case http.MethodGet, http.MethodHead, http.MethodOptions:
return true
}
if _, ok := bouncer.Dispatched(r.Context(), "backend"); ok {
return true
}
if bearerToken(r) != "" {
return true
}

View File

@@ -64,6 +64,31 @@ func TestPhase10CSRF(t *testing.T) {
if rec.Code == http.StatusForbidden {
t.Fatalf("Bearer request was refused: %s", rec.Body.String())
}
// In-process backend dispatch (e.g. pier MCP tool calls) has no
// Authorization or X-Requested-With header and must pass CSRF.
withDispatch, _ := csrfRequest(method, path, super)
withDispatch = withDispatch.WithContext(bouncer.WithDispatch(withDispatch.Context(), &bouncer.Dispatch{
Guards: []string{"backend"},
Principal: super,
}))
rec = httptest.NewRecorder()
handler(rec, withDispatch)
if rec.Code == http.StatusForbidden {
t.Fatalf("in-process backend dispatch was refused: %s", rec.Body.String())
}
// Non-backend dispatch marker must NOT bypass admin CSRF.
wrongDispatch, _ := csrfRequest(method, path, super)
wrongDispatch = wrongDispatch.WithContext(bouncer.WithDispatch(wrongDispatch.Context(), &bouncer.Dispatch{
Guards: []string{"api"},
Principal: super,
}))
rec = httptest.NewRecorder()
handler(rec, wrongDispatch)
if rec.Code != http.StatusForbidden {
t.Fatalf("non-backend dispatch status=%d, want 403", rec.Code)
}
})
}
// refresh, logout, account update, preferences update, avatar upload and remove, account