fix(19.3-06): warn at boot when turnstile routes have no secret (WR-08)

- BuildRouter logs one warning with the route count and the production
  flag when routes name turnstile:open or turnstile:closed and the
  verifier has no secret; the secret is never logged
This commit is contained in:
Jakub Zych
2026-10-15 22:30:57 +02:00
parent caa5b5d8ff
commit d0a580c60c
4 changed files with 88 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ package surf
import (
"bytes"
"fmt"
"log/slog"
"math"
"net/http"
"strings"
@@ -651,6 +652,7 @@ func BuildRouter(app *backpack.App, plugins []party.Plugin) (*Router, error) {
return nil, err
}
}
warnTurnstileWithoutSecret(app, turnstile, r.routes)
if err := r.addCORSPreflights(); err != nil {
return nil, err
}
@@ -676,6 +678,31 @@ func buildTurnstile(app *backpack.App) (*bouncer.Turnstile, string) {
return bouncer.NewTurnstile(strings.TrimSpace(app.Config.String("turnstile.secret")), production), field
}
// warnTurnstileWithoutSecret logs one warning at boot when routes name
// turnstile:open or turnstile:closed and the verifier has no secret: such
// routes are not protected. The secret itself is never logged.
func warnTurnstileWithoutSecret(app *backpack.App, verifier *bouncer.Turnstile, routes []route) {
if verifier != nil && strings.TrimSpace(verifier.Secret) != "" {
return
}
n := 0
for _, rt := range routes {
for _, name := range rt.middleware {
if name == "turnstile:open" || name == "turnstile:closed" {
n++
break
}
}
}
if n == 0 {
return
}
production := verifier != nil && verifier.Production
accessLogger(app).Warn("surf: turnstile.secret is empty; turnstile:open passes every request, "+
"and turnstile:closed passes outside production and answers 503 in production",
slog.Int("routes", n), slog.Bool("production", production))
}
// checkAdminPrefix fails boot when a plugin other than cabana owns a route at
// or under the admin prefix: the admin SPA and API own that whole subtree.
func (r *Router) checkAdminPrefix(prefix string) error {