mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 07:30:04 +00:00
refactor(auth): keep the per-address reset throttle in a module of its own
The reset link check grew the auth service past its size baseline.
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
"src/db/repositories/Trips.repository.ts": 1120,
|
||||
"src/db/repositories/Users.repository.ts": 1421,
|
||||
"src/nest/atlas/atlas.service.ts": 1166,
|
||||
"src/nest/auth/auth.service.ts": 1132,
|
||||
"src/nest/auth/auth.service.ts": 1126,
|
||||
"src/nest/budget/budget.service.ts": 1607,
|
||||
"src/nest/collections/collections.service.ts": 1414,
|
||||
"src/nest/doc-sync/doc-sync.service.ts": 1060,
|
||||
|
||||
@@ -53,6 +53,7 @@ import { DEMO_EMAIL_PRIMARY, DEMO_PASS, isDemoEmail } from '../common/demo';
|
||||
import { avatarUrl } from '../common/avatarUrl';
|
||||
import { TripMembershipService } from '../trip-membership/trip-membership.service';
|
||||
import { WebauthnConfigService } from './webauthn-config.service';
|
||||
import { resetThrottledPerEmail } from './password-reset-throttle';
|
||||
import { setAuthCookie, clearAuthCookie } from '../common/cookie';
|
||||
import { MailerService } from '../notifications/mailer/mailer.service';
|
||||
import { AllowedFileTypesService } from '../files/allowed-file-types.service';
|
||||
@@ -119,19 +120,6 @@ export interface PasswordResetRequestOutcome {
|
||||
reason: 'issued' | 'no_user' | 'oidc_only' | 'throttled_per_email' | 'password_login_disabled';
|
||||
}
|
||||
|
||||
// Per-email throttle (defence-in-depth on top of the per-IP limiter).
|
||||
// Module-scoped + import-time interval on purpose (legacy parity, shared
|
||||
// between bridge and container instances — atlas-geo interval precedent).
|
||||
const perEmailResetAttempts = new Map<string, { count: number; first: number }>();
|
||||
const PASSWORD_RESET_PER_EMAIL_WINDOW_MS = 15 * 60 * 1000;
|
||||
const PASSWORD_RESET_PER_EMAIL_MAX = 3;
|
||||
setInterval(() => {
|
||||
const now = Date.now();
|
||||
for (const [key, record] of perEmailResetAttempts) {
|
||||
if (now - record.first >= PASSWORD_RESET_PER_EMAIL_WINDOW_MS) perEmailResetAttempts.delete(key);
|
||||
}
|
||||
}, 5 * 60 * 1000).unref?.();
|
||||
|
||||
export interface ResetPasswordOutcome {
|
||||
status?: number;
|
||||
success?: boolean;
|
||||
@@ -951,17 +939,9 @@ export class AuthService {
|
||||
|
||||
// Per-email throttle. We check this BEFORE the DB lookup so the timing
|
||||
// is identical regardless of whether the account exists.
|
||||
const throttleKey = email || '__noemail__';
|
||||
const now = Date.now();
|
||||
const record = perEmailResetAttempts.get(throttleKey);
|
||||
if (record && record.count >= PASSWORD_RESET_PER_EMAIL_MAX && now - record.first < PASSWORD_RESET_PER_EMAIL_WINDOW_MS) {
|
||||
if (resetThrottledPerEmail(email || '__noemail__', Date.now())) {
|
||||
return { tokenForDelivery: null, userId: null, userEmail: null, reason: 'throttled_per_email' };
|
||||
}
|
||||
if (!record || now - record.first >= PASSWORD_RESET_PER_EMAIL_WINDOW_MS) {
|
||||
perEmailResetAttempts.set(throttleKey, { count: 1, first: now });
|
||||
} else {
|
||||
record.count++;
|
||||
}
|
||||
|
||||
if (!looksLikeEmail) {
|
||||
return { tokenForDelivery: null, userId: null, userEmail: null, reason: 'no_user' };
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
// Per-email throttle (defence-in-depth on top of the per-IP limiter).
|
||||
// Module-scoped + import-time interval on purpose (legacy parity, shared
|
||||
// between bridge and container instances — atlas-geo interval precedent).
|
||||
const perEmailResetAttempts = new Map<string, { count: number; first: number }>();
|
||||
const PASSWORD_RESET_PER_EMAIL_WINDOW_MS = 15 * 60 * 1000;
|
||||
const PASSWORD_RESET_PER_EMAIL_MAX = 3;
|
||||
setInterval(
|
||||
() => {
|
||||
const now = Date.now();
|
||||
for (const [key, record] of perEmailResetAttempts) {
|
||||
if (now - record.first >= PASSWORD_RESET_PER_EMAIL_WINDOW_MS) perEmailResetAttempts.delete(key);
|
||||
}
|
||||
},
|
||||
5 * 60 * 1000,
|
||||
).unref?.();
|
||||
|
||||
/** Count one reset request for `key`; true when the address already asked too often in this window. */
|
||||
export function resetThrottledPerEmail(key: string, now: number): boolean {
|
||||
const record = perEmailResetAttempts.get(key);
|
||||
if (
|
||||
record &&
|
||||
record.count >= PASSWORD_RESET_PER_EMAIL_MAX &&
|
||||
now - record.first < PASSWORD_RESET_PER_EMAIL_WINDOW_MS
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
if (!record || now - record.first >= PASSWORD_RESET_PER_EMAIL_WINDOW_MS) {
|
||||
perEmailResetAttempts.set(key, { count: 1, first: now });
|
||||
} else {
|
||||
record.count++;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user