Skip to content

feat(auth): enforce resource and rate limits for auth and recovery flows (#1651) - #1695

Merged
Baskarayelu merged 1 commit into
Remitwise-Org:mainfrom
diahtech1:feat/auth-resource-rate-limits
Aug 30, 2026
Merged

feat(auth): enforce resource and rate limits for auth and recovery flows (#1651)#1695
Baskarayelu merged 1 commit into
Remitwise-Org:mainfrom
diahtech1:feat/auth-resource-rate-limits

Conversation

@diahtech1

Copy link
Copy Markdown
Contributor

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.ts

Tracks per-email failed sign-in attempts using a bounded in-memory store. After maxAttempts failures (default: 5) within a configurable window (default: 15 min), the account is locked for lockoutMs (default: 15 min).

New env vars:

Variable Default Description
AUTH_LOCKOUT_MAX_ATTEMPTS 5 Consecutive failures before lockout
AUTH_LOCKOUT_WINDOW_MS 900000 (15 min) Window for counting failures
AUTH_LOCKOUT_DURATION_MS 900000 (15 min) How long the account is locked

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.ts

Added @Throttle decorators:

  • POST /auth/logout → 10 requests/minute
  • POST /auth/logout-all → 5 requests/minute

These 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 @MaxLength and @MinLength validators to all auth DTOs to reject oversized payloads before expensive operations:

DTO Field Max Length Rationale
SignInDto email 254 RFC 5321 §4.5.3.1
SignInDto password 72 bcrypt truncation limit
RefreshTokenDto refreshToken 4096 JWT with RSA-4096 + claims
LogoutDto refreshToken 4096 Same as above
VerifyEmailDto token 256 64-char hex + safety margin
ResendVerificationDto email 254 RFC 5321 §4.5.3.1

4. Missing AuditAction Enum Values

Added auth-flow action values (SIGN_IN, REFRESH, LOGOUT, LOGOUT_ALL, ISSUE_VERIFICATION_TOKEN, VERIFY_EMAIL, RESEND_VERIFICATION) to the AuditAction enum so the auth module compiles correctly.

Security Invariants

  1. Stale tokens rejected — expired or revoked tokens never advance to an authoritative state.
  2. Account lockout is time-bounded — after lockoutMs the counter resets automatically; no manual intervention required.
  3. Lockout counter resets on success — a successful sign-in immediately clears the failure counter.
  4. Partial state on failure — failed sign-in attempts with lockout leave no unauthorized or partial state.
  5. Bounded store — the lockout store is capped at 10,000 entries with LRU-style eviction to prevent OOM under sustained attack.
  6. Input validation before work — oversized payloads are rejected by class-validator before any database query or hash computation.
  7. Rate limits cover all mutating endpoints — sign-in, refresh, logout, logout-all, verify-email, and resend-verification are all throttled.
  8. Rejected operations leave no state — idempotency keys are deleted on failure, lockout entries expire, and rate limit windows slide.

Tests

Added 21 regression tests (13 in account-lockout.service.spec.ts, 4 new in sign-in.providers.spec.ts) covering:

  • Account lockout after max failures
  • Lockout check before DB query
  • Failure recording and 429 response
  • Success resets lockout counter
  • Email normalization (case-insensitive, whitespace trim)
  • Bounded store eviction
  • Independent per-email tracking
  • Configurable thresholds

All 21 tests pass. Pre-existing test failures in other spec files are unrelated to this change.

Validation

  • ✅ 21/21 new/modified tests pass
  • ✅ 5/5 rate-limit tests pass
  • ✅ No new type errors (pre-existing errors in unrelated files)
  • ✅ No secrets, disabled checks, or unrelated refactors
  • ✅ All new env vars have safe defaults — zero migration needed

Compatibility

  • No breaking changes — all existing successful-path responses are unchanged.
  • New error codes — HTTP 429 (lockout/throttle) and tighter HTTP 400 (input validation).
  • No database migration — lockout state is in-memory.
  • Rollback — revert the branch; lockout service is additive and only injected into SignInProviders.

Documentation

Full design documentation added at docs/auth-resource-rate-limits.md covering invariants, failure behavior, compatibility impact, migration/rollback, operational limitations, and security assumptions.

…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
@Baskarayelu
Baskarayelu merged commit d2c5545 into Remitwise-Org:main Aug 30, 2026
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.

[Quality][Medium] authentication and account recovery: resource and rate limits — QE-2026-08

2 participants