feat(api): IP/key rate limit on /api routes with configurable thresholds#19
Merged
BlindMaster24 merged 3 commits intoApr 27, 2026
Merged
Conversation
This was referenced Apr 24, 2026
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
e228e84
into
devin/1777023056-health-metrics
8 checks passed
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
Adds
express-rate-limitin front of${config.api.url}/:methodto cap abuse from unauthenticated (and authenticated) callers. Complements the existing per-API-key quota check inside the handler, which continues to enforce each key's configuredlimitPerSec.Why: until now anonymous hits on
/api/*could loop indefinitely before the auth check — trivial DoS surface. IP-only limiting wasn't enough either (shared NATs), so the limiter keys byauthorizationtoken when present and falls back to IP otherwise.What's new:
express-rate-limit@latest(v8). Exposed factorycreateApiRateLimiterfromsrc/services/api/index.tsso unit tests don't have to bootstrap the whole service.config.api.rateLimit.enabled === true(default true).auth tokenfirst, thenip— a cheap anon scraper can't drain the pool for legitimate keyed clients.RateLimitheaders (standardHeaders: 'draft-7',legacyHeaders: false).{"error":"Превышен лимит запросов"}.config.api.rateLimit:{ enabled, windowMs, max, trustProxy }with sane defaults (120 req/min). Zod schema + example config + AGENTS.md updated.HttpService.run()honorstrustProxy— required when running behind nginx/traefik/Cloudflare so the IP fallback key is the real client, not the proxy./api/healthand/api/metrics(registered onHttpServicein PR feat(http): unauthenticated /api/health and /api/metrics on http service #18) are NOT behind this limiter — they're monitoring endpoints and need to stay open.Tests (
tests/api/rate-limit.test.ts):maxfor anonymous IPRateLimitstandard header on successful responsesChecks:
pnpm run test:all— 31 files / 207 tests passpnpm run ts-check/pnpm run lint/pnpm run format:check— cleanpnpm audit --audit-level=low— no known vulnerabilitiesRun the new tests with:
pnpm exec vitest run tests/api/rate-limit.test.tsStacked on PR #18.
Review & Testing Checklist for Human
api.rateLimit.max: 5, rundocker compose up, hit any/api/...endpoint 6 times from the same IP → the 6th should return 429 with the Russian error payload.Authorization: Bearertokens concurrently — each token should have its own bucket.api.rateLimit.trustProxy: trueand confirmX-Forwarded-Foris honored (otherwise everybody shares one bucket)./api/healthand/api/metricsstay unthrottled — verify by hammering them under the limit.Notes
ApiKeyModel.limitPerSec) remain in place inside the handler; this PR adds a second, broader layer that also catches anonymous abuse.rate-limit-redis— the factory makes that a one-line change.Link to Devin session: https://app.devin.ai/sessions/7732f5fd16e9448295cbabeb8b5f471a
Requested by: @BlindMaster24