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.

security: fix websocket scope and agent impersonation

rcourtmanb7a94bad9fc6
diff --git a/internal/agentexec/server.go b/internal/agentexec/server.go
index ce5299f23a..f1cdc88ca6 100644
--- a/internal/agentexec/server.go
+++ b/internal/agentexec/server.go
@@ -32,7 +32,7 @@ type Server struct {
mu sync.RWMutex
agents map[string]*agentConn // agentID -> connection
pendingReqs map[string]chan CommandResultPayload // requestID -> response channel
- validateToken func(token string) bool
+ validateToken func(token string, agentID string) bool
}
type agentConn struct {
@@ -43,7 +43,7 @@ type agentConn struct {
}
// NewServer creates a new agent execution server
-func NewServer(validateToken func(token string) bool) *Server {
+func NewServer(validateToken func(token string, agentID string) bool) *Server {
return &Server{
agents: make(map[string]*agentConn),
pendingReqs: make(map[string]chan CommandResultPayload),
@@ -116,7 +116,7 @@ func (s *Server) HandleWebSocket(w http.ResponseWriter, r *http.Request) {
}
// Validate token
- if s.validateToken != nil && !s.validateToken(reg.Token) {
+ if s.validateToken != nil && !s.validateToken(reg.Token, reg.AgentID) {
log.Warn().Str("agent_id", reg.AgentID).Msg("Agent registration rejected: invalid token")
s.sendMessage(conn, Message{
Type: MsgTypeRegistered,
diff --git a/internal/agentexec/server_websocket_test.go b/internal/agentexec/server_websocket_test.go
index fb0f5a671d..304759610d 100644
--- a/internal/agentexec/server_websocket_test.go
+++ b/internal/agentexec/server_websocket_test.go
@@ -89,7 +89,7 @@ func waitFor(t *testing.T, timeout time.Duration, cond func() bool) {
}
func TestHandleWebSocket_RegistrationSuccessAndDisconnectRemovesAgent(t *testing.T) {
- s := NewServer(func(token string) bool { return token == "ok" })
+ s := NewServer(func(token string, agentID string) bool { return token == "ok" })
ts := newWSServer(t, s)
defer ts.Close()
@@ -126,7 +126,7 @@ func TestHandleWebSocket_RegistrationSuccessAndDisconnectRemovesAgent(t *testing
}
func TestHandleWebSocket_InvalidTokenRejected(t *testing.T) {
- s := NewServer(func(string) bool { return false })
+ s := NewServer(func(string, string) bool { return false })
ts := newWSServer(t, s)
defer ts.Close()
diff --git a/internal/api/ai_handlers_investigation_additional_test.go b/internal/api/ai_handlers_investigation_additional_test.go
index b849faf6ac..fca2602ae0 100644
--- a/internal/api/ai_handlers_investigation_additional_test.go
+++ b/internal/api/ai_handlers_investigation_additional_test.go
@@ -635,7 +635,7 @@ func registerAgent(t *testing.T, url, agentID, hostname string) *websocket.Conn
}
func TestFindAgentForTarget(t *testing.T) {
- server := agentexec.NewServer(func(string) bool { return true })
+ server := agentexec.NewServer(func(string, string) bool { return true })
ts := httptest.NewServer(http.HandlerFunc(server.HandleWebSocket))
defer ts.Close()
diff --git a/internal/api/router.go b/internal/api/router.go
index d1c26511ed..665baaace0 100644
--- a/internal/api/router.go
+++ b/internal/api/router.go
@@ -1317,7 +1317,8 @@ func (r *Router) setupRoutes() {
// Old API token endpoints removed - now using /api/security/regenerate-token
// Agent execution server for AI tool use
- r.agentExecServer = agentexec.NewServer(func(token string) bool {
+ // Agent execution server for AI tool use
+ r.agentExecServer = agentexec.NewServer(func(token string, agentID string) bool {
// Validate agent tokens using the API tokens system with scope check
if r.config == nil {
return false
@@ -1331,6 +1332,19 @@ func (r *Router) setupRoutes() {
Msg("Agent exec token missing required scope: agent:exec")
return false
}
+
+ // SECURITY: Check if token is bound to a specific agent
+ if boundID, ok := record.Metadata["bound_agent_id"]; ok && boundID != "" {
+ if boundID != agentID {
+ log.Warn().
+ Str("token_id", record.ID).
+ Str("bound_id", boundID).
+ Str("requested_id", agentID).
+ Msg("Agent token mismatch: token is bound to a different agent ID")
+ return false
+ }
+ }
+
return true
}
// Fall back to legacy single token if set (legacy tokens have wildcard access)
@@ -6281,6 +6295,11 @@ func (r *Router) handleWebSocket(w http.ResponseWriter, req *http.Request) {
if !CheckAuth(r.config, w, req) {
return
}
+ // SECURITY: Ensure monitoring:read scope for WebSocket connections
+ // This prevents tokens with only agent scopes from accessing full infra state via requestData
+ if !ensureScope(w, req, config.ScopeMonitoringRead) {
+ return
+ }
r.wsHub.HandleWebSocket(w, req)
}
diff --git a/internal/config/api_tokens.go b/internal/config/api_tokens.go
index cc0d0c2c1f..ec1131fcec 100644
--- a/internal/config/api_tokens.go
+++ b/internal/config/api_tokens.go
@@ -77,6 +77,10 @@ type APITokenRecord struct {
// If set, the token can access resources within any of these organizations.
// Takes precedence over OrgID if both are set.
OrgIDs []string `json:"orgIds,omitempty"`
+
+ // Metadata stores arbitrary key-value pairs for token binding.
+ // Used to bind tokens to specific resources (e.g., bound_agent_id).
+ Metadata map[string]string `json:"metadata,omitempty"`
}
// ensureScopes normalizes the scope slice, applying legacy defaults.
diff --git a/internal/license/license.go b/internal/license/license.go
index 23fa47bbf0..0dbdfa84b7 100644
--- a/internal/license/license.go
+++ b/internal/license/license.go
@@ -17,12 +17,22 @@ import (
// For development, leave empty to skip validation.
var publicKey ed25519.PublicKey
+// Legacy public key for validating licenses signed with the previous key.
+// Used during key rotation to maintain compatibility with existing licenses.
+var legacyPublicKey ed25519.PublicKey
+
// SetPublicKey sets the public key for license validation.
// This should be called during initialization with the production key.
func SetPublicKey(key ed25519.PublicKey) {
publicKey = key
}
+// SetLegacyPublicKey sets the legacy public key for validating old licenses.
+// This enables dual-key verification during key rotation periods.
+func SetLegacyPublicKey(key ed25519.PublicKey) {
+ legacyPublicKey = key
+}
+
// License errors
var (
ErrInvalidLicense = errors.New("invalid license key")