Files
Pulse/internal/api/cloud_handoff.go
rcourtman 1b405c35d6 Refresh expired CSRF cookies on GET and cap cookie MaxAge
Server-side CSRF tokens expire after 4 hours in CSRFTokenStore, while pulse_csrf browser cookies were issued with a 24-hour or 30-day MaxAge. On GET /api/*, the router only issued a cookie when pulse_csrf was missing and never checked whether an existing cookie had expired or become invalid.

Once a session passed the 4-hour mark, browsers kept sending the expired pulse_csrf cookie across page loads and GET requests without triggering apiClient's ensureCSRFToken fallback. Non-replayed mutations (retry: false), such as generating a host agent install command, then failed on the first click with 403 CSRF token validation failed.

Fix both halves of the lifecycle:
- Bound pulse_csrf cookie MaxAge across all session and CSRF issuance sites to 3h55m (5 minutes below the 4-hour server TTL, or the session duration when shorter) so idle tabs drop stale cookies before server expiry.
- Refresh missing, empty, or invalid/expired pulse_csrf cookies on GET /api/* for live sessions, and proactively rotate still-valid cookies within 30 minutes of expiry (coalesced over a 30-second window so parallel page-load GETs mint at most one replacement token).

Contract-Neutral: Bug fix aligning pulse_csrf cookie MaxAge with the 4-hour server CSRF token TTL and refreshing expired or near-expiry CSRF cookies on GET /api/* for live sessions, keeping all CSRF admission, 8-token bound, hash-only storage, and retry: false contracts unchanged
2026-11-28 14:12:31 +00:00

148 lines
4.7 KiB
Go

package api
import (
"crypto/sha256"
"encoding/hex"
"errors"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/rcourtman/pulse-go-rewrite/pkg/cloudauth"
"github.com/rs/zerolog/log"
)
// HandleCloudHandoff returns an HTTP handler that completes the control-plane → tenant
// auth handoff. It reads a per-tenant HMAC key, verifies the handoff token, creates a
// session, and redirects to the dashboard.
//
// Self-guards: returns 404 if the handoff key file does not exist in dataPath,
// meaning this is not a cloud-managed tenant.
func HandleCloudHandoff(dataPath string) http.HandlerFunc {
InitPersistentAuthStores(dataPath)
replay := &jtiReplayStore{configDir: dataPath}
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Self-guard: only respond if a handoff key exists.
keyPath := filepath.Join(dataPath, cloudauth.HandoffKeyFile)
handoffKey, err := os.ReadFile(keyPath)
if err != nil {
http.NotFound(w, r)
return
}
tokenStr := strings.TrimSpace(r.URL.Query().Get("token"))
if tokenStr == "" {
http.Redirect(w, r, "/login?error=handoff_invalid", http.StatusTemporaryRedirect)
return
}
claims, err := cloudauth.VerifyClaimsWithExpiry(handoffKey, tokenStr)
if err != nil {
log.Warn().Err(err).Msg("Cloud handoff token verification failed")
http.Redirect(w, r, "/login?error=handoff_invalid", http.StatusTemporaryRedirect)
return
}
email := normalizeHandoffEmail(claims.Email)
userID := strings.TrimSpace(claims.UserID)
tenantID := strings.TrimSpace(claims.TenantID)
if email == "" || userID == "" || isEmailShapedHandoffUserID(userID) || !isValidOrganizationID(tenantID) {
log.Warn().
Str("tenant_id", tenantID).
Bool("missing_user_id", userID == "").
Bool("email_shaped_user_id", isEmailShapedHandoffUserID(userID)).
Msg("Cloud handoff token rejected due to invalid identity claims")
http.Redirect(w, r, "/login?error=handoff_invalid", http.StatusTemporaryRedirect)
return
}
authz, err := authorizeHandoffOrganizationMembership(dataPath, tenantID, userID, email, claims.Role)
if err != nil {
if errors.Is(err, errHandoffAuthorizationDenied) {
log.Warn().
Err(err).
Str("tenant_id", tenantID).
Str("email", email).
Str("user_id", userID).
Msg("Cloud handoff authorization denied")
http.Redirect(w, r, "/login?error=handoff_invalid", http.StatusTemporaryRedirect)
return
}
log.Error().
Err(err).
Str("tenant_id", tenantID).
Str("email", email).
Str("user_id", userID).
Msg("Cloud handoff authorization lookup failed")
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
tokenHash := sha256.Sum256([]byte(tokenStr))
replayID := "handoff:" + hex.EncodeToString(tokenHash[:])
stored, storeErr := replay.checkAndStore(replayID, claims.ExpiresAt)
if storeErr != nil {
log.Error().Err(storeErr).Msg("Cloud handoff replay-store failure")
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
if !stored {
log.Warn().Str("replay_id_prefix", replayID[:24]).Msg("Cloud handoff token replay blocked")
http.Redirect(w, r, "/login?error=handoff_replayed", http.StatusTemporaryRedirect)
return
}
// Invalidate any pre-existing session to prevent session fixation attacks.
InvalidateOldSessionFromRequest(r)
// Create session using existing machinery (same pattern as HandlePublicMagicLinkVerify).
sessionToken := generateSessionToken()
if sessionToken == "" {
http.Error(w, "Failed to create session", http.StatusInternalServerError)
return
}
userAgent := r.Header.Get("User-Agent")
clientIP := GetClientIP(r)
sessionDuration := 24 * time.Hour
GetSessionStore().CreateSession(sessionToken, sessionDuration, userAgent, clientIP, authz.UserID)
TrackUserSession(authz.UserID, sessionToken)
csrfToken := generateCSRFToken(sessionToken)
cookiePolicy := getBrowserCookiePolicy(r)
cookieMaxAge := int(sessionDuration.Seconds())
cookiePolicy.setHTTPOnly(w, &http.Cookie{
Name: sessionCookieName(cookiePolicy.secure),
Value: sessionToken,
Path: "/",
HttpOnly: true,
MaxAge: cookieMaxAge,
})
cookiePolicy.setClientReadable(w, &http.Cookie{
Name: CookieNameCSRF,
Value: csrfToken,
Path: "/",
MaxAge: csrfCookieMaxAge(sessionDuration),
})
cookiePolicy.setClientReadable(w, &http.Cookie{
Name: CookieNameOrgID,
Value: tenantID,
Path: "/",
MaxAge: cookieMaxAge,
})
log.Info().
Str("email", email).
Str("user_id", authz.UserID).
Msg("Cloud handoff completed, session created")
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
}
}