Files
shared/backend/syncsocket/syncsocket_test.go
T
Gabriel20xxandClaude Haiku 5.5 199a5efaef Fix high audit findings and port the hidden-media rule
Fixed findings from the audit reports (SECURITY, QUALITY, PERFORMANCE):
- P-001 · Unbounded per-session timing maps and no connection cap in the sync hub
- S-001 · Anonymous `PUT /access` switches off login for every guarded route
- S-002 · Malformed cookies crash the Node middleware and fail requests in Java
- S-004 · Anonymous `PUT /sync` writes unvalidated JSON into shared session state

Also includes the pre-existing uncommitted changes in web-ui, README, shared-styles and the .changeset files, as the owner asked.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-10-19 07:47:10 +00:00

47 lines
1.4 KiB
Go

package syncsocket
import (
"fmt"
"testing"
)
// TestSessionTimingMapsStayBounded checks that fresh client-chosen session IDs cannot grow the
// per-session timing maps past maxSyncSessions.
func TestSessionTimingMapsStayBounded(t *testing.T) {
hub := NewHub()
p := &peer{clientID: "client-1"}
for i := 0; i < maxSyncSessions+100; i++ {
hub.handleSyncUpdate(p, map[string]any{
"clientId": "client-1",
"sessionId": fmt.Sprintf("session-%d", i),
"paused": false,
"playAt": "2026-10-19T10:00:00Z",
"playAtLocalMs": float64(1),
"capturedAtLocalMs": float64(1),
})
}
if n := len(hub.playAt); n > maxSyncSessions {
t.Errorf("playAt holds %d sessions, want at most %d", n, maxSyncSessions)
}
if n := len(hub.playAtLocalMs); n > maxSyncSessions {
t.Errorf("playAtLocalMs holds %d sessions, want at most %d", n, maxSyncSessions)
}
if n := len(hub.capturedAtMs); n > maxSyncSessions {
t.Errorf("capturedAtMs holds %d sessions, want at most %d", n, maxSyncSessions)
}
}
// TestHubRefusesPeersBeyondTheConnectionCap checks that the hub stops registering connections
// once it holds maxSyncConnections peers.
func TestHubRefusesPeersBeyondTheConnectionCap(t *testing.T) {
hub := NewHub()
for i := 0; i < maxSyncConnections; i++ {
if !hub.add(&peer{}) {
t.Fatalf("add refused peer %d, below the cap", i)
}
}
if hub.add(&peer{}) {
t.Fatal("add accepted a peer beyond maxSyncConnections")
}
}