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.

Require an admin for non-token agent uninstall

rcourtman111a62cfe88b
diff --git a/internal/api/agent_uninstall_scope_test.go b/internal/api/agent_uninstall_scope_test.go
new file mode 100644
index 0000000000..70e3a47f9e
--- /dev/null
+++ b/internal/api/agent_uninstall_scope_test.go
@@ -0,0 +1,44 @@
+package api
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+)
+
+// A collector uninstalls itself with its own agent:report token. Callers that
+// are not tokens (browser sessions, proxy users) reach the administrative
+// removal path in the handler, so they must be administrators, while the
+// collector's token path keeps working.
+func TestAgentUninstallKeepsTokenSelfRemovalReachable(t *testing.T) {
+ const reportToken = "agent-uninstall-report.12345678"
+ const unrelatedToken = "agent-uninstall-unrelated.12345678"
+ cfg := newTestConfigWithTokens(t,
+ newTokenRecord(t, reportToken, []string{config.ScopeAgentReport}, nil),
+ newTokenRecord(t, unrelatedToken, []string{config.ScopeMonitoringRead}, nil),
+ )
+ router := NewRouter(cfg, nil, nil, nil, nil, "1.0.0")
+
+ for _, path := range []string{"/api/agents/agent/uninstall", "/api/agents/host/uninstall"} {
+ req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(`{}`))
+ req.Header.Set("X-API-Token", unrelatedToken)
+ rec := httptest.NewRecorder()
+ router.Handler().ServeHTTP(rec, req)
+ if rec.Code != http.StatusForbidden {
+ t.Errorf("token without agent:report POST %s = %d, want 403", path, rec.Code)
+ }
+
+ // An empty agentId is rejected by the handler itself, which proves the
+ // request got past authentication and the scope gate.
+ req = httptest.NewRequest(http.MethodPost, path, strings.NewReader(`{}`))
+ req.Header.Set("X-API-Token", reportToken)
+ rec = httptest.NewRecorder()
+ router.Handler().ServeHTTP(rec, req)
+ if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "missing_agent_id") {
+ t.Errorf("agent:report token POST %s = %d %q, want 400 missing_agent_id from the handler", path, rec.Code, rec.Body.String())
+ }
+ }
+}
diff --git a/internal/api/proxy_admin_gating_test.go b/internal/api/proxy_admin_gating_test.go
index 54590ca940..9e3ab10d34 100644
--- a/internal/api/proxy_admin_gating_test.go
+++ b/internal/api/proxy_admin_gating_test.go
@@ -38,6 +38,8 @@ func TestProxyAuthAdminGatesAdminEndpoints(t *testing.T) {
{method: http.MethodPost, path: "/api/agents/agent/unlink", body: `{}`},
{method: http.MethodPost, path: "/api/agents/host/link", body: `{}`},
{method: http.MethodPost, path: "/api/agents/host/unlink", body: `{}`},
+ {method: http.MethodPost, path: "/api/agents/agent/uninstall", body: `{"agentId":"agent-1"}`},
+ {method: http.MethodPost, path: "/api/agents/host/uninstall", body: `{"agentId":"agent-1"}`},
{method: http.MethodGet, path: "/api/admin/profiles/", body: ""},
}
diff --git a/internal/api/router_routes_registration.go b/internal/api/router_routes_registration.go
index 1b3379d4d9..f66059c5bd 100644
--- a/internal/api/router_routes_registration.go
+++ b/internal/api/router_routes_registration.go
@@ -61,8 +61,11 @@ func (r *Router) registerConfigSystemRoutes(updateHandlers *UpdateHandlers) {
r.mux.HandleFunc("/api/agents/host/report", wrapLegacyHostAlias("/api/agents/host/report", RequireAuth(r.config, RequireScope(config.ScopeAgentReport, r.unifiedAgentHandlers.HandleReport))))
r.mux.HandleFunc("/api/agents/agent/lookup", RequireAuth(r.config, RequireScope(config.ScopeAgentReport, r.unifiedAgentHandlers.HandleLookup)))
r.mux.HandleFunc("/api/agents/host/lookup", wrapLegacyHostAlias("/api/agents/host/lookup", RequireAuth(r.config, RequireScope(config.ScopeAgentReport, r.unifiedAgentHandlers.HandleLookup))))
- r.mux.HandleFunc("/api/agents/agent/uninstall", RequireAuth(r.config, RequireScope(config.ScopeAgentReport, r.unifiedAgentHandlers.HandleUninstall)))
- r.mux.HandleFunc("/api/agents/host/uninstall", wrapLegacyHostAlias("/api/agents/host/uninstall", RequireAuth(r.config, RequireScope(config.ScopeAgentReport, r.unifiedAgentHandlers.HandleUninstall))))
+ // Uninstall is a collector's self-removal by token. Anything that is not a
+ // token takes the administrative removal path inside the handler, so it must
+ // be an administrator, as for the unlink and delete routes below.
+ r.mux.HandleFunc("/api/agents/agent/uninstall", RequireAdmin(r.config, RequireScope(config.ScopeAgentReport, r.unifiedAgentHandlers.HandleUninstall)))
+ r.mux.HandleFunc("/api/agents/host/uninstall", wrapLegacyHostAlias("/api/agents/host/uninstall", RequireAdmin(r.config, RequireScope(config.ScopeAgentReport, r.unifiedAgentHandlers.HandleUninstall))))
// SECURITY: Use settings:write (not just host_manage) to prevent compromised host tokens from manipulating other hosts
r.mux.HandleFunc("/api/agents/agent/unlink", RequireAdmin(r.config, RequireScope(config.ScopeSettingsWrite, r.unifiedAgentHandlers.HandleUnlink)))
r.mux.HandleFunc("/api/agents/host/unlink", wrapLegacyHostAlias("/api/agents/host/unlink", RequireAdmin(r.config, RequireScope(config.ScopeSettingsWrite, r.unifiedAgentHandlers.HandleUnlink))))