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 withdrawn credentials reading private diagnostic ZIPs

pulse-triage[bot]19fe353fbd79
diff --git a/internal/api/contract_test.go b/internal/api/contract_test.go
index 6e21543924..900ba5cbfd 100644
--- a/internal/api/contract_test.go
+++ b/internal/api/contract_test.go
@@ -12,6 +12,7 @@ import (
"fmt"
"io"
"math"
+ "math/rand"
"net"
"net/http"
"net/http/httptest"
@@ -29032,3 +29033,102 @@ func TestContract_AssistantJSONMediaBoundary(t *testing.T) {
}
}
}
+
+// Force many compressed writes using repeatable, incompressible synthetic log
+// data. Withdrawal occurs in the first admitted transport write, so that write
+// may finish, but no later ZIP bytes or appended authentication error may pass.
+func TestContractSupportBundleStopsAfterAdmittedPrefix(t *testing.T) {
+ changes := []string{"revoked", "expired", "scope", "org", "multi_org", "identity", "principal", "role", "unrelated_scope"}
+ for _, transport := range []string{"header", "bearer", "session"} {
+ cases := changes
+ if transport == "session" {
+ cases = []string{"invalidated", "expired", "principal", "replaced", "recovery"}
+ }
+ for _, change := range cases {
+ t.Run(transport+"/"+change, func(t *testing.T) {
+ cfg := newTestConfigWithTokens(t, newTokenRecord(t, logStreamToken, []string{config.ScopeSettingsRead, config.ScopeAIExecute}, nil))
+ const session = "synthetic-bundle-prefix-session"
+ if transport == "session" {
+ InitSessionStore(t.TempDir())
+ GetSessionStore().CreateSession(session, time.Hour, "fixture", "127.0.0.1", "operator")
+ t.Cleanup(func() { GetSessionStore().DeleteSession(session) })
+ }
+ data := make([]byte, 256*1024)
+ _, _ = rand.New(rand.NewSource(17)).Read(data)
+ cfg.LogFile = filepath.Join(t.TempDir(), "synthetic.log")
+ if err := os.WriteFile(cfg.LogFile, data, 0600); err != nil {
+ t.Fatal(err)
+ }
+ req := httptest.NewRequest(http.MethodGet, "/api/logs/download", nil)
+ switch transport {
+ case "bearer":
+ req.Header.Set("Authorization", "Bearer "+logStreamToken)
+ case "session":
+ req.AddCookie(&http.Cookie{Name: cookieNameSession, Value: session})
+ default:
+ req.Header.Set("X-API-Token", logStreamToken)
+ }
+ base := &logStreamMutationWriter{ResponseRecorder: httptest.NewRecorder()}
+ writes, prefix := 0, 0
+ base.afterWrite = func(_ string) {
+ writes++
+ if writes != 1 {
+ return
+ }
+ prefix = base.Body.Len()
+ if transport == "session" {
+ mutateReadSession(session, change)
+ return
+ }
+ config.Mu.Lock()
+ defer config.Mu.Unlock()
+ switch change {
+ case "revoked":
+ cfg.RemoveAPIToken(cfg.APITokens[0].ID)
+ case "expired":
+ past := time.Now().Add(-time.Second)
+ cfg.APITokens[0].ExpiresAt = &past
+ case "scope":
+ cfg.APITokens[0].Scopes = []string{config.ScopeMonitoringRead}
+ case "org":
+ cfg.APITokens[0].OrgID = "tenant-only"
+ case "multi_org":
+ cfg.APITokens[0].OrgIDs = []string{"default", "tenant-only"}
+ case "identity":
+ cfg.APITokens[0].ID = "different-credential"
+ case "principal":
+ cfg.APITokens[0].Metadata = map[string]string{apiTokenMetadataOwnerUserID: "different-owner"}
+ case "role":
+ cfg.APITokens[0].Metadata = map[string]string{authpkg.RuntimeRoleMetadataKey: authpkg.RuntimeRoleActionRunner}
+ case "unrelated_scope":
+ cfg.APITokens[0].Scopes = []string{config.ScopeSettingsRead}
+ }
+ }
+ h := NewLogHandlers(cfg, nil)
+ // Token extraction and session admission are real. The full Router/native
+ // suite separately covers the registered route and its persisted controls.
+ AuthContextMiddleware(cfg, nil, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if !CheckAuth(cfg, w, r) {
+ t.Fatal("synthetic admission failed")
+ }
+ h.HandleDownloadBundle(w, r)
+ })).ServeHTTP(base, req)
+ if base.Code != http.StatusOK || base.Header().Get("Content-Type") != "application/zip" || base.Header().Get("X-Content-Type-Options") != "nosniff" {
+ t.Fatalf("admitted archive headers changed: %d %v", base.Code, base.Header())
+ }
+ if change == "unrelated_scope" {
+ if writes < 2 || !bytes.Equal(readSupportBundle(t, base.Body.Bytes())["pulse.log"], data) {
+ t.Fatal("removing unrelated execute scope broke settings download")
+ }
+ return
+ }
+ if prefix == 0 || writes != 1 || base.Body.Len() != prefix {
+ t.Fatalf("ZIP output continued after withdrawal: writes=%d prefix=%d total=%d", writes, prefix, base.Body.Len())
+ }
+ if bytes.Contains(base.Body.Bytes(), []byte("Authentication required")) {
+ t.Fatal("authentication error appended to admitted ZIP")
+ }
+ })
+ }
+ }
+}
diff --git a/internal/api/log_bundle_authority_test.go b/internal/api/log_bundle_authority_test.go
new file mode 100644
index 0000000000..9e4ab721ff
--- /dev/null
+++ b/internal/api/log_bundle_authority_test.go
@@ -0,0 +1,38 @@
+package api
+
+import (
+ "net/http/httptest"
+ "testing"
+)
+
+func TestSupportBundleMediaDoesNotWidenAssistantResults(t *testing.T) {
+ for _, policy := range []struct {
+ name, media string
+ zip bool
+ }{{"json", "application/json", false}, {"zip", "application/zip", true}} {
+ for _, provided := range []string{"", "application/json", "application/zip", "text/html", "image/svg+xml", "application/json; invalid"} {
+ t.Run(policy.name+"/"+provided, func(t *testing.T) {
+ base := httptest.NewRecorder()
+ writer := &tokenResponseWriter{writer: base, header: base.Header().Clone(), allowed: func() bool { return true }, zipDownload: policy.zip}
+ writer.Header()["content-type"] = []string{"text/html"}
+ writer.Header().Set("Content-Type", provided)
+ if _, err := writer.Write([]byte("synthetic bytes")); err != nil {
+ t.Fatal(err)
+ }
+ want := "text/plain; charset=utf-8"
+ if provided == policy.media {
+ want = policy.media
+ }
+ if base.Header().Get("Content-Type") != want || base.Header().Get("X-Content-Type-Options") != "nosniff" {
+ t.Fatalf("media = %v", base.Header())
+ }
+ if _, exists := base.Header()["content-type"]; exists {
+ t.Fatal("alternate-case media bypassed boundary")
+ }
+ if base.Body.String() != "synthetic bytes" {
+ t.Fatal("media selection changed body bytes")
+ }
+ })
+ }
+ }
+}
diff --git a/internal/api/log_bundle_lifetime_linux_test.go b/internal/api/log_bundle_lifetime_linux_test.go
new file mode 100644
index 0000000000..3fd5d031ac
--- /dev/null
+++ b/internal/api/log_bundle_lifetime_linux_test.go
@@ -0,0 +1,152 @@
+package api
+
+import (
+ "bytes"
+ "context"
+ "io"
+ "net/http"
+ "os"
+ "path/filepath"
+ "syscall"
+ "testing"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+)
+
+// A real FIFO holds ordinary log generation after full Router admission but
+// before ZIP bytes reach the transport. Only synthetic data and temp stores.
+func TestSupportBundleNativeWithdrawal(t *testing.T) {
+ for _, transport := range []string{"header", "bearer", "session"} {
+ t.Run(transport, func(t *testing.T) {
+ router, cfg, server := newLogStreamRouter(t)
+ fifo := filepath.Join(t.TempDir(), "held.log")
+ if err := syscall.Mkfifo(fifo, 0600); err != nil {
+ t.Fatal(err)
+ }
+ config.Mu.Lock()
+ cfg.LogFile = fifo
+ config.Mu.Unlock()
+ raw := logStreamToken
+ if transport == "session" {
+ raw = "synthetic-held-bundle-session"
+ GetSessionStore().CreateSession(raw, time.Hour, "fixture", "127.0.0.1", cfg.AuthUser)
+ t.Cleanup(func() { GetSessionStore().DeleteSession(raw) })
+ }
+ ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second)
+ defer cancel()
+ req, _ := http.NewRequestWithContext(ctx, http.MethodGet, server.URL+"/api/logs/download", nil)
+ switch transport {
+ case "bearer":
+ req.Header.Set("Authorization", "Bearer "+raw)
+ case "session":
+ req.AddCookie(&http.Cookie{Name: cookieNameSession, Value: raw})
+ default:
+ req.Header.Set("X-API-Token", raw)
+ }
+ type result struct {
+ response *http.Response
+ err error
+ }
+ done := make(chan result, 1)
+ go func() { response, err := server.Client().Do(req); done <- result{response, err} }()
+ // O_NONBLOCK avoids a test goroutine stuck in open if admission fails.
+ var pipe *os.File
+ deadline := time.Now().Add(5 * time.Second)
+ for time.Now().Before(deadline) {
+ fd, err := syscall.Open(fifo, syscall.O_WRONLY|syscall.O_NONBLOCK, 0600)
+ if err == nil {
+ pipe = os.NewFile(uintptr(fd), fifo)
+ break
+ }
+ if err != syscall.ENXIO {
+ t.Fatal(err)
+ }
+ time.Sleep(time.Millisecond)
+ }
+ if pipe == nil {
+ unblock, _ := os.OpenFile(fifo, os.O_RDWR, 0600)
+ if unblock != nil {
+ unblock.Close()
+ }
+ t.Fatal("download did not reach held log generation")
+ }
+ defer pipe.Close()
+ // Handler has captured/opened the FIFO. Further requests use a normal log.
+ controlFile := filepath.Join(t.TempDir(), "control.log")
+ const marker = "synthetic confidential log after withdrawal"
+ if err := os.WriteFile(controlFile, []byte(marker), 0600); err != nil {
+ t.Fatal(err)
+ }
+ config.Mu.Lock()
+ cfg.LogFile = controlFile
+ config.Mu.Unlock()
+ if transport == "session" {
+ logoutReadSession(t, server.URL, raw)
+ } else {
+ id := cfg.APITokens[0].ID
+ deletion, _ := http.NewRequest(http.MethodDelete, server.URL+"/api/security/tokens/"+id, nil)
+ deletion.Header.Set("X-API-Token", logStreamControl)
+ response, err := server.Client().Do(deletion)
+ if err != nil {
+ t.Fatal(err)
+ }
+ response.Body.Close()
+ persisted, err := router.persistence.LoadAPITokens()
+ if response.StatusCode != http.StatusNoContent || err != nil || len(persisted) != 1 || persisted[0].ID == id {
+ t.Fatal("real DELETE did not persist withdrawal")
+ }
+ }
+ // A fresh request already refuses the same credential.
+ fresh := req.Clone(ctx)
+ response, err := server.Client().Do(fresh)
+ if err != nil {
+ t.Fatal(err)
+ }
+ response.Body.Close()
+ if response.StatusCode != http.StatusUnauthorized {
+ t.Fatalf("fresh admission = %d", response.StatusCode)
+ }
+ if _, err := pipe.Write([]byte(marker)); err != nil {
+ t.Fatal(err)
+ }
+ pipe.Close()
+ var completed result
+ select {
+ case completed = <-done:
+ case <-ctx.Done():
+ t.Fatal("held download failed to finish")
+ }
+ if completed.err != nil {
+ t.Fatal(completed.err)
+ }
+ defer completed.response.Body.Close()
+ body, err := io.ReadAll(completed.response.Body)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if completed.response.StatusCode != http.StatusUnauthorized {
+ // Decode to distinguish a private-log disclosure from a generic status
+ // mismatch. No original/customer material enters this fixture or output.
+ disclosed := bytes.Contains(readSupportBundle(t, body)["pulse.log"], []byte(marker))
+ t.Errorf("held download = %d after persisted withdrawal (private log disclosed=%v); want generic 401", completed.response.StatusCode, disclosed)
+ } else if string(body) != "Authentication required\n" || completed.response.Header.Get("Content-Disposition") != "" || completed.response.Header.Get("Content-Type") != "text/plain; charset=utf-8" {
+ t.Error("withdrawn response retained private ZIP headers/body")
+ }
+ control, _ := http.NewRequestWithContext(ctx, http.MethodGet, server.URL+"/api/logs/download", nil)
+ control.Header.Set("X-API-Token", logStreamControl)
+ independent, err := server.Client().Do(control)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer independent.Body.Close()
+ independentBody, err := io.ReadAll(independent.Body)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if independent.StatusCode != http.StatusOK || independent.Header.Get("Content-Type") != "application/zip" || !bytes.Equal(readSupportBundle(t, independentBody)["pulse.log"], []byte(marker)) {
+ t.Fatal("independent settings reader lost original log evidence")
+ }
+ })
+ }
+}
diff --git a/internal/api/log_handlers.go b/internal/api/log_handlers.go
index c2d28a21ed..a467422a0d 100644
--- a/internal/api/log_handlers.go
+++ b/internal/api/log_handlers.go
@@ -107,6 +107,13 @@ func (h *LogHandlers) HandleStreamLogs(w http.ResponseWriter, r *http.Request) {
// HandleDownloadBundle creates a private log archive with an allowlisted diagnostic summary.
// Log contents are not scrubbed; this is not an anonymised export or configuration backup.
func (h *LogHandlers) HandleDownloadBundle(w http.ResponseWriter, r *http.Request) {
+ // Raw logs are private even though the system summary is allowlisted.
+ // Recheck the admitted token/session at each actual ZIP transport write,
+ // without buffering the archive or withdrawing other readers/work.
+ retainResponseAuthority(h.config, config.ScopeSettingsRead, true, true, h.writeDownloadBundle)(w, r)
+}
+
+func (h *LogHandlers) writeDownloadBundle(w http.ResponseWriter, r *http.Request) {
// Snapshot only diagnostic fields while holding configuration ownership.
// Do not serialize Config or the full process environment into this archive.
config.Mu.RLock()
diff --git a/internal/api/token_response_authority.go b/internal/api/token_response_authority.go
index e0d956520c..9963105fc7 100644
--- a/internal/api/token_response_authority.go
+++ b/internal/api/token_response_authority.go
@@ -15,6 +15,13 @@ import (
// handler waits for a provider. Keep the original work/context lifetime, but
// recheck authority at disclosure, including headers and error responses.
func retainTokenResponseAuthority(cfg *config.Config, scope string, instanceAdmin bool, next http.HandlerFunc) http.HandlerFunc {
+ return retainResponseAuthority(cfg, scope, instanceAdmin, false, next)
+}
+
+// retainResponseAuthority also serves the existing private diagnostic download.
+// The caller selects JSON or ZIP, never arbitrary executable media. Authority,
+// closure and plain-text refusals stay shared with synchronous Assistant results.
+func retainResponseAuthority(cfg *config.Config, scope string, instanceAdmin bool, zipDownload bool, next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
revalidate := tokenStreamSnapshotRevalidator(cfg, r, scope)
sessionAllowed := sessionReadRevalidator(r)
@@ -34,7 +41,7 @@ func retainTokenResponseAuthority(cfg *config.Config, scope string, instanceAdmi
http.Error(w, "Authentication required", http.StatusUnauthorized)
return
}
- writer := &tokenResponseWriter{writer: w, header: w.Header().Clone(), allowed: allowed}
+ writer := &tokenResponseWriter{writer: w, header: w.Header().Clone(), allowed: allowed, zipDownload: zipDownload}
defer writer.close()
next(writer, r)
// A bodyless handler must not implicitly return 200 after withdrawal.
@@ -47,12 +54,13 @@ func retainTokenResponseAuthority(cfg *config.Config, scope string, instanceAdmi
// write produces a generic 401, not false-success/empty JSON; after any admitted
// response bytes, denial only stops further output, never appends an auth error.
type tokenResponseWriter struct {
- writer http.ResponseWriter
- header http.Header
- allowed func() bool
- mu sync.Mutex
- started bool
- closed bool
+ writer http.ResponseWriter
+ header http.Header
+ allowed func() bool
+ zipDownload bool
+ mu sync.Mutex
+ started bool
+ closed bool
}
func (w *tokenResponseWriter) Header() http.Header { return w.header }
@@ -83,14 +91,18 @@ func (w *tokenResponseWriter) commitLocked(status int) {
}
header[key] = append([]string(nil), values...)
}
- // These result endpoints serve JSON or plain-text errors, never HTML.
+ // Results serve only their pinned media type or plain-text errors, never HTML.
// Bind non-executable media to the actual transport, not just the
// handler's private header map. Missing, malformed or unexpected
// types must not allow HTTP sniffing or an extension to render HTML.
// Leave payload bytes alone: HTML-escaping JSON would corrupt data.
mediaType, _, err := mime.ParseMediaType(w.header.Get("Content-Type"))
- if err == nil && mediaType == "application/json" {
- header.Set("Content-Type", "application/json")
+ expectedType := "application/json"
+ if w.zipDownload {
+ expectedType = "application/zip"
+ }
+ if err == nil && mediaType == expectedType {
+ header.Set("Content-Type", expectedType)
} else {
header.Set("Content-Type", "text/plain; charset=utf-8")
}