feat(server): add rate limiting middleware#17
Merged
Conversation
- Implement sliding window rate limiter with in-memory storage - Three limit tiers: general (100/min), auth (20/min), devicePoll (10/min) - Extract client IP from X-Forwarded-For, X-Real-IP, CF-Connecting-IP, Fly-Client-IP - Health endpoint exempt from rate limiting - Store cleanup function for preventing memory growth - 29 tests covering all functionality
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
Changes
New file:
packages/server/middleware/rate-limit.tsRate Limit Tiers:
general/api/v1/accounts/rpc,/api/v1/engine/rpcauth/api/v1/auth/*(except device/token)devicePoll/api/v1/auth/device/tokenFeatures:
getClientIp()- Extracts IP from proxy headers (X-Forwarded-For, X-Real-IP, CF-Connecting-IP, Fly-Client-IP)getLimitType()- Determines rate limit tier based on pathcheckLimit()- Core sliding window algorithmcheckRateLimit()- Main middleware functioncleanupExpiredEntries()- Periodic cleanup to prevent memory growth/health) exempt from rate limitingNew file:
packages/server/middleware/rate-limit.test.ts29 tests covering:
Updated:
packages/server/middleware/index.tsAdded exports for all rate limiting functions and types.
Testing
All 263 tests pass (29 new + 234 existing).
Notes