fix(cabana): allow in-process backend dispatch through CSRF guard
This commit is contained in:
@@ -3,6 +3,8 @@ package cabana
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.golem15.com/golem15/summercms/modules/bouncer"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -34,6 +36,9 @@ func csrfSafe(r *http.Request) bool {
|
|||||||
case http.MethodGet, http.MethodHead, http.MethodOptions:
|
case http.MethodGet, http.MethodHead, http.MethodOptions:
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
if _, ok := bouncer.Dispatched(r.Context(), "backend"); ok {
|
||||||
|
return true
|
||||||
|
}
|
||||||
if bearerToken(r) != "" {
|
if bearerToken(r) != "" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,6 +64,31 @@ func TestPhase10CSRF(t *testing.T) {
|
|||||||
if rec.Code == http.StatusForbidden {
|
if rec.Code == http.StatusForbidden {
|
||||||
t.Fatalf("Bearer request was refused: %s", rec.Body.String())
|
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
|
// refresh, logout, account update, preferences update, avatar upload and remove, account
|
||||||
|
|||||||
Reference in New Issue
Block a user