mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-28 16:58:31 +01:00
Separate user-editable host metadata writes from administrator command execution policy so monitoring:write callers cannot toggle or clear commandsEnabled. Require Docker container action dispatch to verify live session ownership and full 64-character lowercase container IDs, and refuse dispatch when a single token reports multiple Docker hosts for the target identity. Verify Proxmox node action runner routing against the live session hostname and prevent automatic host-report node links from stealing a node held by another reporting agent. Gate command details on agent resource-context, changes, and events routes behind action capability checks, and withhold probe credentials from agents using other tokens. Contract-Neutral: Security hardening for agent action routing, metadata authority, and command/probe disclosure without changing public API contracts.
185 lines
6.9 KiB
Go
185 lines
6.9 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/config"
|
|
unified "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
|
|
"github.com/rcourtman/pulse-go-rewrite/pkg/auth"
|
|
)
|
|
|
|
// Context and history can wait for stores or providers after authentication.
|
|
// Keep both the read authority and any command-disclosure authority used by
|
|
// their projections until disclosure. Never refresh an old request into a
|
|
// different reader or retry its lookup with wider/different authority.
|
|
func retainAgentCommandResponseAuthority(cfg *config.Config, next http.HandlerFunc) http.HandlerFunc {
|
|
read := retainTokenResponseAuthority(cfg, config.ScopeMonitoringRead, false, next)
|
|
// These scopes are BOTH required for a command-bearing projection, not
|
|
// interchangeable alternatives. Monitoring-only reads keep redaction.
|
|
commandRead := retainTokenResponseAuthority(cfg, config.ScopeAIExecute, false, read)
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if token := getAPITokenRecordFromRequest(r); token != nil && token.HasScope(config.ScopeAIExecute) {
|
|
commandRead(w, r)
|
|
return
|
|
}
|
|
read(w, r)
|
|
}
|
|
}
|
|
|
|
type agentCommandDisclosureContextKey struct{}
|
|
|
|
// withAgentCommandDisclosure binds, for callers without an API token, the
|
|
// question GET /api/actions asks before it shows the same execution detail:
|
|
// the action-approve capability answered by requestHasActionCapability. These
|
|
// routes stay open to every signed-in reader; a session or proxy user who may
|
|
// not review actions gets the redacted projection a monitoring:read token gets.
|
|
// The answer is asked again at each disclosure so a long-lived stream follows
|
|
// a withdrawn role, and it never widens past what the request was admitted with.
|
|
func withAgentCommandDisclosure(cfg *config.Config, authorizer auth.Authorizer, next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
admitted := requestHasActionCapability(cfg, authorizer, r, auth.ActionApprove)
|
|
disclosure := func(current *http.Request) bool {
|
|
return admitted && requestHasActionCapability(cfg, authorizer, current, auth.ActionApprove)
|
|
}
|
|
next(w, r.WithContext(context.WithValue(r.Context(), agentCommandDisclosureContextKey{}, disclosure)))
|
|
}
|
|
}
|
|
|
|
// requestCanReadAgentCommandPayloads keeps the token rule (ai:execute) and, for
|
|
// every other caller, the answer bound by withAgentCommandDisclosure. An
|
|
// unbound request fails closed.
|
|
func requestCanReadAgentCommandPayloads(r *http.Request) bool {
|
|
if token := getAPITokenRecordFromRequest(r); token != nil {
|
|
return token.HasScope(config.ScopeAIExecute)
|
|
}
|
|
disclosure, _ := r.Context().Value(agentCommandDisclosureContextKey{}).(func(*http.Request) bool)
|
|
return disclosure != nil && disclosure(r)
|
|
}
|
|
|
|
func redactAgentEventCommandsForRequest(event AgentEvent, r *http.Request) AgentEvent {
|
|
if requestCanReadAgentCommandPayloads(r) {
|
|
return event
|
|
}
|
|
switch payload := event.Payload.(type) {
|
|
case AgentEventApprovalPendingPayload:
|
|
event.Payload = redactAgentEventApprovalPendingCommand(payload)
|
|
case *AgentEventApprovalPendingPayload:
|
|
if payload != nil {
|
|
redacted := redactAgentEventApprovalPendingCommand(*payload)
|
|
event.Payload = &redacted
|
|
}
|
|
case AgentEventActionCompletedPayload:
|
|
event.Payload = redactAgentEventActionCompletedCommand(payload)
|
|
case *AgentEventActionCompletedPayload:
|
|
if payload != nil {
|
|
redacted := redactAgentEventActionCompletedCommand(*payload)
|
|
event.Payload = &redacted
|
|
}
|
|
}
|
|
return event
|
|
}
|
|
|
|
func redactAgentResourceContextCommandsForRequest(bundle *AgentResourceContext, r *http.Request) {
|
|
if bundle == nil || requestCanReadAgentCommandPayloads(r) {
|
|
return
|
|
}
|
|
for i := range bundle.PendingApprovals {
|
|
bundle.PendingApprovals[i] = redactAgentResourceApprovalCommand(bundle.PendingApprovals[i])
|
|
}
|
|
for i := range bundle.RecentActions {
|
|
bundle.RecentActions[i] = redactAgentResourceActionCommand(bundle.RecentActions[i])
|
|
}
|
|
}
|
|
|
|
func redactAgentEventApprovalPendingCommand(payload AgentEventApprovalPendingPayload) AgentEventApprovalPendingPayload {
|
|
if payload.Command != "" {
|
|
payload.Command = ""
|
|
payload.CommandRedacted = true
|
|
}
|
|
return payload
|
|
}
|
|
|
|
func redactAgentEventActionCompletedCommand(payload AgentEventActionCompletedPayload) AgentEventActionCompletedPayload {
|
|
if payload.ActionResultV2 != nil {
|
|
payload.ActionResultSummary = summarizeAgentActionResult(payload.ActionResultV2)
|
|
payload.ActionResultV2 = nil
|
|
payload.Success = payload.ActionResultSummary.Execution.Status == unified.ActionExecutionSucceeded
|
|
payload.CommandRedacted = true
|
|
}
|
|
if payload.ErrorMessage != "" {
|
|
payload.ErrorMessage = redactAgentActionError(payload.ErrorMessage)
|
|
payload.CommandRedacted = true
|
|
}
|
|
if payload.Command != "" {
|
|
payload.Command = ""
|
|
payload.CommandRedacted = true
|
|
}
|
|
if payload.Verification != nil {
|
|
verification := *payload.Verification
|
|
redactAgentResourceVerificationCommand(&verification)
|
|
payload.Verification = &verification
|
|
}
|
|
return payload
|
|
}
|
|
|
|
func redactAgentResourceApprovalCommand(summary AgentResourceApprovalSummary) AgentResourceApprovalSummary {
|
|
if summary.Command != "" {
|
|
summary.Command = ""
|
|
summary.CommandRedacted = true
|
|
}
|
|
return summary
|
|
}
|
|
|
|
func redactAgentResourceActionCommand(summary AgentResourceActionSummary) AgentResourceActionSummary {
|
|
if summary.ActionResultV2 != nil {
|
|
summary.ActionResultSummary = summarizeAgentActionResult(summary.ActionResultV2)
|
|
summary.ActionResultV2 = nil
|
|
summary.Success = summary.ActionResultSummary.Execution.Status == unified.ActionExecutionSucceeded
|
|
summary.CommandRedacted = true
|
|
}
|
|
if summary.ErrorMessage != "" {
|
|
summary.ErrorMessage = redactAgentActionError(summary.ErrorMessage)
|
|
summary.CommandRedacted = true
|
|
}
|
|
if summary.Command != "" {
|
|
summary.Command = ""
|
|
summary.CommandRedacted = true
|
|
}
|
|
if summary.Verification != nil {
|
|
verification := *summary.Verification
|
|
redactAgentResourceVerificationCommand(&verification)
|
|
summary.Verification = &verification
|
|
}
|
|
return summary
|
|
}
|
|
|
|
func redactAgentResourceVerificationCommand(verification *AgentResourceActionVerification) {
|
|
if verification == nil {
|
|
return
|
|
}
|
|
if verification.Command != "" || verification.Note != "" {
|
|
verification.Command = ""
|
|
verification.Note = ""
|
|
verification.CommandRedacted = true
|
|
}
|
|
}
|
|
|
|
// Error text can echo an arbitrary command or provider output, even after
|
|
// known credential patterns have been scrubbed. Keep only the documented,
|
|
// closed refusal vocabulary, never the free-form suffix (or an unknown prefix).
|
|
func redactAgentActionError(message string) string {
|
|
prefix, _, found := strings.Cut(message, ":")
|
|
if found {
|
|
switch prefix {
|
|
case "resource_remediation_locked", "plan_drift", "action_plan_drift",
|
|
"action_plan_expired", "action_dry_run_only", "action_not_approved",
|
|
"action_already_executing", "action_execution_final",
|
|
"action_execution_unavailable", "action_executor_unavailable":
|
|
return prefix + ":"
|
|
}
|
|
}
|
|
return "[redacted-action-detail]"
|
|
}
|