mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-30 01:32:00 +01:00
Every request authenticated with Basic credentials created a session, persisted it and wrote a login audit event. The auth check runs twice on a routed request, so one call took two of the user's ten session slots. Anything polling with Basic credentials, such as a script or a dashboard widget, kept signing its owner's browser out. An API client's Basic credentials now authenticate one request and issue nothing, as an API token does. Only Pulse's own web client, which marks its API calls, still gets a session: a browser behind a proxy that forwards Basic credentials relies on it, and without it pays for a password check per call and loses its Settings pages. A cookie reaching the Basic branch was rejected, and handlers read the username from the cookie, so it is still retired. A password used only over Basic must leave a trace, so the first call per organization, user and address is audited, then once a day. Failures stay audited.
145 lines
4.5 KiB
Go
145 lines
4.5 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. It is a hint, not a boundary:
|
|
// the caller has already proved the credentials, and a session grants nothing
|
|
// those credentials do not.
|
|
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: 86400, // 24 hours
|
|
})
|
|
|
|
// 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
|
|
}
|