Skip to content

feat: add rate limiting — 60 req/min per IP across all endpoints#7

Merged
mrjunos merged 1 commit into
mainfrom
feat/rate-limiting
Apr 8, 2026
Merged

feat: add rate limiting — 60 req/min per IP across all endpoints#7
mrjunos merged 1 commit into
mainfrom
feat/rate-limiting

Conversation

@mrjunos

@mrjunos mrjunos commented Apr 8, 2026

Copy link
Copy Markdown
Owner

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_LIMIT in .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-Key to CORS allow_headers — required for browsers when auth is enabled.

Configuration

# .env
COMPASS_RATE_LIMIT=60/minute   # default
# COMPASS_RATE_LIMIT=30/minute  # tighter for public exposure
# COMPASS_RATE_LIMIT=200/minute # looser for trusted internal networks

Tests added (58 total, up from 56)

2 structural tests verifying slowapi is correctly wired:

  • RateLimitExceeded handler registered on the app
  • Limiter attached to app.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

  • 58 tests passing
  • CI green

🤖 Generated with Claude Code

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>
@mrjunos
mrjunos merged commit 475e163 into main Apr 8, 2026
1 check passed
@mrjunos
mrjunos deleted the feat/rate-limiting branch April 8, 2026 00:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant