{"id":"XE-0752","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"280","source_url":"https://github.com/xeprotocol/xe/issues/280","reporter_id":134,"title":"SSE subscribers unbounded — every chat broadcast walks all listeners under one lock","area":null,"claimed_tier":null,"assessed_tier":null,"verdict":"duplicate","duplicate_of":"XE-0027","tracked":false,"internal_ref":"","internal_url":"","status":"closed","closed_reason":"duplicate","github_state":"closed","disclosed_at":null,"award_xe":null,"award_approved":false,"paid_ref":"","created_at":"2026-11-05T15:53:50.238691+00:00","updated_at":"2026-11-10T00:11:36.261774+00:00","source_created_at":"2026-11-05T15:53:08+00:00","told_at":"2026-11-06T08:14:22+00:00","status_changed_at":"2026-11-06T08:14:08.209590+00:00","url":"https://bounty.xe.network/XE-0752","body":"//#What happens\n\n`GET /chat/events` (api/chat.go:78) calls `SubscribeChat()` once per connected client. In `chat/chat.go`:\n\ngo\nfunc (s ChatStore) Subscribe() (chan Envelope, func()) {\n ch := make(chan *Envelope, 64)\n s.listenerMu.Lock()\n s.listeners = append(s.listeners, ch)   // <- no cap, no per-IP limit, nothing\n s.listenerMu.Unlock()\n ...\n}\n\n\nThere is no cap on `listeners`. And every `Store()` does this:\n\ngo\ns.listenerMu.Lock()\nfor _, ch := range s.listeners {\n select {\n case ch <- env:\n default:\n }\n}\ns.listenerMu.Unlock()\n\n\nSo the cost of delivering one chat message is O(number of SSE connections on the node), all while holding `listenerMu` — which every `Store()` (i.e. every incoming p2p `account_chat` and every `POST /chat/send`) also needs.\n\nAuth for `/chat/events` is a one-time challenge+signature. The read rate limit is 200 req/s sustained, burst 1000 per IP. So one machine can stack up 200 connections per second, each one a permanent listener + goroutine.\n\n## Measured impact\n\nI benchmarked the real `ChatStore` with N subscribers and timed `Store()`:\nlisteners=0      Store() avg=45ns      heap=0.6 MB\nlisteners=1000   Store() avg=689ns     heap=1.3 MB\nlisteners=10000  Store() avg=5.5µs     heap=7.5 MB\nlisteners=50000  Store() avg=77.7µs    heap=36.5 MB\n\n\n50k connections (reachable in ~4 min from a single IP at the allowed rate) makes every chat broadcast ~1700x slower and serializes all chat ingestion behind `listenerMu`. Chat delivery for legitimate users degrades linearly while the attacker holds the connections open — and it costs the attacker nothing per message after connect.\n\n## Repro\n\ngo\nstore := chat.NewChatStore(100)\nsubs := make([]func(), 0, N)\nfor i := 0; i < N; i++ {\n    _, unsub := store.Subscribe()\n    subs = append(subs, unsub)\n}\n// now time store.Store(env) — grows linearly with N, see numbers above\n\n\nEnd-to-end version: open N `GET /chat/events?account=...&challenge=...&sig=...&pub_key=...` connections, keep them idle (the 20s ping keeps them alive), then watch `/chat/send` latency climb.\n\n## Related oddity\n\n`http.Server` is created with `WriteTimeout: 60s` (cmd/xe/main.go:1458) but SSE connections need to live indefinitely — either the write deadline kills every SSE stream at 60s (making the feature broken for real users) or it's exempted somehow. Might be worth checking while fixing this.\n\n## Suggested fix\n\nCap total listeners (e.g. 256) and reject new SSE connections beyond that; or per-IP SSE connection limit; and/or move broadcast outside the store lock (snapshot the slice, release, then fan out).\n\nThis lands under the Track 2 bullet \"Chat, SSE and API starvation under many concurrent clients\" almost word for word.\n\n\n","patch_refs":[],"thread":[{"id":566,"source":"github","source_ref":"6680387615","author":"Joseph Denne","team":true,"body":"Thanks. This is the same finding as https://bounty.xe.network/XE-0027, which was filed first; the programme pays the\nfirst verifiable submission, so this one closes as a duplicate. If you believe it is a different\nbug, reply with what differs and we will look again.","created_at":"2026-11-06T08:14:22+00:00"}]}