mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-06 18:39:42 +00:00
81df19aade
## Summary Fixes `CSRF token validation failed` errors on non-replayed (`retry: false`) mutations (such as clicking **Generate command** in the remote commands dialog) after a browser session has been open for more than 4 hours. ### Root Cause - `CSRFTokenStore` expires server-side tokens after 4 hours, while `pulse_csrf` cookies were issued with a 24-hour (or 30-day Remember Me) `MaxAge`. - `GET /api/*` middleware only issued a `pulse_csrf` cookie when the cookie was missing, never when an existing cookie was expired or invalid. - Because the stale cookie remained in `document.cookie`, `apiClient` never called `ensureCSRFToken()`, and `retry: false` mutations surfaced the 403 directly on the first attempt. ### Changes - Added `csrfTokenTTL` (4h), `csrfCookieMaxAgeSeconds` (3h55m), and `csrfCookieMaxAge(sessionDuration)` so every `pulse_csrf` cookie expires slightly before the server-side token (or matches shorter session durations). - Updated `GET /api/*` middleware in `internal/api/router.go` to refresh missing, empty, invalid, or expired `pulse_csrf` cookies for live sessions, and to proactively rotate valid cookies with less than 30 minutes remaining (coalesced across a 30-second window via `HasRecentToken` so concurrent page-load GETs mint at most one replacement token). - Added unit and middleware regression tests in `csrf_store_test.go`, `security_test.go`, and `router_csrf_middleware_test.go`. ### Verification - `go test -v -count=1 ./internal/api -run 'TestCSRF|TestCSRFCookie|TestIssueNewCSRFCookie|TestClearCSRFCookie|TestRouter.*CSRF|TestCheckCSRF'` - `go test -race -count=1 ./internal/api -run 'TestCSRFTokenStore|TestRouter.*CSRF|TestIssueNewCSRFCookie|TestCSRFCookieMaxAge'` - `go test -count=1 ./internal/api -run 'Test.*Login|Test.*SAML|Test.*MagicLink|Test.*CloudHandoff|Test.*BasicAuth|Test.*SecuritySetup'` - `go vet ./internal/api/...`