Skip to content

Commit a073439

Browse files
committed
fix(realtime): bound socket denial-audit throttle map to match app-side cap
1 parent a989e3b commit a073439

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

apps/realtime/src/middleware/network-policy.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,19 @@ interface HandshakeLike {
8282
* the log.
8383
*/
8484
const DENIAL_AUDIT_WINDOW_MS = 5 * 60 * 1000
85+
const DENIAL_AUDIT_MAX_TRACKED = 10_000
8586
const lastDenialAuditAt = new Map<string, number>()
8687

8788
function recordSocketDenial(userId: string, organizationId: string, clientIp: string | null): void {
8889
const last = lastDenialAuditAt.get(userId)
8990
if (last && Date.now() - last < DENIAL_AUDIT_WINDOW_MS) return
91+
// Bound the map (matching the app-side throttle): on overflow drop the
92+
// oldest-inserted key. Long-lived realtime process, many distinct denied
93+
// members — this keeps it a slow ceiling, not an unbounded leak.
94+
if (lastDenialAuditAt.size >= DENIAL_AUDIT_MAX_TRACKED) {
95+
const oldest = lastDenialAuditAt.keys().next().value
96+
if (oldest !== undefined) lastDenialAuditAt.delete(oldest)
97+
}
9098
lastDenialAuditAt.set(userId, Date.now())
9199
recordAudit({
92100
workspaceId: null,

0 commit comments

Comments
 (0)