High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(browsersession): sweep expired dashboard sessions and compare token hashes in constant time
Luigi Agostia80d44f64ca4
diff --git a/internal/browsersession/session.go b/internal/browsersession/session.goindex 882d48d7..cd6f0094 100644--- a/internal/browsersession/session.go+++ b/internal/browsersession/session.go@@ -1,8 +1,10 @@package browsersessionimport (+ "context""crypto/rand""crypto/sha256"+ "crypto/subtle""encoding/hex""encoding/json""os"@@ -296,7 +298,8 @@ func (m *Manager) applyConfigLocked(cfg Config) {}func (m *Manager) sessionValid(state sessionState, now time.Time, expected [32]byte) bool {- return m.sessionTimeValid(state, now) && state.TokenHash == expected+ return m.sessionTimeValid(state, now) &&+ subtle.ConstantTimeCompare(state.TokenHash[:], expected[:]) == 1}func (m *Manager) sessionTimeValid(state sessionState, now time.Time) bool {@@ -304,6 +307,46 @@ func (m *Manager) sessionTimeValid(state sessionState, now time.Time) bool {now.Sub(state.CreatedAt) <= m.maxLifetime}+// MaintenanceInterval is how often RunMaintenance sweeps expired sessions.+const MaintenanceInterval = 5 * time.Minute++// PruneExpired drops expired sessions and persists the result. Expiry is+// otherwise only noticed when a session's own cookie is presented again, so a+// dashboard login that is simply abandoned — a closed browser, a second device —+// is never detected, and its record plus its token hash survive in memory and in+// the persisted file for the life of the process.+func (m *Manager) PruneExpired() {+ if m == nil {+ return+ }+ m.mu.Lock()+ before := len(m.sessions)+ m.pruneExpiredLocked(m.now())+ var job saveJob+ if len(m.sessions) != before {+ job = m.snapshotLocked()+ }+ m.mu.Unlock()+ m.writeSnapshot(job)+}++// RunMaintenance sweeps expired sessions until ctx is done.+func (m *Manager) RunMaintenance(ctx context.Context) {+ if m == nil {+ return+ }+ t := time.NewTicker(MaintenanceInterval)+ defer t.Stop()+ for {+ select {+ case <-ctx.Done():+ return+ case <-t.C:+ m.PruneExpired()+ }+ }+}+func (m *Manager) pruneExpiredLocked(now time.Time) {for id, state := range m.sessions {if !m.sessionTimeValid(state, now) {diff --git a/internal/browsersession/session_test.go b/internal/browsersession/session_test.goindex 0a43bb60..c5d10c00 100644--- a/internal/browsersession/session_test.go+++ b/internal/browsersession/session_test.go@@ -424,3 +424,64 @@ func TestSessionManagerClearsElevationAcrossRestartByDefault(t *testing.T) {t.Fatal("IsElevated() after restart = true, want false when persistence across restart is disabled")}}++// A dashboard login that is never presented again — a closed browser, a second+// device — expires but is noticed by nothing: every other deletion path runs off+// the cookie the abandoned session no longer sends. Without a sweep its record and+// its token hash sit in memory and in the persisted file for the life of the+// process, and the file is rewritten whole on every later mutation.+func TestPruneExpiredForgetsAnAbandonedSession(t *testing.T) {+ path := filepath.Join(t.TempDir(), "sessions.json")+ now := time.Date(2026, 3, 18, 12, 0, 0, 0, time.UTC)+ mgr := NewManager(Config{+ IdleTimeout: time.Hour,+ MaxLifetime: 24 * time.Hour,+ Persist: true,+ PersistPath: path,+ })+ mgr.now = func() time.Time { return now }++ abandoned, err := mgr.Create("secret")+ if err != nil {+ t.Fatalf("Create: %v", err)+ }++ now = now.Add(90 * time.Minute)+ live, err := mgr.Create("secret")+ if err != nil {+ t.Fatalf("Create: %v", err)+ }++ mgr.PruneExpired()++ persisted := func() map[string]bool {+ data, readErr := os.ReadFile(path)+ if readErr != nil {+ t.Fatalf("read persist file: %v", readErr)+ }+ var ps persistedSessions+ if unmarshalErr := json.Unmarshal(data, &ps); unmarshalErr != nil {+ t.Fatalf("unmarshal: %v", unmarshalErr)+ }+ ids := map[string]bool{}+ for _, r := range ps.Sessions {+ ids[r.ID] = true+ }+ return ids+ }()++ mgr.mu.RLock()+ _, stillInMemory := mgr.sessions[abandoned]+ _, liveInMemory := mgr.sessions[live]+ mgr.mu.RUnlock()++ if stillInMemory {+ t.Error("the expired session is still in memory after the sweep")+ }+ if persisted[abandoned] {+ t.Error("the expired session's record and token hash are still on disk after the sweep")+ }+ if !liveInMemory || !persisted[live] {+ t.Error("the sweep dropped a session that is still within its idle timeout")+ }+}diff --git a/internal/server/server.go b/internal/server/server.goindex 9bc6eda1..45bccc69 100644--- a/internal/server/server.go+++ b/internal/server/server.go@@ -319,6 +319,7 @@ func RunDashboard(cfg *config.RuntimeConfig, version string) {maintenanceCtx, maintenanceCancel := context.WithCancel(context.Background())go orch.RunMaintenance(maintenanceCtx)go sessionStore.RunMaintenance(maintenanceCtx)+ go sessions.RunMaintenance(maintenanceCtx)shutdownOnce := &sync.Once{}doShutdown := func() {