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.

Keep Assistant streams bound to current token authority

pulse-triage[bot]7759875c556c
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
index 7fe16d2ddd..2065f7a51c 100644
--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
@@ -15,6 +15,17 @@
## Purpose
+### Assistant result streams do not own agent execution lifetime
+
+Assistant HTTP result readers recheck current token authority at their serialized
+writer, including deadline control. Token withdrawal denies further Assistant
+output; it does not introduce an agent disconnect, deployment cancellation or
+command-cancellation operation. Legacy execute and alert investigation retain
+their independent 15/5-minute work bounds; chat retains request cancellation.
+Independent authorised readers and session/proxy admission are unchanged. The
+native Assistant lifetime and persisted-delete tests in `internal/api/` cover
+this distinction without starting an agent or a command.
+
### Dashboard token lifetime is not agent-execution authority
An already-open dashboard `/ws` read loses access when its admitted API token
diff --git a/docs/release-control/v6/internal/subsystems/api-contracts.md b/docs/release-control/v6/internal/subsystems/api-contracts.md
index 71fcd28268..30b20e2977 100644
--- a/docs/release-control/v6/internal/subsystems/api-contracts.md
+++ b/docs/release-control/v6/internal/subsystems/api-contracts.md
@@ -21,6 +21,39 @@
## Purpose
+### Assistant streams use current token authority at disclosure
+
+The existing POST `execute/stream`, `investigate-alert` and `chat` routes
+revalidate their admitted API-token identity, principal and selected organisation
+at the actual serialized writer, including idle progress, heartbeats and terminal
+frames. The first two retain instance-admin and `ai:execute` requirements, with
+the guard outside the alert-analysis extension. Chat retains its existing
+`ai:chat` **or** relay-mobile scope alternatives; removing an unrelated scope or
+replacing one allowed chat scope with the other does not end a permitted read.
+Revocation, expiry, invalid runtime role, identity/principal replacement, lost
+scope or organisation/instance authority suppress subsequent output. No HTTP
+error or invented terminal success is appended to an admitted SSE response.
+A frame admitted before concurrent withdrawal can already be in flight.
+
+The guard exposes deadline setters, not an unguarded response-writer escape:
+Assistant handlers still clear the initial server deadlines and bound each
+write. Closure joins active transport writes and rejects queued/late callbacks.
+Legacy execute and investigation keep their independent background execution
+and original 15/5-minute bounds; denying their read is not cancelling their work,
+and HTTP completion can therefore wait for that work. Chat keeps its existing
+request-bound 15-minute execution and cancellation behaviour. Session/proxy
+admission, licence gates, payloads and other readers remain unchanged. This adds
+no route, grant, tool, event type or history source.
+
+`assistant_stream_lifetime_test.go` uses the full Router, native HTTP/SSE and
+real legacy/chat handlers against a held local text-only provider. It covers
+both token headers, fresh denials, independent readers, scope alternatives,
+non-token readers, instance/licence gates and actual idle execution lifetimes.
+`TestContract_AssistantStreamsWithdrawAfterPersistedTokenDelete` executes the
+real persisted DELETE for all three routes. Shared writer tests pin deadline
+forwarding, final closure and the lack of a bypass. These are synthetic source
+proofs, not installed/private-extension recovery, exposure or publication.
+
### Selected organization detail and billing request lifetimes
The existing organization `update` client optionally accepts the same
diff --git a/docs/release-control/v6/internal/subsystems/storage-recovery.md b/docs/release-control/v6/internal/subsystems/storage-recovery.md
index 8dbdcf897d..7352726b39 100644
--- a/docs/release-control/v6/internal/subsystems/storage-recovery.md
+++ b/docs/release-control/v6/internal/subsystems/storage-recovery.md
@@ -21,6 +21,17 @@
## Purpose
+### Assistant reader withdrawal is not a recovery mutation
+
+The shared API stream guard also covers Assistant results, not recovery-state
+mutation. Current API-token refusal suppresses subsequent response frames and
+retains the admitted organisation; it never deletes stored evidence, clears a
+recovery marker or cancels an independent recovery job. Deadline forwarding does
+not expose an unguarded writer. Legacy Assistant work remains background-bound;
+chat remains request-bound. Native Assistant independent-reader/lifetime and
+persisted-token-delete regressions in `internal/api/` pin this boundary using
+synthetic text, not a recovery replay or customer workload.
+
### Revoked monitoring sockets do not preserve recovery access
Dashboard `/ws` token reads revalidate current authority before data/queued
diff --git a/internal/api/agent_events_auth.go b/internal/api/agent_events_auth.go
index fe288ebc22..693c19ef97 100644
--- a/internal/api/agent_events_auth.go
+++ b/internal/api/agent_events_auth.go
@@ -45,7 +45,7 @@ func monitoringTokenSnapshotRevalidator(cfg *config.Config, req *http.Request) f
// tokenStreamSnapshotRevalidator preserves the stream's admitted credential,
// principal and organization while checking its current operation-specific
// scope. Callers must retain any additional route policy (e.g. instance admin).
-func tokenStreamSnapshotRevalidator(cfg *config.Config, req *http.Request, scope string) func() (*config.APITokenRecord, bool) {
+func tokenStreamSnapshotRevalidator(cfg *config.Config, req *http.Request, scopes ...string) func() (*config.APITokenRecord, bool) {
admitted := getAPITokenRecordFromRequest(req)
if admitted == nil {
return nil
@@ -77,8 +77,14 @@ func tokenStreamSnapshotRevalidator(cfg *config.Config, req *http.Request, scope
if !ok || (tenantInventory && tenantTokenConfigForRequest(req) != live) {
return nil, false
}
+ // Keep the route's existing scope alternatives (notably mobile chat)
+ // rather than requiring a new grant or pinning one interchangeable scope.
+ hasScope := false
+ for _, scope := range scopes {
+ hasScope = hasScope || current.HasScope(scope)
+ }
if current.ID != admitted.ID || apiTokenAuthenticatedUser(current) != apiTokenAuthenticatedUser(admitted) ||
- !current.HasScope(scope) || !checker.CheckAccess(current, "", orgID).Allowed {
+ !hasScope || !checker.CheckAccess(current, "", orgID).Allowed {
return nil, false
}
return current, true
diff --git a/internal/api/assistant_stream_lifetime_test.go b/internal/api/assistant_stream_lifetime_test.go
new file mode 100644
index 0000000000..0e040a6393
--- /dev/null
+++ b/internal/api/assistant_stream_lifetime_test.go
@@ -0,0 +1,388 @@
+package api
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "sync"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+ internalauth "github.com/rcourtman/pulse-go-rewrite/pkg/auth"
+ "github.com/rcourtman/pulse-go-rewrite/pkg/extensions"
+)
+
+const assistantPrivateResult = "synthetic-private-assistant-result"
+const assistantControlToken = "assistant-control-token.12345678"
+
+// Only text from a local fake Ollama, never model tools, agents or commands.
+// Separate gates/counters exclude startup probes and distinguish two real
+// requests: withdrawing one reader must not affect the other reader's result.
+type assistantProviderGate struct {
+ entered, release, finished chan struct{}
+ enterOnce, releaseOnce sync.Once
+ calls atomic.Int32
+ cancelled atomic.Bool
+}
+
+func newAssistantProviderGate() *assistantProviderGate {
+ return &assistantProviderGate{entered: make(chan struct{}), release: make(chan struct{}), finished: make(chan struct{})}
+}
+func (g *assistantProviderGate) unblock() { g.releaseOnce.Do(func() { close(g.release) }) }
+
+// The test binder preserves the extension seam and its licence refusal while
+// executing the actual core handler. It is not private-binary/licence proof.
+type assistantAnalysisFixture struct {
+ extensions.AIAlertAnalysisEndpoints
+ runtime extensions.AIAlertAnalysisRuntime
+ licensed *atomic.Bool
+}
+
+func (f assistantAnalysisFixture) HandleInvestigateAlert(w http.ResponseWriter, r *http.Request) {
+ if !f.licensed.Load() {
+ f.runtime.WriteLicenseRequired(w, featureAIAlertsKey, "Alert investigation requires Pulse Pro")
+ return
+ }
+ f.runtime.CoreHandlers.HandleInvestigateAlert(w, r)
+}
+
+type assistantStreamFixture struct {
+ router *Router
+ server *httptest.Server
+ token string
+ kind string
+ primary *assistantProviderGate
+ control *assistantProviderGate
+ licensed atomic.Bool
+ otherCalls atomic.Int32
+}
+
+func newAssistantStreamFixture(t *testing.T, kind string, scopes []string) *assistantStreamFixture {
+ t.Helper()
+ f := &assistantStreamFixture{kind: kind, primary: newAssistantProviderGate(), control: newAssistantProviderGate()}
+ f.licensed.Store(true)
+ provider := newIPv4HTTPServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/api/chat" {
+ mockOllamaForExecute().ServeHTTP(w, r)
+ return
+ }
+ var request struct {
+ Messages []struct{ Role, Content string } `json:"messages"`
+ }
+ if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
+ http.Error(w, "invalid fixture request", http.StatusBadRequest)
+ return
+ }
+ var gate *assistantProviderGate
+ for _, message := range request.Messages {
+ if message.Role != "user" {
+ continue
+ }
+ if strings.Contains(message.Content, "stream-fixture-primary") {
+ gate = f.primary
+ } else if strings.Contains(message.Content, "stream-fixture-control") {
+ gate = f.control
+ }
+ }
+ content := "synthetic provider ready"
+ if gate == nil {
+ f.otherCalls.Add(1)
+ } else {
+ if gate.calls.Add(1) != 1 {
+ http.Error(w, "unexpected repeat fixture request", http.StatusInternalServerError)
+ return
+ }
+ defer close(gate.finished)
+ gate.enterOnce.Do(func() { close(gate.entered) })
+ select {
+ case <-gate.release:
+ case <-r.Context().Done():
+ gate.cancelled.Store(true)
+ return
+ }
+ if r.Context().Err() != nil {
+ gate.cancelled.Store(true)
+ return
+ }
+ content = assistantPrivateResult
+ }
+ _ = json.NewEncoder(w).Encode(map[string]any{
+ "model": "llama3", "message": map[string]any{"role": "assistant", "content": content},
+ "done": true, "done_reason": "stop", "prompt_eval_count": 3, "eval_count": 5,
+ })
+ }))
+ t.Cleanup(provider.Close)
+
+ // NewRouter resolves the extension while binding routes. Restore the
+ // previous binder immediately; tests never run parallel with global setup.
+ aiAlertAnalysisBindMu.RLock()
+ previous := aiAlertAnalysisEndpointsBinder
+ aiAlertAnalysisBindMu.RUnlock()
+ if kind == "investigate-alert" {
+ SetAIAlertAnalysisEndpointsBinder(func(defaults extensions.AIAlertAnalysisEndpoints, runtime extensions.AIAlertAnalysisRuntime) extensions.AIAlertAnalysisEndpoints {
+ return assistantAnalysisFixture{AIAlertAnalysisEndpoints: defaults, runtime: runtime, licensed: &f.licensed}
+ })
+ defer SetAIAlertAnalysisEndpointsBinder(previous)
+ }
+ f.router, f.token = setupExecuteRouterForScopes(t, provider.URL, scopes, true)
+ f.router.config.APITokens = append(f.router.config.APITokens, newTokenRecord(t, assistantControlToken, []string{config.ScopeWildcard}, nil))
+ f.router.config.AuthUser = "assistant-fixture-admin"
+ f.router.config.ProxyAuthSecret = "synthetic-assistant-proxy-secret"
+ f.router.config.ProxyAuthUserHeader = "X-Remote-User"
+ if kind == "chat" {
+ if err := f.router.aiHandler.Start(context.Background(), nil); err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { _ = f.router.aiHandler.Stop(context.Background()) })
+ }
+ f.server = newIPv4HTTPServer(t, f.router.Handler())
+ t.Cleanup(f.server.Close)
+ t.Cleanup(func() { f.primary.unblock(); f.control.unblock() })
+ return f
+}
+
+func (f *assistantStreamFixture) request(t *testing.T, label, raw, transport string) *http.Request {
+ t.Helper()
+ path := "/api/ai/" + f.kind
+ if f.kind == "execute" {
+ path += "/stream"
+ }
+ body := fmt.Sprintf(`{"prompt":"Return stream-fixture-%s text without tools."}`, label)
+ if f.kind == "investigate-alert" {
+ body = fmt.Sprintf(`{"alertIdentifier":"fixture-%s","resource_name":"stream-fixture-%s","resource_type":"node","resource_id":"synthetic-node","alert_type":"cpu"}`, label, label)
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
+ t.Cleanup(cancel)
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, f.server.URL+path, strings.NewReader(body))
+ if err != nil {
+ t.Fatal(err)
+ }
+ req.Header.Set("Content-Type", "application/json")
+ req.Header.Set("X-Pulse-Org-ID", "default")
+ switch transport {
+ case "bearer":
+ req.Header.Set("Authorization", "Bearer "+raw)
+ case "session":
+ req.AddCookie(&http.Cookie{Name: cookieNameSession, Value: raw})
+ case "proxy":
+ req.Header.Set("X-Proxy-Secret", f.router.config.ProxyAuthSecret)
+ req.Header.Set("X-Remote-User", "assistant-fixture-admin")
+ default:
+ if raw != "" {
+ req.Header.Set("X-API-Token", raw)
+ }
+ }
+ return req
+}
+
+func (f *assistantStreamFixture) open(t *testing.T, label, raw, transport string) *http.Response {
+ t.Helper()
+ resp, err := f.server.Client().Do(f.request(t, label, raw, transport))
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { resp.Body.Close() })
+ if resp.StatusCode != http.StatusOK || !strings.Contains(resp.Header.Get("Content-Type"), "text/event-stream") {
+ data, _ := io.ReadAll(resp.Body)
+ t.Fatalf("admission = %d: %s", resp.StatusCode, data)
+ }
+ gate := f.primary
+ if label == "control" {
+ gate = f.control
+ }
+ select {
+ case <-gate.entered:
+ case <-time.After(5 * time.Second):
+ t.Fatal("execution did not reach its exact prompt-bound provider gate")
+ }
+ return resp
+}
+
+func readAssistantResult(t *testing.T, response *http.Response, visible bool) {
+ t.Helper()
+ data, err := io.ReadAll(response.Body)
+ response.Body.Close()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got := strings.Contains(string(data), assistantPrivateResult); got != visible {
+ t.Fatalf("private result visible = %t, want %t: %s", got, visible, data)
+ }
+ if !visible && (strings.Contains(string(data), `"type":"complete"`) || strings.Contains(string(data), `"type":"done"`) || strings.Contains(string(data), "Authentication required")) {
+ t.Fatalf("withdrawn stream appended terminal success or HTTP error: %s", data)
+ }
+}
+
+func assistantScope(kind string) string {
+ if kind == "chat" {
+ return config.ScopeAIChat
+ }
+ return config.ScopeAIExecute
+}
+
+func TestAssistantStreamsWithdrawAuthority(t *testing.T) {
+ for _, kind := range []string{"execute", "investigate-alert", "chat"} {
+ for _, transport := range []string{"header", "bearer"} {
+ for _, change := range []string{"revoked", "expired", "scope_removed", "org_removed", "credential_replaced", "principal_replaced", "invalid_role"} {
+ t.Run(kind+"/"+transport+"/"+change, func(t *testing.T) {
+ f := newAssistantStreamFixture(t, kind, []string{assistantScope(kind)})
+ control := f.open(t, "control", assistantControlToken, "header")
+ primary := f.open(t, "primary", f.token, transport)
+ config.Mu.Lock()
+ record := &f.router.config.APITokens[0]
+ switch change {
+ case "revoked":
+ f.router.config.RemoveAPIToken(record.ID)
+ case "expired":
+ past := time.Now().Add(-time.Second)
+ record.ExpiresAt = &past
+ case "scope_removed":
+ record.Scopes = []string{config.ScopeMonitoringRead}
+ case "org_removed":
+ record.OrgIDs = []string{"other-org"}
+ case "credential_replaced":
+ record.ID = "replacement-credential"
+ case "principal_replaced":
+ record.Metadata = map[string]string{apiTokenMetadataOwnerUserID: "replacement-principal"}
+ case "invalid_role":
+ record.Metadata = map[string]string{internalauth.RuntimeRoleMetadataKey: internalauth.RuntimeRoleActionRunner}
+ }
+ config.Mu.Unlock()
+ if change != "credential_replaced" && change != "principal_replaced" {
+ fresh, err := f.server.Client().Do(f.request(t, "primary", f.token, transport))
+ if err != nil {
+ t.Fatal(err)
+ }
+ fresh.Body.Close()
+ want := http.StatusForbidden
+ if change == "revoked" || change == "expired" || change == "invalid_role" {
+ want = http.StatusUnauthorized
+ }
+ if fresh.StatusCode != want || f.primary.calls.Load() != 1 {
+ t.Fatalf("fresh status/calls = %d/%d, want %d/1", fresh.StatusCode, f.primary.calls.Load(), want)
+ }
+ }
+ f.primary.unblock()
+ f.control.unblock()
+ readAssistantResult(t, primary, false)
+ readAssistantResult(t, control, true)
+ if kind != "chat" && f.primary.cancelled.Load() {
+ t.Fatal("read withdrawal cancelled independent legacy execution")
+ }
+ })
+ }
+ }
+ }
+}
+
+func TestAssistantStreamsPreserveScopeAlternativesAndNonTokenReaders(t *testing.T) {
+ for _, kind := range []string{"execute", "investigate-alert", "chat"} {
+ for _, transport := range []string{"header", "session", "proxy"} {
+ t.Run(kind+"/"+transport, func(t *testing.T) {
+ f := newAssistantStreamFixture(t, kind, []string{assistantScope(kind), config.ScopeSettingsRead})
+ raw := f.token
+ if transport == "session" {
+ raw = "synthetic-assistant-session"
+ GetSessionStore().CreateSession(raw, time.Hour, "fixture", "127.0.0.1", f.router.config.AuthUser)
+ t.Cleanup(func() { GetSessionStore().DeleteSession(raw) })
+ }
+ response := f.open(t, "primary", raw, transport)
+ config.Mu.Lock()
+ if transport == "header" {
+ // Losing an unrelated scope is not losing this route's read.
+ f.router.config.APITokens[0].Scopes = []string{assistantScope(kind)}
+ } else {
+ f.router.config.RemoveAPIToken(f.router.config.APITokens[0].ID)
+ }
+ config.Mu.Unlock()
+ f.primary.unblock()
+ readAssistantResult(t, response, true)
+ })
+ }
+ }
+ for _, scopes := range [][]string{{config.ScopeRelayMobileAccess}, {config.ScopeAIChat}, {config.ScopeAIChat, config.ScopeRelayMobileAccess}} {
+ t.Run(strings.Join(scopes, "+"), func(t *testing.T) {
+ f := newAssistantStreamFixture(t, "chat", scopes)
+ response := f.open(t, "primary", f.token, "header")
+ config.Mu.Lock()
+ f.router.config.APITokens[0].Scopes = []string{config.ScopeRelayMobileAccess}
+ config.Mu.Unlock()
+ f.primary.unblock()
+ readAssistantResult(t, response, true)
+ })
+ }
+}
+
+func TestAssistantStreamsRetainAdminAndLicenseGates(t *testing.T) {
+ for _, kind := range []string{"execute", "investigate-alert"} {
+ t.Run(kind, func(t *testing.T) {
+ f := newAssistantStreamFixture(t, kind, []string{config.ScopeAIExecute})
+ response := f.open(t, "primary", f.token, "header")
+ config.Mu.Lock()
+ // Still has selected-org access, but no longer instance authority.
+ f.router.config.APITokens[0].OrgIDs = []string{"default", "another-org"}
+ config.Mu.Unlock()
+ fresh, err := f.server.Client().Do(f.request(t, "primary", f.token, "header"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ fresh.Body.Close()
+ if fresh.StatusCode != http.StatusForbidden {
+ t.Fatalf("lost instance authority = %d", fresh.StatusCode)
+ }
+ f.primary.unblock()
+ readAssistantResult(t, response, false)
+ })
+ }
+ t.Run("extension-license", func(t *testing.T) {
+ f := newAssistantStreamFixture(t, "investigate-alert", []string{config.ScopeAIExecute})
+ f.licensed.Store(false)
+ response, err := f.server.Client().Do(f.request(t, "primary", f.token, "header"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ response.Body.Close()
+ if response.StatusCode != http.StatusPaymentRequired || f.primary.calls.Load() != 0 {
+ t.Fatalf("license refusal = %d, calls = %d", response.StatusCode, f.primary.calls.Load())
+ }
+ })
+}
+
+// Withdrawal is a read decision, not a new policy for stopping work. Legacy
+// handlers deliberately run independently of the browser; chat deliberately
+// cancels with its request. Exercise the real idle writer, not a new short timer.
+func TestAssistantStreamsPreserveExecutionLifetimes(t *testing.T) {
+ for _, kind := range []string{"execute", "investigate-alert", "chat"} {
+ t.Run(kind, func(t *testing.T) {
+ f := newAssistantStreamFixture(t, kind, []string{assistantScope(kind)})
+ response := f.open(t, "primary", f.token, "header")
+ config.Mu.Lock()
+ f.router.config.RemoveAPIToken(f.router.config.APITokens[0].ID)
+ config.Mu.Unlock()
+ if kind == "chat" {
+ select {
+ case <-f.primary.finished:
+ if !f.primary.cancelled.Load() {
+ t.Fatal("chat provider completed instead of observing request cancellation")
+ }
+ case <-time.After(8 * time.Second):
+ t.Fatal("chat idle writer did not cancel request-bound work")
+ }
+ } else {
+ select {
+ case <-f.primary.finished:
+ t.Fatal("idle read withdrawal ended independent legacy provider work")
+ case <-time.After(3 * time.Second):
+ }
+ f.primary.unblock()
+ }
+ readAssistantResult(t, response, false)
+ })
+ }
+}
diff --git a/internal/api/contract_test.go b/internal/api/contract_test.go
index 429e8dee61..79d7e519f1 100644
--- a/internal/api/contract_test.go
+++ b/internal/api/contract_test.go
@@ -28958,3 +28958,46 @@ func TestContractSupportBundleCredentialBoundary(t *testing.T) {
t.Fatal("download mutated live credentials or connection state")
}
}
+
+// Deleting a credential through the real persisted API withdraws existing
+// Assistant readers as well as fresh admission. Only local text-only providers
+// and temporary stores run; no command or private extension is needed.
+func TestContract_AssistantStreamsWithdrawAfterPersistedTokenDelete(t *testing.T) {
+ for _, kind := range []string{"execute", "investigate-alert", "chat"} {
+ t.Run(kind, func(t *testing.T) {
+ f := newAssistantStreamFixture(t, kind, []string{assistantScope(kind)})
+ primary := f.open(t, "primary", f.token, "header")
+ control := f.open(t, "control", assistantControlToken, "bearer")
+ id := f.router.config.APITokens[0].ID
+ req, err := http.NewRequest(http.MethodDelete, f.server.URL+"/api/security/tokens/"+id, nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ req.Header.Set("X-API-Token", assistantControlToken)
+ deleted, err := f.server.Client().Do(req)
+ if err != nil {
+ t.Fatal(err)
+ }
+ deleted.Body.Close()
+ if deleted.StatusCode != http.StatusNoContent {
+ t.Fatalf("persisted DELETE = %d", deleted.StatusCode)
+ }
+ records, err := f.router.persistence.LoadAPITokens()
+ if err != nil || len(records) != 1 || records[0].ID == id {
+ t.Fatalf("deleted token remains durable: records=%d error=%v", len(records), err)
+ }
+ fresh, err := f.server.Client().Do(f.request(t, "primary", f.token, "header"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ fresh.Body.Close()
+ if fresh.StatusCode != http.StatusUnauthorized || f.primary.calls.Load() != 1 {
+ t.Fatalf("fresh DELETE admission = %d; execution calls = %d", fresh.StatusCode, f.primary.calls.Load())
+ }
+ f.primary.unblock()
+ f.control.unblock()
+ readAssistantResult(t, primary, false)
+ readAssistantResult(t, control, true)
+ })
+ }
+}
diff --git a/internal/api/router_routes_ai_relay.go b/internal/api/router_routes_ai_relay.go
index 35d94aa4c6..53f4735723 100644
--- a/internal/api/router_routes_ai_relay.go
+++ b/internal/api/router_routes_ai_relay.go
@@ -49,9 +49,11 @@ func (r *Router) registerAIRelayRoutesGroup() {
// AI models list - require ai:chat scope (needed to select a model for chat)
r.mux.HandleFunc("/api/ai/models", RequireAuth(r.config, RequireScope(config.ScopeAIChat, r.aiSettingsHandler.HandleListModels)))
r.mux.HandleFunc("/api/ai/execute", RequireAdmin(r.config, RequireScope(config.ScopeAIExecute, r.aiSettingsHandler.HandleExecute)))
- r.mux.HandleFunc("/api/ai/execute/stream", RequireAdmin(r.config, RequireScope(config.ScopeAIExecute, r.aiSettingsHandler.HandleExecuteStream)))
+ r.mux.HandleFunc("/api/ai/execute/stream", RequireAdmin(r.config, RequireScope(config.ScopeAIExecute,
+ retainTokenStreamAuthority(r.config, config.ScopeAIExecute, true, r.aiSettingsHandler.HandleExecuteStream))))
r.mux.HandleFunc("/api/ai/kubernetes/analyze", RequireAdmin(r.config, RequireScope(config.ScopeAIExecute, r.aiAlertAnalysisEndpoints.HandleAnalyzeKubernetesCluster)))
- r.mux.HandleFunc("/api/ai/investigate-alert", RequireAdmin(r.config, RequireScope(config.ScopeAIExecute, r.aiAlertAnalysisEndpoints.HandleInvestigateAlert)))
+ r.mux.HandleFunc("/api/ai/investigate-alert", RequireAdmin(r.config, RequireScope(config.ScopeAIExecute,
+ retainTokenStreamAuthority(r.config, config.ScopeAIExecute, true, r.aiAlertAnalysisEndpoints.HandleInvestigateAlert))))
r.mux.HandleFunc("/api/ai/run-command", RequireAdmin(r.config, RequireScope(config.ScopeAIExecute, r.aiSettingsHandler.HandleRunCommand)))
// Knowledge reads belong to ai:chat; durable mutations require ai:execute.
@@ -257,7 +259,8 @@ func (r *Router) registerAIRelayRoutesGroup() {
// SECURITY: Status endpoint reports the chat service and should require ai:chat scope for token clients.
r.mux.HandleFunc("/api/ai/status", RequireAuth(r.config, RequireScope(config.ScopeAIChat, r.aiHandler.HandleStatus)))
r.mux.HandleFunc("/api/ai/workflow-prompts/activity", RequireAuth(r.config, RequireScope(config.ScopeAIChat, r.aiHandler.HandleRecordWorkflowPromptActivity)))
- r.mux.HandleFunc("/api/ai/chat", RequireAuth(r.config, requireRelayMobileRuntimeRoute(relayMobileRouteChatSend, r.aiHandler.HandleChat)))
+ r.mux.HandleFunc("/api/ai/chat", RequireAuth(r.config, requireRelayMobileRuntimeRoute(relayMobileRouteChatSend,
+ retainTokenStreamAuthorityForScopes(r.config, relayMobileRuntimeRouteSpecFor(relayMobileRouteChatSend).compatibleScopes(), false, r.aiHandler.HandleChat))))
r.mux.HandleFunc("/api/ai/sessions", RequireAuth(r.config, r.routeAISessionsCollection))
r.mux.HandleFunc("/api/ai/sessions/", RequireAuth(r.config, r.routeAISessions))
diff --git a/internal/api/token_stream_lifetime.go b/internal/api/token_stream_lifetime.go
index 1f65f041e5..55b1fc4bc4 100644
--- a/internal/api/token_stream_lifetime.go
+++ b/internal/api/token_stream_lifetime.go
@@ -15,8 +15,12 @@ import (
// is produced: replay and queued broadcasts can otherwise disclose after a
// credential is withdrawn. Session/proxy admission is intentionally unchanged.
func retainTokenStreamAuthority(cfg *config.Config, scope string, instanceAdmin bool, next http.HandlerFunc) http.HandlerFunc {
+ return retainTokenStreamAuthorityForScopes(cfg, []string{scope}, instanceAdmin, next)
+}
+
+func retainTokenStreamAuthorityForScopes(cfg *config.Config, scopes []string, instanceAdmin bool, next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
- revalidate := tokenStreamSnapshotRevalidator(cfg, r, scope)
+ revalidate := tokenStreamSnapshotRevalidator(cfg, r, scopes...)
allowed := func() bool {
if revalidate == nil {
// Session/proxy admission stays with the route. They still
@@ -89,6 +93,27 @@ type tokenStreamWriter struct {
func (w *tokenStreamWriter) Header() http.Header { return w.writer.Header() }
+// Forward deadline control without exposing Unwrap, which would also let a
+// caller bypass the disclosure guard. Assistant streams clear the server's
+// initial deadlines and bound every subsequent write, including heartbeats.
+func (w *tokenStreamWriter) SetWriteDeadline(deadline time.Time) error {
+ w.mu.Lock()
+ defer w.mu.Unlock()
+ if !w.checkLocked() {
+ return io.ErrClosedPipe
+ }
+ return http.NewResponseController(w.writer).SetWriteDeadline(deadline)
+}
+
+func (w *tokenStreamWriter) SetReadDeadline(deadline time.Time) error {
+ w.mu.Lock()
+ defer w.mu.Unlock()
+ if !w.checkLocked() {
+ return io.ErrClosedPipe
+ }
+ return http.NewResponseController(w.writer).SetReadDeadline(deadline)
+}
+
func (w *tokenStreamWriter) checkLocked() bool {
if w.closed {
return false
diff --git a/internal/api/token_stream_lifetime_test.go b/internal/api/token_stream_lifetime_test.go
index c7f43c5807..aa36908660 100644
--- a/internal/api/token_stream_lifetime_test.go
+++ b/internal/api/token_stream_lifetime_test.go
@@ -504,3 +504,57 @@ func TestTokenStreamWriterNonTokenReadClosesBeforeReturn(t *testing.T) {
t.Fatal("non-token read accepted a write after handler return")
}
}
+
+// Deadline forwarding is deliberately narrow: ResponseController must work,
+// but Unwrap/ReaderFrom must not expose an unguarded output path.
+type deadlineTokenStreamWriter struct {
+ *httptest.ResponseRecorder
+ writeDeadlines []time.Time
+ readDeadlines []time.Time
+}
+
+func (w *deadlineTokenStreamWriter) SetWriteDeadline(deadline time.Time) error {
+ w.writeDeadlines = append(w.writeDeadlines, deadline)
+ return nil
+}
+func (w *deadlineTokenStreamWriter) SetReadDeadline(deadline time.Time) error {
+ w.readDeadlines = append(w.readDeadlines, deadline)
+ return nil
+}
+
+func TestTokenStreamWriterPreservesDeadlineControlWithoutEscape(t *testing.T) {
+ base := &deadlineTokenStreamWriter{ResponseRecorder: httptest.NewRecorder()}
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ allowed := true
+ writer := &tokenStreamWriter{writer: base, flusher: base, ctx: ctx, cancel: cancel, allowed: func() bool { return allowed }}
+ controller := http.NewResponseController(writer)
+ deadline := time.Now().Add(10 * time.Second)
+ for _, d := range []time.Time{{}, deadline} {
+ if err := controller.SetWriteDeadline(d); err != nil {
+ t.Fatal(err)
+ }
+ if err := controller.SetReadDeadline(d); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if len(base.writeDeadlines) != 2 || !base.writeDeadlines[0].IsZero() || !base.writeDeadlines[1].Equal(deadline) || len(base.readDeadlines) != 2 || !base.readDeadlines[0].IsZero() || !base.readDeadlines[1].Equal(deadline) {
+ t.Fatal("deadline clearing/bounding did not reach the underlying transport")
+ }
+ if _, ok := any(writer).(interface{ Unwrap() http.ResponseWriter }); ok {
+ t.Fatal("writer exposes an unguarded response writer")
+ }
+ if _, ok := any(writer).(io.ReaderFrom); ok {
+ t.Fatal("writer exposes an unguarded ReaderFrom")
+ }
+ allowed = false
+ if err := controller.SetWriteDeadline(deadline); !errors.Is(err, io.ErrClosedPipe) {
+ t.Fatalf("withdrawn deadline control = %v", err)
+ }
+ if err := controller.SetReadDeadline(deadline); !errors.Is(err, io.ErrClosedPipe) {
+ t.Fatalf("closed deadline control = %v", err)
+ }
+ if len(base.writeDeadlines) != 2 || len(base.readDeadlines) != 2 || ctx.Err() == nil {
+ t.Fatal("withdrawn control reached transport or retained live request")
+ }
+}