@lenne.tech/nest-server 12.7.3 → 12.8.0
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 +4 -0
- package/.claude/rules/configurable-features.md +3 -0
- package/.claude/rules/testing.md +5 -5
- package/FRAMEWORK-API.md +2 -2
- package/dist/core/common/interfaces/server-options.interface.d.ts +2 -0
- package/dist/core/common/middlewares/string-cookies.middleware.d.ts +12 -0
- package/dist/core/common/middlewares/string-cookies.middleware.js +50 -0
- package/dist/core/common/middlewares/string-cookies.middleware.js.map +1 -0
- package/dist/core/common/pipes/map-and-validate.pipe.d.ts +2 -0
- package/dist/core/common/pipes/map-and-validate.pipe.js +15 -0
- package/dist/core/common/pipes/map-and-validate.pipe.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-email-verification.service.d.ts +6 -1
- package/dist/core/modules/better-auth/core-better-auth-email-verification.service.js +19 -4
- package/dist/core/modules/better-auth/core-better-auth-email-verification.service.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/core.module.js +2 -0
- package/dist/core.module.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/server/modules/error-code/error-codes.d.ts +1 -0
- package/docs/REQUEST-LIFECYCLE.md +27 -0
- package/migration-guides/12.7.x-to-12.8.x.md +152 -0
- package/package.json +2 -2
- package/src/core/common/interfaces/server-options.interface.ts +45 -4
- package/src/core/common/middlewares/string-cookies.middleware.ts +57 -0
- package/src/core/common/pipes/map-and-validate.pipe.ts +40 -0
- package/src/core/modules/better-auth/core-better-auth-email-verification.service.ts +44 -9
- package/src/core/modules/error-code/error-codes.ts +13 -0
- package/src/core.module.ts +5 -0
- package/src/index.ts +1 -0
|
@@ -695,15 +695,49 @@ function processNonWhitelistedFields(
|
|
|
695
695
|
return forbiddenKeys;
|
|
696
696
|
}
|
|
697
697
|
|
|
698
|
+
/** The declared parameter types a request value must not arrive as an object for */
|
|
699
|
+
const PRIMITIVE_METATYPES: readonly unknown[] = [String, Number, Boolean];
|
|
700
|
+
|
|
701
|
+
/**
|
|
702
|
+
* Whether a request value is an object where the route declares a string, number or boolean (DEV-4144).
|
|
703
|
+
*
|
|
704
|
+
* `@Body('token') token: string` has `metatype === String`, and the pipe used to return a basic-type value unchecked —
|
|
705
|
+
* the declared type is erased at runtime. `{ "token": { "$ne": null } }` therefore reached the service as an object,
|
|
706
|
+
* and a MongoDB filter read it as an operator (DEV-4143: a reset token that set somebody else's password). Only a
|
|
707
|
+
* PLAIN OBJECT is refused: an operator always is one. `null` passes (clearing an optional field is legitimate; the
|
|
708
|
+
* query guards handle it), and so does an array (a repeated query key, `?tag=a&tag=b`, arrives as one and selects
|
|
709
|
+
* nothing in a filter). A parameter typed as a class, as `any` or as a union the compiler emits as `Object` is not
|
|
710
|
+
* covered — its value is validated by its DTO or by the guard at the query. (`tsc` with `strictNullChecks` emits
|
|
711
|
+
* `string | undefined` as `Object`; SWC and `tsc` without it emit `String`, and the parameter is covered.) A number
|
|
712
|
+
* at a `string` parameter passes on purpose — a client may send a numeric code as a number — so a lookup on MySQL /
|
|
713
|
+
* MariaDB, which compare a number numerically, still checks its value with `isQueryableString()`.
|
|
714
|
+
*
|
|
715
|
+
* Exported for tests and for project pipes that replace this one.
|
|
716
|
+
*/
|
|
717
|
+
export function receivesObjectForPrimitive(value: unknown, metadata: ArgumentMetadata): boolean {
|
|
718
|
+
return (
|
|
719
|
+
(metadata.type === 'body' || metadata.type === 'query' || metadata.type === 'param') &&
|
|
720
|
+
PRIMITIVE_METATYPES.includes(metadata.metatype) &&
|
|
721
|
+
value !== null &&
|
|
722
|
+
typeof value === 'object' &&
|
|
723
|
+
!Array.isArray(value)
|
|
724
|
+
);
|
|
725
|
+
}
|
|
726
|
+
|
|
698
727
|
@Injectable()
|
|
699
728
|
export class MapAndValidatePipe implements PipeTransform {
|
|
700
729
|
private readonly nonWhitelistedFieldsMode: 'strip' | 'error' | false = 'strip';
|
|
730
|
+
private readonly rejectObjectsForPrimitiveTypes: boolean = true;
|
|
701
731
|
|
|
702
732
|
constructor() {
|
|
703
733
|
const pipeConfig = ConfigService.getFastButReadOnly('security.mapAndValidatePipe');
|
|
704
734
|
if (typeof pipeConfig === 'object' && pipeConfig?.nonWhitelistedFields !== undefined) {
|
|
705
735
|
this.nonWhitelistedFieldsMode = pipeConfig.nonWhitelistedFields;
|
|
706
736
|
}
|
|
737
|
+
// Only an explicit `false` switches the check off — a security default must not fall to a typo or a string
|
|
738
|
+
if (typeof pipeConfig === 'object' && pipeConfig?.rejectObjectsForPrimitiveTypes === false) {
|
|
739
|
+
this.rejectObjectsForPrimitiveTypes = false;
|
|
740
|
+
}
|
|
707
741
|
}
|
|
708
742
|
|
|
709
743
|
async transform(value: any, metadata: ArgumentMetadata) {
|
|
@@ -713,6 +747,12 @@ export class MapAndValidatePipe implements PipeTransform {
|
|
|
713
747
|
return value;
|
|
714
748
|
}
|
|
715
749
|
|
|
750
|
+
// An object where the route declares a string, number or boolean is the shape of a MongoDB operator
|
|
751
|
+
// ({ "$ne": null }) and never a value the route can use — refused before anything reaches a query (DEV-4144)
|
|
752
|
+
if (this.rejectObjectsForPrimitiveTypes && receivesObjectForPrimitive(value, metadata)) {
|
|
753
|
+
throw new BadRequestException(`${ErrorCode.INVALID_PARAMETER_TYPE} [${metadata.data ?? metadata.type}]`);
|
|
754
|
+
}
|
|
755
|
+
|
|
716
756
|
const { metatype } = metadata;
|
|
717
757
|
|
|
718
758
|
if (DEBUG_VALIDATION) {
|
|
@@ -233,10 +233,8 @@ export class CoreBetterAuthEmailVerificationService {
|
|
|
233
233
|
let sent = false;
|
|
234
234
|
|
|
235
235
|
try {
|
|
236
|
-
//
|
|
237
|
-
|
|
238
|
-
url = this.buildFrontendVerificationUrl(token);
|
|
239
|
-
}
|
|
236
|
+
// The app's verification page when callbackURL is configured, Better-Auth's own link otherwise
|
|
237
|
+
url = this.verificationMailUrl({ token, url });
|
|
240
238
|
|
|
241
239
|
this.logAuthUrlForDevelopment('EMAIL VERIFICATION', user.email, url);
|
|
242
240
|
|
|
@@ -731,20 +729,57 @@ export class CoreBetterAuthEmailVerificationService {
|
|
|
731
729
|
}
|
|
732
730
|
}
|
|
733
731
|
|
|
732
|
+
/**
|
|
733
|
+
* The link the verification mail carries: the app's verification page when `callbackURL` is configured and resolves
|
|
734
|
+
* ({@link buildFrontendVerificationUrl}), otherwise the link Better-Auth built (`<baseUrl>/<basePath>/verify-email`,
|
|
735
|
+
* token sealed), which verifies on the API and redirects from there.
|
|
736
|
+
*
|
|
737
|
+
* A relative `callbackURL` with no resolvable app URL keeps Better-Auth's link and warns once — a guessed host
|
|
738
|
+
* would 404 (DEV-4140).
|
|
739
|
+
*/
|
|
740
|
+
protected verificationMailUrl(options: { token: string; url: string }): string {
|
|
741
|
+
if (!this.config.callbackURL) {
|
|
742
|
+
return options.url;
|
|
743
|
+
}
|
|
744
|
+
const frontendUrl = this.buildFrontendVerificationUrl(options.token);
|
|
745
|
+
if (frontendUrl === undefined && !this.warnedAboutMissingAppUrl) {
|
|
746
|
+
this.warnedAboutMissingAppUrl = true;
|
|
747
|
+
this.logger.warn(
|
|
748
|
+
`No app URL resolves for the verification link (callbackURL "${this.config.callbackURL}" is relative, appUrl is ` +
|
|
749
|
+
'unset and baseUrl gives none): the mail links to the API instead of the app page. Set NSC__APP_URL, or ' +
|
|
750
|
+
'NSC__BASE_URL to derive it from.',
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
return frontendUrl ?? options.url;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/** Whether {@link verificationMailUrl} already warned that no app URL resolves (once per process) */
|
|
757
|
+
protected warnedAboutMissingAppUrl = false;
|
|
758
|
+
|
|
734
759
|
/**
|
|
735
760
|
* Build the frontend verification URL from the configured callbackURL and token.
|
|
736
761
|
*
|
|
737
|
-
* Resolves relative
|
|
762
|
+
* Resolves a relative `callbackURL` against the app URL the rest of the framework uses
|
|
763
|
+
* (`resolveAppUrlFromConfig()`: explicit `appUrl`, the localhost default in `local` / `ci` / `e2e`, else derived from
|
|
764
|
+
* `baseUrl` unless `cors.deriveAppUrl` is `false`). Appends the token as a query parameter.
|
|
765
|
+
*
|
|
766
|
+
* Until DEV-4140 a relative value was resolved against `appUrl || 'http://localhost:3001'`, so every deployed stage
|
|
767
|
+
* without `NSC__APP_URL` mailed a link to localhost — while the password-reset link next to it already derived the
|
|
768
|
+
* app from `baseUrl`.
|
|
738
769
|
*
|
|
739
770
|
* @param token - The verification token from Better-Auth
|
|
740
|
-
* @returns The full frontend URL with token query parameter
|
|
771
|
+
* @returns The full frontend URL with token query parameter, or `undefined` for a relative `callbackURL` when no app
|
|
772
|
+
* URL resolves (the caller then keeps Better-Auth's own link)
|
|
741
773
|
*/
|
|
742
|
-
protected buildFrontendVerificationUrl(token: string): string {
|
|
774
|
+
protected buildFrontendVerificationUrl(token: string): string | undefined {
|
|
743
775
|
let baseUrl = this.config.callbackURL!;
|
|
744
776
|
|
|
745
|
-
// Resolve relative paths against
|
|
777
|
+
// Resolve relative paths against the app URL — the SAME resolver as the password-reset link
|
|
746
778
|
if (baseUrl.startsWith('/')) {
|
|
747
|
-
const appUrl = this.configService
|
|
779
|
+
const appUrl = resolveAppUrlFromConfig(this.configService);
|
|
780
|
+
if (!appUrl) {
|
|
781
|
+
return undefined;
|
|
782
|
+
}
|
|
748
783
|
baseUrl = `${appUrl.replace(/\/$/, '')}${baseUrl}`;
|
|
749
784
|
}
|
|
750
785
|
|
|
@@ -503,6 +503,19 @@ export const LtnsErrors = {
|
|
|
503
503
|
},
|
|
504
504
|
},
|
|
505
505
|
|
|
506
|
+
/**
|
|
507
|
+
* An object arrived where the route declares a string, number or boolean (`@Body('token') token: string`) — the
|
|
508
|
+
* shape a MongoDB operator takes (`{ "$ne": null }`). Answered by `MapAndValidatePipe` (12.8.0, DEV-4144).
|
|
509
|
+
*/
|
|
510
|
+
INVALID_PARAMETER_TYPE: {
|
|
511
|
+
code: 'LTNS_0306',
|
|
512
|
+
message: 'Invalid parameter type',
|
|
513
|
+
translations: {
|
|
514
|
+
de: 'Ein Parameter hat einen ungültigen Typ.',
|
|
515
|
+
en: 'A parameter has an invalid type.',
|
|
516
|
+
},
|
|
517
|
+
},
|
|
518
|
+
|
|
506
519
|
// =====================================================
|
|
507
520
|
// Resource Errors (LTNS_0400-LTNS_0499)
|
|
508
521
|
// =====================================================
|
package/src/core.module.ts
CHANGED
|
@@ -38,6 +38,7 @@ import {
|
|
|
38
38
|
} from './core/common/interfaces/server-options.interface.js';
|
|
39
39
|
import { RequestContextMiddleware } from './core/common/middleware/request-context.middleware.js';
|
|
40
40
|
import { SecurityHeadersMiddleware } from './core/common/middlewares/security-headers.middleware.js';
|
|
41
|
+
import { StringCookiesMiddleware } from './core/common/middlewares/string-cookies.middleware.js';
|
|
41
42
|
import { CorePersistenceLifecycle } from './core/common/persistence/core-persistence.lifecycle.js';
|
|
42
43
|
import { CorePersistenceModule } from './core/common/persistence/core-persistence.module.js';
|
|
43
44
|
import {
|
|
@@ -115,6 +116,10 @@ export class CoreModule implements NestModule {
|
|
|
115
116
|
// RequestContext middleware must run for all routes to provide AsyncLocalStorage context
|
|
116
117
|
consumer.apply(RequestContextMiddleware).forRoutes('*');
|
|
117
118
|
|
|
119
|
+
// No non-string values in the parsed cookies (cookie-parser turns `j:{...}` into an object, the shape of a MongoDB
|
|
120
|
+
// operator — DEV-4138 / DEV-4144). Before every other middleware and every guard, so none of them sees one.
|
|
121
|
+
consumer.apply(StringCookiesMiddleware).forRoutes('*');
|
|
122
|
+
|
|
118
123
|
// Security headers on EVERY response, guards' rejections included. Middleware rather than an
|
|
119
124
|
// interceptor for exactly that reason: an interceptor never runs for a request a guard turns
|
|
120
125
|
// away, and those are the ones an attacker generates most of. The middleware itself decides
|
package/src/index.ts
CHANGED
|
@@ -84,6 +84,7 @@ export * from './core/common/interfaces/swagger-setup-options.interface.js';
|
|
|
84
84
|
export * from './core/common/middleware/request-context.middleware.js';
|
|
85
85
|
export * from './core/common/middlewares/to-lower-case.middleware.js';
|
|
86
86
|
export * from './core/common/middlewares/security-headers.middleware.js';
|
|
87
|
+
export * from './core/common/middlewares/string-cookies.middleware.js';
|
|
87
88
|
export * from './core/common/models/core-model.model.js';
|
|
88
89
|
export * from './core/common/models/core-persistence.model.js';
|
|
89
90
|
export * from './core/common/models/pagination-info.model.js';
|