java-topology/docs/tickets/synapse-0002-sync-get-users-in-room-sequence-linear-scan.md

17 lines
1,003 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# synapse-0002: CWE-407 in synapse — sync handler get_users_in_room Sequence linear scan
**Severity:** HIGH
**File:** `synapse/handlers/sync.py:1439`
**Pattern:**
```python
for room_id, event in mem_last_change_by_room_id.items(): # O(R) rooms changed
...
if event.membership == Membership.JOIN:
user_ids_in_room = await self.store.get_users_in_room(room_id) # returns Sequence[str] (List)
if user_id in user_ids_in_room: # O(U) linear scan
mutable_joined_room_ids.add(room_id)
```
**Complexity:** O(R × U) — for each of R rooms that changed membership, scans all U users in the room
**Fix:** `set(await self.store.get_users_in_room(room_id))` or restructure to avoid the membership check entirely
**Speedup:** 10010000× at large room size (U=10000 users, R=10 rooms → 100000 comparisons vs 10 hash lookups)
**Hot path:** `_generate_sync_entry_for_rooms()` — called on every sync request when membership changes occur