@lenne.tech/nest-server 12.6.6 → 12.6.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/.claude/rules/better-auth.md +10 -0
  2. package/.claude/rules/configurable-features.md +1 -1
  3. package/.claude/rules/testing.md +3 -3
  4. package/FRAMEWORK-API.md +1 -1
  5. package/dist/core/common/filters/http-exception-log.filter.js +4 -3
  6. package/dist/core/common/filters/http-exception-log.filter.js.map +1 -1
  7. package/dist/core/modules/better-auth/better-auth.config.js +31 -5
  8. package/dist/core/modules/better-auth/better-auth.config.js.map +1 -1
  9. package/dist/core/modules/better-auth/core-better-auth-email-verification.service.d.ts +1 -0
  10. package/dist/core/modules/better-auth/core-better-auth-email-verification.service.js +5 -1
  11. package/dist/core/modules/better-auth/core-better-auth-email-verification.service.js.map +1 -1
  12. package/dist/core/modules/better-auth/core-better-auth-rate-limit.middleware.d.ts +2 -0
  13. package/dist/core/modules/better-auth/core-better-auth-rate-limit.middleware.js +12 -0
  14. package/dist/core/modules/better-auth/core-better-auth-rate-limit.middleware.js.map +1 -1
  15. package/dist/core/modules/better-auth/core-better-auth-rate-limiter.service.d.ts +2 -0
  16. package/dist/core/modules/better-auth/core-better-auth-rate-limiter.service.js +23 -1
  17. package/dist/core/modules/better-auth/core-better-auth-rate-limiter.service.js.map +1 -1
  18. package/dist/core/modules/user/core-user.service.js +5 -1
  19. package/dist/core/modules/user/core-user.service.js.map +1 -1
  20. package/migration-guides/11.41.7-to-11.41.8.md +1 -1
  21. package/migration-guides/12.2.x-to-12.3.x.md +2 -2
  22. package/migration-guides/12.6.6-to-12.6.7.md +125 -0
  23. package/migration-guides/12.6.7-to-12.6.8.md +69 -0
  24. package/package.json +5 -5
  25. package/src/core/common/filters/http-exception-log.filter.ts +7 -3
  26. package/src/core/common/interfaces/server-options.interface.ts +2 -1
  27. package/src/core/modules/better-auth/README.md +2 -2
  28. package/src/core/modules/better-auth/better-auth.config.ts +63 -5
  29. package/src/core/modules/better-auth/core-better-auth-email-verification.service.ts +18 -1
  30. package/src/core/modules/better-auth/core-better-auth-rate-limit.middleware.ts +23 -0
  31. package/src/core/modules/better-auth/core-better-auth-rate-limiter.service.ts +44 -3
  32. package/src/core/modules/user/core-user.service.ts +10 -2
@@ -288,7 +288,36 @@ export class CoreBetterAuthRateLimiter {
288
288
  }
289
289
 
290
290
  /**
291
- * Normalize endpoint path for consistent grouping
291
+ * The route names the base path serves: the last static segment of every route Better-Auth and the controller
292
+ * answer (`/sign-in/email` → `email`, `/two-factor/enable` → `enable`). Set by CoreBetterAuthRateLimitMiddleware from
293
+ * the running instance. A counter is keyed by one of these names, so a client cannot create counters of its own.
294
+ */
295
+ protected knownEndpoints: null | ReadonlySet<string> = null;
296
+
297
+ /**
298
+ * Record the routes the base path serves (paths relative to it, `:param` segments allowed). Only their last static
299
+ * segment counts.
300
+ */
301
+ setKnownEndpoints(paths: Iterable<unknown>): void {
302
+ const names = new Set<string>();
303
+ for (const path of paths) {
304
+ if (typeof path !== 'string') {
305
+ continue;
306
+ }
307
+ const last = path.split('?')[0].split('/').filter(Boolean).pop();
308
+ if (last && !last.startsWith(':')) {
309
+ names.add(last.toLowerCase());
310
+ }
311
+ }
312
+ this.knownEndpoints = names;
313
+ }
314
+
315
+ /**
316
+ * Normalize endpoint path for consistent grouping. The result comes from a bounded set — a known route name, a
317
+ * configured strict entry, `callback` or `other` — never from the request alone: keyed by the raw last segment, one
318
+ * client sending `/iam/x1`, `/iam/x2`, … created one counter per path, none of them ever reached the limit, and once
319
+ * the store held `maxEntries` counters every new client fell into the overflow buckets the attacker had filled, and
320
+ * was refused at its first sign-in.
292
321
  */
293
322
  private normalizeEndpoint(path: string): string {
294
323
  // Remove query strings
@@ -299,9 +328,21 @@ export class CoreBetterAuthRateLimiter {
299
328
  return 'callback';
300
329
  }
301
330
 
302
- // Extract the last segment as the endpoint identifier
331
+ // The last segment, when it names a route the base path serves
303
332
  const segments = cleanPath.split('/').filter(Boolean);
304
- return segments[segments.length - 1] || 'root';
333
+ const last = segments[segments.length - 1];
334
+ if (last && this.knownEndpoints?.has(last)) {
335
+ return last;
336
+ }
337
+
338
+ // A strict route under a name nobody serves (`/reset-password/<token>`, `/sign-in/<anything>`) keeps its own counter
339
+ const strict = this.config.strictEndpoints.find((entry) => pathContainsSegments(cleanPath, entry.toLowerCase()));
340
+ if (strict) {
341
+ return `strict:${strict.toLowerCase()}`;
342
+ }
343
+
344
+ // Everything else of one client shares one counter, as every IAM request did before the per-route split
345
+ return 'other';
305
346
  }
306
347
 
307
348
  /**
@@ -740,8 +740,16 @@ export abstract class CoreUserService<
740
740
  // Perform the update
741
741
  const updatedUser = await super.update(id, input, serviceOptions);
742
742
 
743
- // Sync email change to IAM if email was changed and mapper is available
744
- if (this.options?.betterAuthUserMapper && oldEmail && input.email && oldEmail !== input.email) {
743
+ // Sync email change to IAM if email was changed and mapper is available. Compared the way the address is stored
744
+ // (trimmed, lower case): `Foo@Example.com` for `foo@example.com` changes nothing, and the sync would end every
745
+ // session of the account for it.
746
+ const normalizeEmail = (value: unknown) => (typeof value === 'string' ? value.trim().toLowerCase() : value);
747
+ if (
748
+ this.options?.betterAuthUserMapper &&
749
+ oldEmail &&
750
+ input.email &&
751
+ normalizeEmail(oldEmail) !== normalizeEmail(input.email)
752
+ ) {
745
753
  try {
746
754
  await this.options.betterAuthUserMapper.syncEmailChangeFromLegacy(oldEmail, input.email);
747
755
  this.userServiceLogger.debug(`Synced email change from Legacy to IAM: ${oldEmail} → ${input.email}`);