feat: add automatic CSRF protection for action calls

Generate a per-context CSRF token (128-bit, crypto/rand) and inject it
as a Datastar signal (via-csrf) alongside via-ctx. Validate with
constant-time comparison on /_action/{id} before executing, returning
403 on mismatch. Transparent to users since Datastar sends all signals
automatically.

Closes #9
This commit is contained in:
Ryan Hamamura
2026-02-06 11:17:41 -10:00
parent 8aa91c577c
commit b7acfa6302
2 changed files with 16 additions and 1 deletions

View File

@@ -20,6 +20,7 @@ import (
type Context struct {
id string
route string
csrfToken string
app *V
view func() h.H
routeParams map[string]string
@@ -477,6 +478,7 @@ func newContext(id string, route string, v *V) *Context {
return &Context{
id: id,
route: route,
csrfToken: genCSRFToken(),
routeParams: make(map[string]string),
app: v,
componentRegistry: make(map[string]*Context),