fix(rate-limit): share counters via Redis store and drop spoofable client IPs - #22
Open
naobadiah01 wants to merge 1 commit into
Open
fix(rate-limit): share counters via Redis store and drop spoofable client IPs#22naobadiah01 wants to merge 1 commit into
naobadiah01 wants to merge 1 commit into
Conversation
…ient IPs Replace the per-process Map with a shared atomic store so limits survive restarts and hold across replicas. A Redis-backed store (INCR + PEXPIRE in a Lua script) is used when REDIS_URL is set, with an in-memory fallback for development and tests; all four limiters share one store instance. Client identity now comes from the socket address by default, and only the rightmost x-forwarded-for hop is trusted when TRUST_PROXY is enabled, so clients can no longer rotate headers to reset their budget. Docs and .env.example document the new REDIS_URL and TRUST_PROXY settings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #12. Rate limiting was a per-process
Mapkeyed by a client-suppliedheader — counters reset on every restart, were per-replica, and the identity
could be rotated by sending a different
x-forwarded-forper request.Changes
src/middleware/rate-limit-store.ts): newRateLimitStoreinterface with an atomicincrement(key, windowMs)returning
{ count, resetTime }. ARedisStoreimplements it with a Luascript (
INCR+PEXPIRE, fixed-window), andInMemoryStoreremains asthe fallback for development and tests.
generalLimiter,authLimiter,employerLimiter, andauthenticatedLimiterall share the single storesingleton, so limits are combined across replicas and survive restarts
when
REDIS_URLis configured.getClientIPuses the socket address unlessTRUST_PROXY=trueis set; only then is the rightmostx-forwarded-forhop trusted. Clients can no longer rotate headers to reset their budget.
than blocking valid traffic.
REDIS_URLandTRUST_PROXYadded to.env.example,docs/ARCHITECTURE.md, anddocs/OPERATIONS.md.Acceptance criteria
x-forwarded-forcannot rotate the key.env.exampledocument the Redis requirement and proxy settingTests
pnpm lint,pnpm test:coverage(rate-limit suite: 11 tests pass).