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>
This commit is contained in:
Gabriel20xxandClaude Haiku 5.5 committed 2026-10-19 07:47:10 +00:00
1 parent 7fd2f14f91
commit 199a5efaef
12 files changed
+445 -42

No files matched your search

+29 -5
View File
@@ -31,8 +31,9 @@ import (
)
const (
maxSyncIdentifier = 128
maxSyncSessions = 1000
maxSyncIdentifier = 128
maxSyncSessions = 1000
maxSyncConnections = 1000
)
type peer struct {
@@ -73,10 +74,15 @@ func NewHub() *Hub {
}
}
func (h *Hub) add(p *peer) {
// add registers a peer, or reports false when the hub already holds maxSyncConnections peers.
func (h *Hub) add(p *peer) bool {
h.mu.Lock()
defer h.mu.Unlock()
if len(h.peers) >= maxSyncConnections {
return false
}
h.peers[p] = struct{}{}
h.mu.Unlock()
return true
}
func (h *Hub) remove(p *peer) string {
@@ -300,7 +306,10 @@ func (h *Hub) ServeWebSocket(w http.ResponseWriter, r *http.Request, allowedOrig
userAgent: firstNonEmpty(r.Header.Get("User-Agent"), "Unknown"),
ipAddress: firstNonEmpty(strings.Split(r.Header.Get("X-Forwarded-For"), ",")[0], r.RemoteAddr, "Unknown"),
}
h.add(p)
if !h.add(p) {
_ = conn.Close(websocket.StatusTryAgainLater, "too many sync connections")
return
}
defer func() {
sessionID := h.remove(p)
_ = conn.Close(websocket.StatusNormalClosure, "")
@@ -453,6 +462,9 @@ func (h *Hub) handleSyncUpdate(p *peer, message map[string]any) {
if captured := syncInt(message, "capturedAtLocalMs", 0); captured > 0 {
h.capturedAtMs[sessionID] = captured
}
trimSessionMap(h.playAt)
trimSessionMap(h.playAtLocalMs)
trimSessionMap(h.capturedAtMs)
if toClientID == "" {
h.states[sessionID] = cloneMap(state)
if len(h.states) > maxSyncSessions {
@@ -571,6 +583,18 @@ func cloneMap(value map[string]any) map[string]any {
return result
}
// trimSessionMap keeps a per-session map within maxSyncSessions by dropping entries, the same
// bound the states map uses. Session IDs come from clients, so without it each map would grow
// for every ID ever seen.
func trimSessionMap[V any](values map[string]V) {
for len(values) > maxSyncSessions {
for key := range values {
delete(values, key)
break
}
}
}
func firstNonEmpty(values ...string) string {
for _, value := range values {
if strings.TrimSpace(value) != "" {
+46
View File
@@ -0,0 +1,46 @@
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")
}
}