feat(api): Add rate limiting to public endpoints#1165
Open
jeromehardaway wants to merge 1 commit into
Open
Conversation
Apply the existing in-memory sliding-window limiter to public endpoints via a new enforceRateLimit helper in src/lib/rate-limit.ts: - /api/j0di3/* — 30 req/min, enforced centrally in j0di3-proxy, keyed by troopId (falls back to user id, then client IP) - /api/contact — 5 req/min per IP - /api/health — 30 req/min per IP Exceeding a limit returns 429 with a Retry-After header derived from resetAt and a safe JSON error. Buckets are namespaced per endpoint so limits never bleed across routes. Limits are documented in docs/RATE_LIMITS.md, including the Redis upgrade path for multi-instance deployments.
Contributor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Problem
Public API endpoints — the J0dI3 proxy routes,
/api/contact, and/api/health— had no rate limiting, leaving them open to abuse (the contact endpoint in particular triggers AI spam classification and Slack webhooks on every request).Solution
Issue #1064 suggests Upstash Redis, but this repo already ships a tested in-memory sliding-window limiter (
src/lib/rate-limit.ts) that the docs note is single-instance only, with Redis as the scale-out path. This PR deliberately reuses that limiter rather than adding a dependency; the Redis swap is documented as the follow-up indocs/RATE_LIMITS.md.enforceRateLimit(req, res, { name, maxRequests, windowMs, key? })tosrc/lib/rate-limit.ts. Buckets are namespaced per endpoint so limits never bleed across routes. On limit it sends 429 with aRetry-Afterheader derived fromresetAtand a safe JSON error; every response carriesX-RateLimit-Remaining/X-RateLimit-Reset.src/lib/j0di3-proxy.tsdispatch(), so every/api/j0di3/*route (including admin proxies) is covered in one place: 30 req/min keyed by troopId, falling back to user id, then client IP.@swaggerblocks for/api/contactand/api/health(429 responses included); regeneratedpublic/swagger-spec.json.docs/RATE_LIMITS.md.Tests
enforceRateLimit: allow path + headers, 429 +Retry-After, per-name bucket isolation, custom key override.setHeaderso the limiter never bleeds across unrelated tests.Verification
npm run typecheck— 7 errors, all pre-existing on master (verified by stashing); no new errors introduced.npm run lint— changed files clean (0 errors); remaining repo errors are pre-existing in unrelated component tests.npm test— 41 files, 391 tests, all passing.npm run build— succeeds; swagger spec regenerated with the new paths.Follow-up
Swap the in-memory
Mapfor a shared store (e.g. Upstash Redis) if the app scales past a single instance — call sites stay the same.Closes #1064