Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

refactor(auth): keep the per-address reset throttle in a module of its own

Maurice1553be3d5ba9
diff --git a/server/scripts/size-baseline.json b/server/scripts/size-baseline.json
index f41522dc7..5277b10a0 100644
--- a/server/scripts/size-baseline.json
+++ b/server/scripts/size-baseline.json
@@ -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,
diff --git a/server/src/nest/auth/auth.service.ts b/server/src/nest/auth/auth.service.ts
index f45a0aafc..310e7326c 100644
--- a/server/src/nest/auth/auth.service.ts
+++ b/server/src/nest/auth/auth.service.ts
@@ -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' };
diff --git a/server/src/nest/auth/password-reset-throttle.ts b/server/src/nest/auth/password-reset-throttle.ts
new file mode 100644
index 000000000..5b746d7a2
--- /dev/null
+++ b/server/src/nest/auth/password-reset-throttle.ts
@@ -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;
+}