Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Keep control-plane account responses out of HTTP caches

pulse-triage[bot]84352c007182
diff --git a/docs/release-control/v6/internal/subsystems/cloud-paid.md b/docs/release-control/v6/internal/subsystems/cloud-paid.md
index 3864226242..2a4cd84e6f 100644
--- a/docs/release-control/v6/internal/subsystems/cloud-paid.md
+++ b/docs/release-control/v6/internal/subsystems/cloud-paid.md
@@ -15,6 +15,23 @@
## Purpose
+### Control-plane account responses are not HTTP-cacheable by default
+
+`CPSecurityHeaders`, which wraps the registered control-plane routes in the
+server, sets `Cache-Control: no-store` before a downstream handler writes.
+This covers identity/account bootstrap HTML and JSON, redirects, and refusal
+or error responses. Public static handlers can retain an explicit cache
+policy; the favicon still uses `public, max-age=86400` for GET and HEAD.
+Response bodies, status codes, cookies, CSP, account membership and session
+revocation are unchanged. This does not purge previously stored responses,
+erase client memory, or guarantee browser history/back-forward-cache behaviour.
+
+Normal-suite `security_cache_test.go` checks the committed response headers,
+preserved bodies/cookies/statuses, real registered portal/bootstrap/dashboard
+and membership routes with synthetic sessions, unauthenticated and revoked
+readers, and the unchanged public favicon cache. This is HTTP metadata
+hardening, not a rendered-page change or installed account-recovery proof.
+
### Billing Admin reads and changes use an accepted global snapshot
Hosted Billing Admin remains a platform-administration surface, not an
diff --git a/internal/cloudcp/security.go b/internal/cloudcp/security.go
index 63ee327849..9e964269bf 100644
--- a/internal/cloudcp/security.go
+++ b/internal/cloudcp/security.go
@@ -25,6 +25,11 @@ func generateCPCSPNonce() string {
// cpsec.NonceFromContext.
func CPSecurityHeaders(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ // Portal HTML and API responses carry account/session data. Do not let
+ // HTTP caches retain them, including redirects and authentication errors.
+ // Public static handlers can still set their explicit cache policy.
+ w.Header().Set("Cache-Control", "no-store")
+
nonce := generateCPCSPNonce()
if nonce != "" {
r = r.WithContext(cpsec.WithNonce(r.Context(), nonce))
diff --git a/internal/cloudcp/security_cache_test.go b/internal/cloudcp/security_cache_test.go
new file mode 100644
index 0000000000..0419dbb88a
--- /dev/null
+++ b/internal/cloudcp/security_cache_test.go
@@ -0,0 +1,162 @@
+package cloudcp
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strconv"
+ "strings"
+ "testing"
+
+ cpauth "github.com/rcourtman/pulse-go-rewrite/internal/cloudcp/auth"
+ "github.com/rcourtman/pulse-go-rewrite/internal/cloudcp/portal"
+ "github.com/rcourtman/pulse-go-rewrite/internal/cloudcp/registry"
+)
+
+func TestCPSecurityHeaders_NoStoreBeforeResponse(t *testing.T) {
+ for _, status := range []int{http.StatusOK, http.StatusFound, http.StatusUnauthorized, http.StatusForbidden, http.StatusNotFound, http.StatusInternalServerError} {
+ t.Run(strconv.Itoa(status), func(t *testing.T) {
+ inner := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/plain")
+ w.Header().Set("Location", "/portal")
+ http.SetCookie(w, &http.Cookie{Name: "fixture", Value: "synthetic", HttpOnly: true, Secure: true})
+ w.WriteHeader(status)
+ _, _ = w.Write([]byte("synthetic response"))
+ })
+ req := httptest.NewRequest(http.MethodGet, "/portal", nil)
+ rec := httptest.NewRecorder()
+ CPSecurityHeaders(inner).ServeHTTP(rec, req)
+ resp := rec.Result()
+ defer resp.Body.Close()
+ // Result snapshots headers at the first write, not after the body.
+ if got := resp.Header.Get("Cache-Control"); got != "no-store" {
+ t.Errorf("committed Cache-Control = %q, want no-store", got)
+ }
+ if resp.StatusCode != status || rec.Body.String() != "synthetic response" || resp.Header.Get("Location") != "/portal" || resp.Header.Get("Content-Type") != "text/plain" {
+ t.Fatal("cache policy changed downstream response")
+ }
+ cookies := resp.Cookies()
+ if len(cookies) != 1 || cookies[0].Value != "synthetic" || !cookies[0].HttpOnly || !cookies[0].Secure {
+ t.Fatal("cache policy changed downstream cookie")
+ }
+ })
+ }
+}
+
+func TestCPSecurityHeaders_NoStorePortalRoutes(t *testing.T) {
+ dir := t.TempDir()
+ reg, err := registry.NewTenantRegistry(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { _ = reg.Close() })
+ accountID, err := registry.GenerateAccountID()
+ if err != nil {
+ t.Fatal(err)
+ }
+ userID, err := registry.GenerateUserID()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := reg.CreateAccount(&registry.Account{ID: accountID, Kind: registry.AccountKindMSP, DisplayName: "Cache policy fixture"}); err != nil {
+ t.Fatal(err)
+ }
+ if err := reg.CreateUser(&registry.User{ID: userID, Email: "[email protected]"}); err != nil {
+ t.Fatal(err)
+ }
+ if err := reg.CreateMembership(&registry.AccountMembership{AccountID: accountID, UserID: userID, Role: registry.MemberRoleOwner}); err != nil {
+ t.Fatal(err)
+ }
+ svc, err := cpauth.NewService(dir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(svc.Close)
+ token, err := svc.GenerateSessionToken(userID, "[email protected]", cpauth.SessionTTL)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ mux := http.NewServeMux()
+ RegisterRoutes(mux, &Deps{
+ Config: &CPConfig{DataDir: dir, AdminKey: "synthetic-admin-key", BaseURL: "https://cloud.example.invalid", ControlPlaneMode: ControlPlaneModeProviderHostedMSP},
+ Registry: reg, MagicLinks: svc, Version: "test",
+ })
+ handler := CPSecurityHeaders(mux)
+ read := func(t *testing.T, path, credential string) *httptest.ResponseRecorder {
+ t.Helper()
+ req := httptest.NewRequest(http.MethodGet, path, nil)
+ if credential != "" {
+ req.AddCookie(&http.Cookie{Name: cpauth.SessionCookieName, Value: credential})
+ }
+ rec := httptest.NewRecorder()
+ handler.ServeHTTP(rec, req)
+ resp := rec.Result()
+ defer resp.Body.Close()
+ if got := resp.Header.Get("Cache-Control"); got != "no-store" {
+ t.Errorf("GET %s committed Cache-Control = %q, want no-store", path, got)
+ }
+ return rec
+ }
+ paths := []string{portal.PortalPagePath, portal.PortalBootstrapPath, portal.PortalDashboardPath + "?account_id=" + accountID, "/api/accounts/" + accountID + "/members"}
+ for _, path := range paths {
+ t.Run(path, func(t *testing.T) {
+ rec := read(t, path, token)
+ if rec.Code != http.StatusOK {
+ t.Fatalf("status = %d, want 200", rec.Code)
+ }
+ if !strings.Contains(rec.Body.String(), "[email protected]") && !strings.Contains(rec.Body.String(), "Cache policy fixture") {
+ t.Fatal("fixture did not exercise a personal account response")
+ }
+ })
+ }
+ for _, path := range paths[1:] {
+ t.Run("unauthenticated"+path, func(t *testing.T) {
+ rec := read(t, path, "")
+ if rec.Code != http.StatusUnauthorized {
+ t.Fatalf("status = %d, want 401", rec.Code)
+ }
+ if strings.Contains(rec.Body.String(), "[email protected]") {
+ t.Fatal("unauthenticated response contains account data")
+ }
+ })
+ }
+ if _, err := reg.RevokeUserSessions(userID); err != nil {
+ t.Fatal(err)
+ }
+ for _, path := range paths {
+ t.Run("revoked"+path, func(t *testing.T) {
+ rec := read(t, path, token)
+ want := http.StatusUnauthorized
+ if path == portal.PortalPagePath {
+ want = http.StatusOK
+ }
+ if rec.Code != want {
+ t.Fatalf("status = %d, want %d", rec.Code, want)
+ }
+ if strings.Contains(rec.Body.String(), "[email protected]") || strings.Contains(rec.Body.String(), "Cache policy fixture") {
+ t.Fatal("revoked response contains account data")
+ }
+ })
+ }
+}
+
+func TestCPSecurityHeaders_PreservesPublicAssetCache(t *testing.T) {
+ for _, method := range []string{http.MethodGet, http.MethodHead} {
+ t.Run(method, func(t *testing.T) {
+ req := httptest.NewRequest(method, "/favicon.svg", nil)
+ rec := httptest.NewRecorder()
+ CPSecurityHeaders(http.HandlerFunc(handleControlPlaneFaviconSVG)).ServeHTTP(rec, req)
+ resp := rec.Result()
+ defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK || resp.Header.Get("Cache-Control") != "public, max-age=86400" || resp.Header.Get("Content-Type") != "image/svg+xml" {
+ t.Fatal("public favicon cache contract changed")
+ }
+ if method == http.MethodGet && rec.Body.String() != string(controlPlaneFaviconSVG) {
+ t.Fatal("favicon bytes changed")
+ }
+ if method == http.MethodHead && rec.Body.Len() != 0 {
+ t.Fatal("HEAD wrote favicon bytes")
+ }
+ })
+ }
+}