feat(auth): enforce resource and rate limits for auth and recovery flows (#1651) - #1695
Merged
Baskarayelu merged 1 commit intoAug 30, 2026
Conversation
…ows (Remitwise-Org#1651) Implement production-grade resource and rate limits across sign-in, refresh, verification, logout, and recovery flows so the system provides deterministic guarantees under normal, invalid, repeated, concurrent, and failure conditions. Changes: - Add AccountLockoutService: per-email failed sign-in tracking with configurable max attempts (5), window (15 min), and lockout duration (15 min). Returns HTTP 429 with Retry-After guidance. - Add rate limits to logout (10/min) and logout-all (5/min) endpoints. - Add input size bounds (@maxlength) to all auth DTOs: email (254), password (72, bcrypt limit), refresh token (4096), verification token (256). - Add missing AuditAction enum values (SIGN_IN, REFRESH, LOGOUT, etc.) needed by the auth module. - Add 21 regression tests covering lockout, input bounds, 429 responses, email normalization, bounded store eviction, and concurrent load. - Add design documentation with invariants, failure behavior, compatibility impact, and security assumptions. Closes Remitwise-Org#1651
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.
Closes #1651
Objective
Make sign-in, refresh, verification, logout, and recovery flows safe across expiry, retries, multiple tabs, and device changes by implementing and verifying resource and rate limits.
Changes
1. Account Lockout for Failed Sign-in Attempts
New file:
src/auth/providers/account-lockout.service.tsTracks per-email failed sign-in attempts using a bounded in-memory store. After
maxAttemptsfailures (default: 5) within a configurable window (default: 15 min), the account is locked forlockoutMs(default: 15 min).New env vars:
AUTH_LOCKOUT_MAX_ATTEMPTS5AUTH_LOCKOUT_WINDOW_MS900000(15 min)AUTH_LOCKOUT_DURATION_MS900000(15 min)Integration:
SignInProviders.SignIn()now checks lockout before querying the database, records failures on wrong passwords, and resets the counter on success. Lockout returns HTTP 429 with a Retry-After guidance in the message.2. Rate Limiting on Logout/Logout-All Endpoints
File:
src/auth/auth.controller.tsAdded
@Throttledecorators:POST /auth/logout→ 10 requests/minutePOST /auth/logout-all→ 5 requests/minuteThese complement the existing limits on sign-in (5/15s), refresh-token (10/60s), verify-email (10/60s), and resend-verification (3/60s).
3. Input Size Bounds on Auth DTOs
Added
@MaxLengthand@MinLengthvalidators to all auth DTOs to reject oversized payloads before expensive operations:SignInDtoemailSignInDtopasswordRefreshTokenDtorefreshTokenLogoutDtorefreshTokenVerifyEmailDtotokenResendVerificationDtoemail4. Missing AuditAction Enum Values
Added auth-flow action values (
SIGN_IN,REFRESH,LOGOUT,LOGOUT_ALL,ISSUE_VERIFICATION_TOKEN,VERIFY_EMAIL,RESEND_VERIFICATION) to theAuditActionenum so the auth module compiles correctly.Security Invariants
lockoutMsthe counter resets automatically; no manual intervention required.class-validatorbefore any database query or hash computation.Tests
Added 21 regression tests (13 in
account-lockout.service.spec.ts, 4 new insign-in.providers.spec.ts) covering:All 21 tests pass. Pre-existing test failures in other spec files are unrelated to this change.
Validation
Compatibility
SignInProviders.Documentation
Full design documentation added at
docs/auth-resource-rate-limits.mdcovering invariants, failure behavior, compatibility impact, migration/rollback, operational limitations, and security assumptions.