feat: per-key rate limiting with configurable fail-closed mode#374
Merged
Baskarayelu merged 1 commit intoMay 29, 2026
Merged
Conversation
- Add per-API-key Redis fixed-window buckets (keyed by apiKeyRecord.id) alongside the existing per-tenant bucket; a request is rejected when either counter exceeds the tier ceiling, preventing one noisy key from exhausting the shared tenant budget. - Separate tier limit (tenant bucket) from per-key limit (key bucket): options.max overrides the key ceiling only; the tenant ceiling always comes from the tier config (maxFree/maxPro/maxEnterprise). - Emit rate_limit_rejected_total Prometheus counter with labels tier, key_id, and reason (tenant_limit | key_limit | redis_unavailable). - Make fail-closed the production default: RATE_LIMIT_FAIL_OPEN now defaults to false when NODE_ENV=production and true in dev/test. The catch-block fallback in src/app.ts mirrors this so a validateConfig failure at startup cannot silently disable limits. - Add getRedis injectable option to RateLimitConfig for test isolation (avoids vi.doMock + dynamic import fragility). - Extend tests: per-key isolation, key-B allowed when key-A blocked, remaining header reflects tighter budget, rate_limit_rejected_total counter assertions for all three reasons, fail-closed 503, getKeyId helper, disabled middleware. - Update docs/api.md (Rate Limiting section) and docs/security.md (architecture, fail-closed, Prometheus metric, env vars, security considerations). Closes CredenceOrg#335
|
@abrak01 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
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.
Add per-API-key Redis fixed-window buckets (keyed by apiKeyRecord.id) alongside the existing per-tenant bucket; a request is rejected when either counter exceeds the tier ceiling, preventing one noisy key from exhausting the shared tenant budget.
Separate tier limit (tenant bucket) from per-key limit (key bucket): options.max overrides the key ceiling only; the tenant ceiling always comes from the tier config (maxFree/maxPro/maxEnterprise).
Emit rate_limit_rejected_total Prometheus counter with labels tier, key_id, and reason (tenant_limit | key_limit | redis_unavailable).
Make fail-closed the production default: RATE_LIMIT_FAIL_OPEN now defaults to false when NODE_ENV=production and true in dev/test. The catch-block fallback in src/app.ts mirrors this so a validateConfig failure at startup cannot silently disable limits.
Add getRedis injectable option to RateLimitConfig for test isolation (avoids vi.doMock + dynamic import fragility).
Extend tests: per-key isolation, key-B allowed when key-A blocked, remaining header reflects tighter budget, rate_limit_rejected_total counter assertions for all three reasons, fail-closed 503, getKeyId helper, disabled middleware.
Update docs/api.md (Rate Limiting section) and docs/security.md (architecture, fail-closed, Prometheus metric, env vars, security considerations).
Closes #335