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.

Join session progress writes before returning the HTTP reader

pulse-triage[bot]66349b52d611
diff --git a/internal/api/token_stream_lifetime.go b/internal/api/token_stream_lifetime.go
index 50fbf9b8b0..1f65f041e5 100644
--- a/internal/api/token_stream_lifetime.go
+++ b/internal/api/token_stream_lifetime.go
@@ -17,11 +17,12 @@ import (
func retainTokenStreamAuthority(cfg *config.Config, scope string, instanceAdmin bool, next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
revalidate := tokenStreamSnapshotRevalidator(cfg, r, scope)
- if revalidate == nil {
- next(w, r)
- return
- }
allowed := func() bool {
+ if revalidate == nil {
+ // Session/proxy admission stays with the route. They still
+ // need the same join-before-return protection from async writes.
+ return true
+ }
current, ok := revalidate()
return ok && (!instanceAdmin || tokenActsForInstance(current))
}
@@ -42,7 +43,7 @@ func retainTokenStreamAuthority(cfg *config.Config, scope string, instanceAdmin
ctx, cancel := context.WithCancel(r.Context())
writer := &tokenStreamWriter{writer: w, flusher: flusher, ctx: ctx, cancel: cancel, allowed: allowed}
watchDone := make(chan struct{})
- go func() {
+ watch := func() {
defer close(watchDone)
ticker := time.NewTicker(15 * time.Second)
defer ticker.Stop()
@@ -56,7 +57,13 @@ func retainTokenStreamAuthority(cfg *config.Config, scope string, instanceAdmin
}
}
}
- }()
+ }
+ if revalidate != nil {
+ go watch()
+ } else {
+ // No new periodic authentication checks for session/proxy readers.
+ close(watchDone)
+ }
defer func() {
// Join any in-flight broadcaster write before returning the HTTP
// writer, and reject queued writes even after handler cleanup.
diff --git a/internal/api/token_stream_lifetime_test.go b/internal/api/token_stream_lifetime_test.go
index 3bcdc83128..c7f43c5807 100644
--- a/internal/api/token_stream_lifetime_test.go
+++ b/internal/api/token_stream_lifetime_test.go
@@ -487,3 +487,20 @@ func TestTokenStreamWriterCloseJoinsActiveWrite(t *testing.T) {
t.Fatalf("late bytes = %q", base.Body.String())
}
}
+
+func TestTokenStreamWriterNonTokenReadClosesBeforeReturn(t *testing.T) {
+ var retained http.ResponseWriter
+ handler := retainTokenStreamAuthority(&config.Config{}, config.ScopeSettingsRead, true, func(w http.ResponseWriter, r *http.Request) {
+ retained = w
+ fmt.Fprint(w, "data: admitted-session\n\n")
+ w.(http.Flusher).Flush()
+ })
+ rec := httptest.NewRecorder()
+ handler(rec, httptest.NewRequest(http.MethodGet, "/api/updates/stream", nil))
+ if _, err := retained.Write([]byte("late-broadcast")); !errors.Is(err, io.ErrClosedPipe) {
+ t.Fatalf("non-token writer outlived HTTP handler: %v", err)
+ }
+ if strings.Contains(rec.Body.String(), "late-broadcast") {
+ t.Fatal("non-token read accepted a write after handler return")
+ }
+}