Skip to content

fix(auth): clear rate limiter cleanup interval on server stop + cap map size (issue #18) - #37

Merged
labtgbot merged 1 commit into
mainfrom
issue-18-auth-rate-limiter-leak
Jun 15, 2026
Merged

fix(auth): clear rate limiter cleanup interval on server stop + cap map size (issue #18)#37
labtgbot merged 1 commit into
mainfrom
issue-18-auth-rate-limiter-leak

Conversation

@xdevrobot

Copy link
Copy Markdown
Collaborator

Summary

Fixes HIGH — Memory Leak + Security Bypass in the auth rate limiter. The setInterval cleanup timer created in createAuthMiddleware() was never cleared, holding a reference to the failedAttempts Map and preventing garbage collection. Under distributed attacks, the Map grew unbounded.

Root Cause

  1. setInterval created inside createAuthMiddleware() was never stored or cleared
  2. unref() only prevents keeping the process alive — it doesn't free the closure reference
  3. No way to dispose the timer from ApiServer.stop()
  4. No cap on failedAttempts Map size

Changes

src/api/middleware/auth.ts

  • Changed return type from MiddlewareHandler to { middleware, dispose } (new AuthMiddleware interface)
  • dispose() calls clearInterval() and failedAttempts.clear()
  • Added MAX_FAILED_ATTEMPTS_ENTRIES = 10_000 cap — oldest entry evicted when exceeded

src/api/server.ts

  • Import and store AuthMiddleware instance during setupRoutes()
  • stop() calls authMw.dispose() before closing the server

src/api/tests/api-server.test.ts

  • Updated test helper to use authMw.middleware (new return shape)

Security Impact

  • Memory leak proportional to attack traffic is eliminated
  • Cleanup interval is properly disposed on server stop
  • failedAttempts Map is capped at 10,000 entries to prevent distributed brute-force memory exhaustion
  • Stale blocked IPs are cleared immediately on server stop

Closes #18

🤖 Generated with Claude Code

…ap size (issue #18)

- createAuthMiddleware now returns { middleware, dispose } instead of just the middleware
- ApiServer.stop() calls dispose() to clearInterval and clear the failedAttempts Map
- Added MAX_FAILED_ATTEMPTS_ENTRIES cap (10,000) to prevent unbounded Map growth
  under distributed brute-force attacks with many unique IPs
@labtgbot
labtgbot merged commit 0f13b01 into main Jun 15, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[HIGH] Auth rate limiter cleanup interval is never cleared — memory leak + stale IP entries

2 participants