fix(auth): add rate limiting to login and register#53
Conversation
|
Warning Review limit reached
Next review available in: 20 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Repository: alphaonelabs/coderabbit/.coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
WalkthroughIntroduces an in-memory, per-client-IP authentication rate limiter with configurable window and maximum attempt thresholds. Returns HTTP 429 responses with Retry-After headers when limits are exceeded. Integrates early checks into Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/worker.py`:
- Around line 452-458: Replace the current multi-header fallback logic for
client_ip with a trusted-only lookup: read solely
req.headers.get("CF-Connecting-IP") into client_ip (do not use "X-Forwarded-For"
or "X-Real-IP"), and if CF-Connecting-IP is missing or empty, bail out in a
fail-closed manner (e.g., log and return an error/429) rather than deriving an
IP from untrusted headers; update the subsequent key construction (key =
f"{route}:{client_ip}") to use this trusted client_ip value.
In `@tests/test_api_auth.py`:
- Around line 145-162: After the third call in
test_register_is_rate_limited_per_ip assert the 429 response includes a
non-empty Retry-After header and that the response body matches the rate-limit
payload produced by _too_many_requests (e.g., parse the response and assert the
error/message field or text contains the expected "Too Many Requests" content);
apply the same two assertions to test_login_is_rate_limited_per_ip, and in
test_login_rate_limit_resets_after_window add an extra immediate third request
after the successful post-window call to assert the bucket re-arms and returns
429 with Retry-After and the expected rate-limit body.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: alphaonelabs/coderabbit/.coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 60659546-7c64-4607-a400-2b2ffb68cab0
📒 Files selected for processing (3)
src/worker.pytests/helpers.pytests/test_api_auth.py
There was a problem hiding this comment.
Pull request overview
Adds in-memory, per-IP rate limiting for the auth endpoints to reduce brute-force/abuse on /api/login and /api/register, with new tests covering basic limiting behavior and window resets.
Changes:
- Introduces a shared auth rate-limit checker and 429 response helper (with
Retry-After). - Applies rate limiting to
api_registerandapi_login. - Adds pytest coverage for per-IP limiting and login window reset; updates test env defaults for rate-limit settings.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/worker.py |
Adds rate limiting state, config reading from env, and enforces it in login/register handlers. |
tests/test_api_auth.py |
Adds fixtures and new tests for rate limiting + window reset behavior. |
tests/helpers.py |
Sets default auth rate-limit env values in make_env() for tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@A1L13N Please review the PR if you geta chance, thanks! |
fix(auth): add rate limiting to login and register
Summary
Validation
Notes
Purpose
Adds rate limiting protection to the
/api/loginand/api/registerendpoints to prevent brute-force attacks and credential stuffing attempts.Key Modifications
Authentication Rate Limiter (src/worker.py)
AUTH_RATE_LIMIT_WINDOW_SECONDSandAUTH_RATE_LIMIT_MAX_ATTEMPTSRetry-Afterheader and CORS headersTest Infrastructure (tests/helpers.py)
make_env()helper to include rate-limiting configuration in the mocked environmentTest Coverage (tests/test_api_auth.py)
CF-Connecting-IPheader)Impact