fix(auth): add rate limiting to login and register#53
fix(auth): add rate limiting to login and register#53Flashl3opard wants to merge 3 commits intoalphaonelabs:mainfrom
Conversation
|
Warning Rate limit exceeded
Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 25 minutes and 22 seconds. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ 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