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.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
Read RBAC identity from request context, not response header
rcourtman77a6a8c695f8
diff --git a/internal/api/auth.go b/internal/api/auth.goindex 328fc0620a..6750c2a877 100644--- a/internal/api/auth.go+++ b/internal/api/auth.go@@ -1100,11 +1100,30 @@ func RequirePermission(cfg *config.Config, authorizer auth.Authorizer, action, r}}- // Extract user from header (set by CheckAuth) and inject into context- username := w.Header().Get("X-Authenticated-User")+ // Take the authenticated identity from the REQUEST context, not the+ // response header. checkAuth above (and every other auth path+ // upstream) attaches the username via attachUserContext, which+ // stores it on the request context. Reading from w.Header() — a+ // mutable response header that any handler between checkAuth and+ // RequirePermission can overwrite — would let a downstream+ // middleware substitute an arbitrary username and have the RBAC+ // authorizer make its decision against that value. Response+ // headers are not an identity channel.ctx := r.Context()- if username != "" {- ctx = internalauth.WithUser(ctx, username)+ username := internalauth.GetUser(ctx)+ if username == "" {+ // Defensive fallback: if some upstream auth path set the+ // header but skipped attachUserContext, surface that here so+ // the authorizer still gets the right identity, but log it so+ // the gap can be plugged at the source.+ if headerUser := w.Header().Get("X-Authenticated-User"); headerUser != "" {+ log.Warn().+ Str("user", headerUser).+ Str("path", r.URL.Path).+ Msg("Authenticated user found only in response header; upstream auth path should attachUserContext")+ username = headerUser+ ctx = internalauth.WithUser(ctx, username)+ }}// Check permission via authorizerdiff --git a/internal/api/security_regression_test.go b/internal/api/security_regression_test.goindex 7bf9f88bfd..04304b14d5 100644--- a/internal/api/security_regression_test.go+++ b/internal/api/security_regression_test.go@@ -4668,3 +4668,46 @@ func TestCheckCSRF_HeaderDoesNotBypassWhenSessionCookiePresent(t *testing.T) {})}}++// TestRequirePermissionUsesContextUsername regresses the X-Authenticated-User+// response-header-as-identity bug. RequirePermission previously read the+// authenticated username from w.Header().Get("X-Authenticated-User"). That+// header is mutable across the handler chain; any middleware sitting between+// checkAuth and RequirePermission that wrote to the response header could+// substitute an arbitrary identity, and the RBAC authorizer would make its+// decision against the substituted value. The contract is now: the identity+// comes from the REQUEST context (set by attachUserContext during auth).+// When the response header and the context disagree, the context wins.+func TestRequirePermissionUsesContextUsername(t *testing.T) {+ cfg := &config.Config{}++ var observedSubject string+ authorizer := &mockAuthorizerFn{fn: func(ctx context.Context, action string, resource string) (bool, error) {+ observedSubject = auth.GetUser(ctx)+ return true, nil+ }}++ handler := RequirePermission(cfg, authorizer, "read", "logs", func(w http.ResponseWriter, r *http.Request) {+ w.WriteHeader(http.StatusOK)+ })++ req := newLoopbackRequest("GET", "/api/test", nil)++ // Simulate a downstream middleware that has written a different value+ // to the response header AFTER checkAuth ran. The header below is the+ // would-be-attacker-supplied value; the context (set by attachUserContext)+ // holds the real authenticated user.+ rr := httptest.NewRecorder()+ rr.Header().Set("X-Authenticated-User", "evil-spoofed-user")++ ctxReq := req.WithContext(auth.WithUser(req.Context(), "real-authenticated-user"))++ handler.ServeHTTP(rr, ctxReq)++ if rr.Code != http.StatusOK {+ t.Fatalf("expected 200, got %d", rr.Code)+ }+ if observedSubject != "real-authenticated-user" {+ t.Fatalf("RBAC subject must come from context, got %q (header was %q)", observedSubject, "evil-spoofed-user")+ }+}