@lenne.tech/nest-server 11.44.0 → 11.44.1
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.
- package/.claude/rules/better-auth.md +40 -4
- package/.claude/rules/configurable-features.md +1 -1
- package/.claude/rules/testing.md +4 -4
- package/FRAMEWORK-API.md +1 -1
- package/dist/config.env.js +2 -2
- package/dist/config.env.js.map +1 -1
- package/dist/core/common/interfaces/server-options.interface.d.ts +1 -0
- package/dist/core/modules/auth/services/core-auth.service.js +3 -0
- package/dist/core/modules/auth/services/core-auth.service.js.map +1 -1
- package/dist/core/modules/better-auth/better-auth.config.js +2 -0
- package/dist/core/modules/better-auth/better-auth.config.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-api.middleware.js +2 -1
- package/dist/core/modules/better-auth/core-better-auth-api.middleware.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-path-guard.middleware.d.ts +8 -0
- package/dist/core/modules/better-auth/core-better-auth-path-guard.middleware.js +70 -0
- package/dist/core/modules/better-auth/core-better-auth-path-guard.middleware.js.map +1 -0
- package/dist/core/modules/better-auth/core-better-auth-rate-limit.middleware.js +2 -1
- package/dist/core/modules/better-auth/core-better-auth-rate-limit.middleware.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-rate-limiter.service.d.ts +1 -1
- package/dist/core/modules/better-auth/core-better-auth-rate-limiter.service.js +5 -3
- package/dist/core/modules/better-auth/core-better-auth-rate-limiter.service.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-user.mapper.d.ts +2 -0
- package/dist/core/modules/better-auth/core-better-auth-user.mapper.js +25 -0
- package/dist/core/modules/better-auth/core-better-auth-user.mapper.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.controller.js +13 -5
- package/dist/core/modules/better-auth/core-better-auth.controller.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.module.js +2 -0
- package/dist/core/modules/better-auth/core-better-auth.module.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.resolver.js +7 -2
- package/dist/core/modules/better-auth/core-better-auth.resolver.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.service.d.ts +1 -0
- package/dist/core/modules/better-auth/core-better-auth.service.js +14 -0
- package/dist/core/modules/better-auth/core-better-auth.service.js.map +1 -1
- package/dist/core/modules/better-auth/index.d.ts +1 -0
- package/dist/core/modules/better-auth/index.js +1 -0
- package/dist/core/modules/better-auth/index.js.map +1 -1
- package/dist/core/modules/error-code/error-codes.d.ts +9 -0
- package/dist/core/modules/error-code/error-codes.js +8 -0
- package/dist/core/modules/error-code/error-codes.js.map +1 -1
- package/dist/server/modules/error-code/error-codes.d.ts +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/docs/REQUEST-LIFECYCLE.md +1 -0
- package/docs/error-codes.md +14 -0
- package/migration-guides/11.44.0-to-11.44.1.md +152 -0
- package/package.json +1 -1
- package/src/config.env.ts +6 -2
- package/src/core/common/interfaces/server-options.interface.ts +15 -0
- package/src/core/modules/auth/services/core-auth.service.ts +8 -0
- package/src/core/modules/better-auth/INTEGRATION-CHECKLIST.md +3 -0
- package/src/core/modules/better-auth/README.md +34 -4
- package/src/core/modules/better-auth/better-auth.config.ts +9 -0
- package/src/core/modules/better-auth/core-better-auth-api.middleware.ts +8 -3
- package/src/core/modules/better-auth/core-better-auth-path-guard.middleware.ts +85 -0
- package/src/core/modules/better-auth/core-better-auth-rate-limit.middleware.ts +5 -2
- package/src/core/modules/better-auth/core-better-auth-rate-limiter.service.ts +14 -7
- package/src/core/modules/better-auth/core-better-auth-user.mapper.ts +41 -0
- package/src/core/modules/better-auth/core-better-auth.controller.ts +24 -10
- package/src/core/modules/better-auth/core-better-auth.module.ts +12 -2
- package/src/core/modules/better-auth/core-better-auth.resolver.ts +12 -2
- package/src/core/modules/better-auth/core-better-auth.service.ts +28 -0
- package/src/core/modules/better-auth/index.ts +1 -0
- package/src/core/modules/error-code/error-codes.ts +9 -0
|
@@ -584,17 +584,31 @@ export class CoreBetterAuthController {
|
|
|
584
584
|
}
|
|
585
585
|
|
|
586
586
|
if (hasUser(response)) {
|
|
587
|
-
//
|
|
588
|
-
//
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
587
|
+
// An already registered address: with `requireEmailVerification` or `autoSignIn: false` Better-Auth answers it
|
|
588
|
+
// like a new sign-up, with a placeholder user it never stored (enumeration protection). Linking that user
|
|
589
|
+
// re-pointed the real account found by address and overwrote its legacy password with the caller's (11.44.1).
|
|
590
|
+
// The answer below stays that of a new sign-up; only the writes are left out.
|
|
591
|
+
const isPlaceholder = !(await this.betterAuthService.isPersistedUser(response.user.id));
|
|
592
|
+
|
|
593
|
+
if (isPlaceholder) {
|
|
594
|
+
// The same bcrypt round as the legacy sync, so the response time does not tell the address exists
|
|
595
|
+
await this.userMapper.equalizeLegacyPasswordSync(input.password);
|
|
596
|
+
} else {
|
|
597
|
+
// Link or create user in our database
|
|
598
|
+
// Pass termsAndPrivacyAccepted to store the acceptance timestamp
|
|
599
|
+
await this.userMapper.linkOrCreateUser(response.user, {
|
|
600
|
+
termsAndPrivacyAccepted: input.termsAndPrivacyAccepted,
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
// Sync password to legacy (enables IAM Sign-Up → Legacy Sign-In)
|
|
604
|
+
// Pass the plain password so it can be hashed with bcrypt for Legacy Auth
|
|
605
|
+
await this.userMapper.syncPasswordToLegacy(response.user.id, response.user.email, input.password);
|
|
606
|
+
}
|
|
596
607
|
|
|
597
|
-
|
|
608
|
+
// A new user has no roles yet; the placeholder must not carry those of the account it stands for
|
|
609
|
+
const mappedUser = isPlaceholder
|
|
610
|
+
? this.userMapper.mapUnstoredSessionUser(response.user)
|
|
611
|
+
: await this.userMapper.mapSessionUser(response.user);
|
|
598
612
|
|
|
599
613
|
// Get token: JWT accessToken > top-level token > session.token
|
|
600
614
|
// Without this, no session cookies are set after sign-up, causing 401 on
|
|
@@ -32,6 +32,7 @@ import { DefaultBetterAuthResolver } from './better-auth.resolver';
|
|
|
32
32
|
import { CoreBetterAuthApiMiddleware } from './core-better-auth-api.middleware';
|
|
33
33
|
import { CoreBetterAuthChallengeService } from './core-better-auth-challenge.service';
|
|
34
34
|
import { CoreBetterAuthEmailVerificationService } from './core-better-auth-email-verification.service';
|
|
35
|
+
import { CoreBetterAuthPathGuardMiddleware } from './core-better-auth-path-guard.middleware';
|
|
35
36
|
import { CoreBetterAuthRateLimitMiddleware } from './core-better-auth-rate-limit.middleware';
|
|
36
37
|
import { CoreBetterAuthRateLimiter } from './core-better-auth-rate-limiter.service';
|
|
37
38
|
import { revokeApiTokensOfUser } from '../api-token/core-api-token.registry';
|
|
@@ -422,8 +423,10 @@ export class CoreBetterAuthModule implements NestModule, OnModuleInit {
|
|
|
422
423
|
* 1. CoreBetterAuthMiddleware - Session validation and user mapping for all routes
|
|
423
424
|
* Must run FIRST so that req.betterAuthSession is available for downstream middleware.
|
|
424
425
|
* In JWT mode, this resolves the JWT to a real DB session (via getActiveSessionForUser).
|
|
425
|
-
* 2.
|
|
426
|
-
*
|
|
426
|
+
* 2. CoreBetterAuthPathGuardMiddleware - Refuses a non-canonical path under the base path (400), before
|
|
427
|
+
* anything below reads it
|
|
428
|
+
* 3. CoreBetterAuthRateLimitMiddleware - Rate limiting for auth endpoints
|
|
429
|
+
* 4. CoreBetterAuthApiMiddleware - Forwards plugin endpoints (passkey, 2FA, etc.) to Better Auth's native handler
|
|
427
430
|
* Runs AFTER session middleware so it can use req.betterAuthSession.session.token
|
|
428
431
|
* to authenticate requests in JWT mode.
|
|
429
432
|
*/
|
|
@@ -442,6 +445,13 @@ export class CoreBetterAuthModule implements NestModule, OnModuleInit {
|
|
|
442
445
|
consumer.apply(CoreBetterAuthMiddleware).forRoutes('(.*)'); // New path-to-regexp syntax for wildcard
|
|
443
446
|
CoreBetterAuthModule.logger.debug('CoreBetterAuthMiddleware registered for all routes');
|
|
444
447
|
|
|
448
|
+
// Refuse a non-canonical path (`.` / `..` segments, also encoded, backslashes, encoded slashes) before anything
|
|
449
|
+
// under the base path reads it: every check below reads the path as SENT, while Better-Auth routes the one
|
|
450
|
+
// `new URL()` resolved — `/callback/../sign-in/email` would pass the rate limiter as a callback and run as a
|
|
451
|
+
// sign-in. Registered unconditionally and FIRST, so it guards the limiter, the controller routes and the
|
|
452
|
+
// forwarding alike.
|
|
453
|
+
consumer.apply(CoreBetterAuthPathGuardMiddleware).forRoutes(`${basePath}/*path`);
|
|
454
|
+
|
|
445
455
|
// Apply rate limiting to Better-Auth endpoints only.
|
|
446
456
|
// Mirrors the "presence implies enabled" contract `CoreBetterAuthRateLimiter.configure()`
|
|
447
457
|
// implements. Gating the MOUNT on `.enabled` alone made that fix unobservable: the limiter
|
|
@@ -483,10 +483,20 @@ export class CoreBetterAuthResolver {
|
|
|
483
483
|
if (hasUser(response)) {
|
|
484
484
|
const sessionUser: BetterAuthSessionUser = response.user;
|
|
485
485
|
|
|
486
|
+
// An already registered address: with `requireEmailVerification` or `autoSignIn: false` Better-Auth answers it
|
|
487
|
+
// like a new sign-up, with a placeholder user it never stored (enumeration protection). Linking that user
|
|
488
|
+
// re-pointed the real account found by address, and the answer carried that account's id and roles (11.44.1).
|
|
489
|
+
// The answer stays that of a new sign-up; only the write is left out.
|
|
490
|
+
const isPlaceholder = !(await this.betterAuthService.isPersistedUser(sessionUser.id));
|
|
491
|
+
|
|
486
492
|
// Link or create user in our database
|
|
487
493
|
// Pass termsAndPrivacyAccepted to store the acceptance timestamp
|
|
488
|
-
|
|
489
|
-
|
|
494
|
+
if (!isPlaceholder) {
|
|
495
|
+
await this.userMapper.linkOrCreateUser(sessionUser, { termsAndPrivacyAccepted });
|
|
496
|
+
}
|
|
497
|
+
const mappedUser = isPlaceholder
|
|
498
|
+
? this.userMapper.mapUnstoredSessionUser(sessionUser)
|
|
499
|
+
: await this.userMapper.mapSessionUser(sessionUser);
|
|
490
500
|
|
|
491
501
|
// If email verification is enabled, revoke the session and don't return session data
|
|
492
502
|
// The user must verify their email before they can use any session
|
|
@@ -1068,6 +1068,34 @@ export class CoreBetterAuthService implements OnApplicationBootstrap, OnModuleIn
|
|
|
1068
1068
|
}
|
|
1069
1069
|
}
|
|
1070
1070
|
|
|
1071
|
+
/**
|
|
1072
|
+
* Whether Better-Auth has stored the user `userId` — false for the placeholder it answers a sign-up with when the
|
|
1073
|
+
* address is already registered (11.44.1).
|
|
1074
|
+
*
|
|
1075
|
+
* With `emailAndPassword.requireEmailVerification: true` or `autoSignIn: false`, Better-Auth answers a sign-up for a
|
|
1076
|
+
* known address like a new one (enumeration protection): `{ token: null, user: <a generated id, the given address> }`.
|
|
1077
|
+
* That user exists nowhere. The sign-up paths linked it to the app user found by address — the real account then
|
|
1078
|
+
* carried the placeholder's id and `verified: false`, its legacy password became the one the caller chose, and the
|
|
1079
|
+
* answer carried its roles. A sign-up therefore writes only for a user this confirms. Asked through Better-Auth's own
|
|
1080
|
+
* adapter (`internalAdapter.findUserById()`), so it reads wherever Better-Auth keeps its users.
|
|
1081
|
+
*
|
|
1082
|
+
* @returns `false` for an unknown id, an empty id, and a failed lookup — a sign-up that cannot confirm the user writes
|
|
1083
|
+
* nothing rather than guessing
|
|
1084
|
+
*/
|
|
1085
|
+
async isPersistedUser(userId: string): Promise<boolean> {
|
|
1086
|
+
if (typeof userId !== 'string' || !userId) {
|
|
1087
|
+
return false;
|
|
1088
|
+
}
|
|
1089
|
+
try {
|
|
1090
|
+
const context = await (this.getInstance() as { $context?: Promise<any> } | null)?.$context;
|
|
1091
|
+
const user: unknown = await context?.internalAdapter?.findUserById?.(userId);
|
|
1092
|
+
return !!user && String((user as { id?: unknown }).id) === userId;
|
|
1093
|
+
} catch (error) {
|
|
1094
|
+
this.logger.error(`isPersistedUser error: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
1095
|
+
return false;
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1071
1099
|
/**
|
|
1072
1100
|
* Revokes a specific session (logout for that session)
|
|
1073
1101
|
*
|
|
@@ -34,6 +34,7 @@ export * from './core-better-auth-email-verification.service';
|
|
|
34
34
|
export * from './core-better-auth-error-codes.helper';
|
|
35
35
|
export * from './core-better-auth-migration-status.model';
|
|
36
36
|
export * from './core-better-auth-models';
|
|
37
|
+
export * from './core-better-auth-path-guard.middleware';
|
|
37
38
|
export * from './core-better-auth-rate-limit.middleware';
|
|
38
39
|
export * from './core-better-auth-rate-limiter.service';
|
|
39
40
|
export * from './core-better-auth-signup-validator.service';
|
|
@@ -493,6 +493,15 @@ export const LtnsErrors = {
|
|
|
493
493
|
},
|
|
494
494
|
},
|
|
495
495
|
|
|
496
|
+
INVALID_REQUEST_PATH: {
|
|
497
|
+
code: 'LTNS_0305',
|
|
498
|
+
message: 'Invalid request path',
|
|
499
|
+
translations: {
|
|
500
|
+
de: 'Der Anfragepfad ist ungültig.',
|
|
501
|
+
en: 'The request path is invalid.',
|
|
502
|
+
},
|
|
503
|
+
},
|
|
504
|
+
|
|
496
505
|
/**
|
|
497
506
|
* An object arrived where the route declares a string, number or boolean (`@Body('token') token: string`) — the
|
|
498
507
|
* shape a MongoDB operator takes (`{ "$ne": null }`). Answered by `MapAndValidatePipe` (11.44.0 / 12.8.0, DEV-4144).
|