feat: add rate limiting — 60 req/min per IP across all endpoints#7
Merged
Conversation
Uses slowapi (IP-based) to return HTTP 429 when a client exceeds the configured limit. Default: 60/minute. Configurable via COMPASS_RATE_LIMIT in .env (e.g. "30/minute", "200/minute"). All endpoints (health, chat, upload, documents) share the same limit. Limit is intentionally flat — different per-endpoint limits can be added later if needed, see .env.example for guidance. Also fixes: added X-API-Key to CORS allowed headers (required for browsers when auth is enabled). Adds 2 tests verifying the slowapi handler and limiter are correctly wired to the app. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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 IP-based rate limiting via
slowapi. All endpoints return HTTP 429 when a client exceeds the configured limit.Default: 60 requests/minute per IP. Configurable via
COMPASS_RATE_LIMITin.env.All 4 endpoints share the same limit intentionally — flat limits are easier to reason about and adjust. Per-endpoint tuning can be added later if needed (documented in
.env.example).Also fixes: added
X-API-Keyto CORSallow_headers— required for browsers when auth is enabled.Configuration
Tests added (58 total, up from 56)
2 structural tests verifying slowapi is correctly wired:
RateLimitExceededhandler registered on the appapp.state(required by slowapi middleware)Note: the 429 behavior itself is tested by slowapi's own test suite — testing it here would require resetting the limiter between requests, which creates test isolation issues.
Test plan
🤖 Generated with Claude Code