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.

Stop the Autopilot gate from leaking acknowledgement_id downstream

rcourtman3fcd26b5c319
diff --git a/internal/api/ai_handlers.go b/internal/api/ai_handlers.go
index fb4d4382e1..f44e91f317 100644
--- a/internal/api/ai_handlers.go
+++ b/internal/api/ai_handlers.go
@@ -7797,6 +7797,14 @@ func (h *AISettingsHandler) GatePatrolAutonomyUpdate(next http.HandlerFunc) http
}
compatUnlocked := true
settings.FullModeUnlocked = &compatUnlocked
+ // The acknowledgement is consumed by this gate and handed to the
+ // downstream handler via context. It must not survive into the
+ // normalized body: the Pro autonomy handler decodes the body
+ // strictly and rejects unknown fields, so forwarding
+ // acknowledgement_id made every Autopilot activation fail with
+ // "Invalid request body" on Pro builds.
+ consumedAcknowledgementID := settings.AcknowledgementID
+ settings.AcknowledgementID = ""
normalizedBody, err := json.Marshal(settings)
if err != nil {
writeErrorResponse(w, http.StatusInternalServerError, "encoding_failed", "Failed to prepare Patrol autonomy request", nil)
@@ -7804,7 +7812,7 @@ func (h *AISettingsHandler) GatePatrolAutonomyUpdate(next http.HandlerFunc) http
}
r.Body = io.NopCloser(bytes.NewReader(normalizedBody))
r.ContentLength = int64(len(normalizedBody))
- activationRequest := patrolAutopilotActivationRequest{AcknowledgementID: settings.AcknowledgementID, Actor: actor}
+ activationRequest := patrolAutopilotActivationRequest{AcknowledgementID: consumedAcknowledgementID, Actor: actor}
next(w, r.WithContext(context.WithValue(r.Context(), patrolAutopilotActivationContextKey{}, activationRequest)))
}
}
diff --git a/internal/api/patrol_autopilot_test.go b/internal/api/patrol_autopilot_test.go
index 019b0d1923..9018c0384c 100644
--- a/internal/api/patrol_autopilot_test.go
+++ b/internal/api/patrol_autopilot_test.go
@@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
+ "io"
"net/http"
"net/http/httptest"
"strings"
@@ -240,6 +241,55 @@ func TestPatrolAutopilotActivationRequiresCurrentBoundAcknowledgement(t *testing
}
}
+// The Pro autonomy handler (pulse-enterprise aiautofix) decodes the gated
+// body with DisallowUnknownFields and its settings struct has no
+// acknowledgement_id, so the gate's normalized body must not carry the
+// consumed acknowledgement. Forwarding it made every Autopilot activation
+// on Pro builds fail with "Invalid request body".
+func TestPatrolAutopilotActivationForwardsStrictDecodableBody(t *testing.T) {
+ now := time.Date(2026, 7, 19, 18, 0, 0, 0, time.UTC)
+ handler, _ := newPatrolAutopilotTestHandler(t, "org-a", &now)
+
+ created := createPatrolAutopilotAcknowledgement(t, handler, "org-a", "alice", "session-strict", "ack-strict-001")
+ if created.Code != http.StatusCreated {
+ t.Fatalf("create status=%d body=%s", created.Code, created.Body.String())
+ }
+
+ activate := patrolAutopilotSessionRequest(t, http.MethodPut, "/api/ai/patrol/autonomy", `{"autonomy_level":"full","acknowledgement_id":"ack-strict-001","investigation_budget":10,"investigation_timeout_sec":120}`, "org-a", "alice", "session-strict")
+ rec := httptest.NewRecorder()
+ handler.GatePatrolAutonomyUpdate(func(w http.ResponseWriter, gated *http.Request) {
+ body, err := io.ReadAll(gated.Body)
+ if err != nil {
+ t.Fatalf("read gated body: %v", err)
+ }
+ if strings.Contains(string(body), "acknowledgement_id") {
+ t.Fatalf("gated body leaks consumed acknowledgement_id: %s", body)
+ }
+ // Mirror of the enterprise handler's strict decode contract.
+ var downstream struct {
+ AutonomyLevel string `json:"autonomy_level"`
+ FullModeUnlocked *bool `json:"full_mode_unlocked,omitempty"`
+ InvestigationBudget int `json:"investigation_budget"`
+ InvestigationTimeoutSec int `json:"investigation_timeout_sec"`
+ }
+ dec := json.NewDecoder(bytes.NewReader(body))
+ dec.DisallowUnknownFields()
+ if err := dec.Decode(&downstream); err != nil {
+ t.Fatalf("gated body fails strict decode: %v body=%s", err, body)
+ }
+ if downstream.AutonomyLevel != string(config.PatrolAutonomyFull) || downstream.FullModeUnlocked == nil || !*downstream.FullModeUnlocked {
+ t.Fatalf("gated body lost activation shape: %s", body)
+ }
+ if req, ok := gated.Context().Value(patrolAutopilotActivationContextKey{}).(patrolAutopilotActivationRequest); !ok || req.AcknowledgementID != "ack-strict-001" {
+ t.Fatalf("activation context missing consumed acknowledgement: %#v", gated.Context().Value(patrolAutopilotActivationContextKey{}))
+ }
+ w.WriteHeader(http.StatusOK)
+ })(rec, activate)
+ if rec.Code != http.StatusOK {
+ t.Fatalf("gate status=%d body=%s", rec.Code, rec.Body.String())
+ }
+}
+
func TestPatrolAutopilotLowerModeDoesNotCreateOrRefreshAcknowledgement(t *testing.T) {
now := time.Date(2026, 7, 11, 20, 0, 0, 0, time.UTC)
handler, persistence := newPatrolAutopilotTestHandler(t, "org-a", &now)