Skip to content

fix(auth): rate-limit failed login attempts per identity - #1304

Merged
RUKAYAT-CODER merged 1 commit into
rinafcode:mainfrom
anifast-123:fix/identity-rate-limit-login
Aug 30, 2026
Merged

fix(auth): rate-limit failed login attempts per identity#1304
RUKAYAT-CODER merged 1 commit into
rinafcode:mainfrom
anifast-123:fix/identity-rate-limit-login

Conversation

@anifast-123

Copy link
Copy Markdown
Contributor

Summary

Adds per-identity (email) sliding-window rate limiting to prevent credential-stuffing attacks that rotate IPs but target the same account.

Changes

  • src/lib/ratelimit.ts: Added LOGIN_IDENTITY tier (5 attempts / 15 min window), checkIdentityRateLimit() and resetIdentityRateLimit() functions that track failed attempts per email using a identity: key prefix
  • src/lib/authMiddleware.ts: Added checkLoginRateLimit(email) and resetLoginRateLimit(email) helpers that wrap the identity rate limiter
  • src/app/api/auth/login/route.ts: Wired in per-identity checks — blocks when limit exceeded, increments counter on failed attempt, resets on successful login
  • src/app/api/tutorials/__tests__/ratelimit.test.ts: Added 6 tests covering limit enforcement, blocking, reset after success, independence across identities, remaining count, and window expiry

How It Works

  1. Before authentication: checkLoginRateLimit(email) checks if the email has exceeded 5 failed attempts in the last 15 minutes. If so, returns 429 immediately.
  2. On failed login: Each failed attempt increments the per-identity counter.
  3. On successful login: resetLoginRateLimit(email) clears the counter so the next batch of attempts starts fresh.
  4. Independent from IP limits: The identity-based limit runs alongside the existing IP-based AUTH tier, providing defense-in-depth against credential-stuffing bots that rotate IPs.

Testing

  • All 56 auth + rate-limit tests pass ✅
  • TypeScript typecheck passes ✅
  • No regressions detected ✅

Closes #1153

Add per-identity (email) sliding-window rate limiting to prevent
credential-stuffing attacks that rotate IPs but target the same account.

- Add LOGIN_IDENTITY tier (5 attempts / 15 min) in ratelimit.ts
- Add checkIdentityRateLimit() and resetIdentityRateLimit() functions
- Add checkLoginRateLimit() and resetLoginRateLimit() helpers in authMiddleware.ts
- Wire into login route: block on identity limit, reset on successful login
- Add 6 unit tests covering limit, block, reset, independence, and expiry

Closes rinafcode#1153

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
@drips-wave

drips-wave Bot commented Aug 30, 2026

Copy link
Copy Markdown

@anifast-123 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! 🚀

Learn more about application limits

@RUKAYAT-CODER

Copy link
Copy Markdown
Contributor

Thank you for contributing to the project.

@RUKAYAT-CODER
RUKAYAT-CODER merged commit c53b869 into rinafcode:main Aug 30, 2026
6 checks passed
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.

Rate-limit failed login attempts per identity

2 participants