Files
Pulse/internal/api/basic_auth_session.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

147 lines
4.7 KiB
Go

package api
import (
"net/http"
"strings"
"sync"
"time"
"github.com/rs/zerolog/log"
)
// requestIsPulseWebClient reports whether Pulse's own web client sent this
// request: frontend-modern/src/utils/apiClient.ts marks every API call it makes.
// Scripts and dashboards do not send the header, and a page on another origin
// cannot without Pulse's CORS policy allowing it, which
// TestPulseCORSNeverAllowsTheWebClientMarker holds. Issuing a session on it is a
// hint, not a boundary: the caller has already proved the credentials, and a
// session grants nothing those credentials do not. isCrossSiteBrowserRequest does
// rely on it, where the browser sent no Sec-Fetch-Site.
func requestIsPulseWebClient(r *http.Request) bool {
return strings.EqualFold(strings.TrimSpace(r.Header.Get("X-Requested-With")), "XMLHttpRequest")
}
// issueBasicBrowserSession gives Pulse's web client a browser session for
// verified Basic credentials. A browser behind a proxy that forwards Basic
// credentials, such as an Authentik proxy provider, signs in this way. API clients
// never reach it: each of their calls would take one of the user's
// maxSessionsPerUser sessions and eventually sign the user's browser out.
func issueBasicBrowserSession(w http.ResponseWriter, r *http.Request, username string) bool {
token := generateSessionToken()
if token == "" {
return false
}
// Store session persistently (including username for restart survival)
userAgent := r.Header.Get("User-Agent")
clientIP := GetClientIP(r)
GetSessionStore().CreateSession(token, 24*time.Hour, userAgent, clientIP, username)
// Track session for user (in-memory for fast lookups)
TrackUserSession(username, token)
// Generate CSRF token
csrfToken := generateCSRFToken(token)
// Get appropriate cookie settings based on proxy detection
cookiePolicy := getBrowserCookiePolicy(r)
// Debug logging for Cloudflare tunnel issues
sameSiteName := "Default"
switch cookiePolicy.sameSite {
case http.SameSiteNoneMode:
sameSiteName = "None"
case http.SameSiteLaxMode:
sameSiteName = "Lax"
case http.SameSiteStrictMode:
sameSiteName = "Strict"
}
log.Debug().
Bool("secure", cookiePolicy.secure).
Str("same_site", sameSiteName).
Str("token", safePrefixForLog(token, 8)+"...").
Str("remote_addr", r.RemoteAddr).
Msg("Setting session cookie after successful login")
// Set session cookie
cookiePolicy.setHTTPOnly(w, &http.Cookie{
Name: sessionCookieName(cookiePolicy.secure),
Value: token,
Path: "/",
HttpOnly: true,
MaxAge: 86400, // 24 hours
})
// Set CSRF cookie (not HttpOnly so JS can read it)
cookiePolicy.setClientReadable(w, &http.Cookie{
Name: CookieNameCSRF,
Value: csrfToken,
Path: "/",
MaxAge: csrfCookieMaxAge(24 * time.Hour),
})
// Audit log successful login
LogAuditEventForTenant(GetOrgID(r.Context()), "login", username, clientIP, r.URL.Path, true, "Basic auth login")
return true
}
// basicAuthAuditInterval re-records continued Basic access from one address at
// most this often, so the audit trail still shows it.
const basicAuthAuditInterval = 24 * time.Hour
// maxBasicAuthAudits bounds remembered addresses. Only a caller holding the
// password adds one, and at capacity an unremembered address is audited on every
// call until a slot expires: repeating a record beats losing one.
const maxBasicAuthAudits = 1024
// basicAuthAuditTracker decides which successful API-client Basic calls are audit
// events. A password used only over Basic must leave a trace, but recording every
// poll buries the logins and failures the log exists to show. Failed attempts are
// always audited and never pass through here.
type basicAuthAuditTracker struct {
mu sync.Mutex
last map[basicAuthAuditKey]time.Time
}
type basicAuthAuditKey struct {
orgID string
username string
clientIP string
}
func newBasicAuthAuditTracker() *basicAuthAuditTracker {
return &basicAuthAuditTracker{last: make(map[basicAuthAuditKey]time.Time)}
}
var basicAuthAudits = newBasicAuthAuditTracker()
// due reports whether this call is new audit information: the first from this
// organization, user and address since startup, or the first in
// basicAuthAuditInterval.
func (t *basicAuthAuditTracker) due(orgID, username, clientIP string, now time.Time) bool {
key := basicAuthAuditKey{orgID: orgID, username: username, clientIP: clientIP}
t.mu.Lock()
defer t.mu.Unlock()
if last, seen := t.last[key]; seen {
if now.Sub(last) < basicAuthAuditInterval {
return false
}
t.last[key] = now
return true
}
if len(t.last) >= maxBasicAuthAudits {
for candidate, last := range t.last {
if now.Sub(last) >= basicAuthAuditInterval {
delete(t.last, candidate)
}
}
if len(t.last) >= maxBasicAuthAudits {
return true
}
}
t.last[key] = now
return true
}