@lenne.tech/nest-server 11.45.0 → 11.46.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.
Files changed (47) hide show
  1. package/.claude/rules/configurable-features.md +2 -1
  2. package/.claude/rules/testing.md +4 -4
  3. package/FRAMEWORK-API.md +2 -2
  4. package/dist/core/common/filters/http-exception-log.filter.js +4 -3
  5. package/dist/core/common/filters/http-exception-log.filter.js.map +1 -1
  6. package/dist/core/common/helpers/swagger.helper.js +6 -0
  7. package/dist/core/common/helpers/swagger.helper.js.map +1 -1
  8. package/dist/core/common/interfaces/server-options.interface.d.ts +5 -0
  9. package/dist/core/modules/better-auth/better-auth.config.js +1 -1
  10. package/dist/core/modules/better-auth/better-auth.config.js.map +1 -1
  11. package/dist/core/modules/better-auth/core-better-auth-email-verification.service.js +2 -2
  12. package/dist/core/modules/better-auth/core-better-auth-email-verification.service.js.map +1 -1
  13. package/dist/core/modules/error-code/error-codes.d.ts +18 -0
  14. package/dist/core/modules/error-code/error-codes.js +16 -0
  15. package/dist/core/modules/error-code/error-codes.js.map +1 -1
  16. package/dist/core/modules/system-setup/core-system-setup.controller.d.ts +2 -2
  17. package/dist/core/modules/system-setup/core-system-setup.controller.js +14 -4
  18. package/dist/core/modules/system-setup/core-system-setup.controller.js.map +1 -1
  19. package/dist/core/modules/system-setup/core-system-setup.service.d.ts +18 -0
  20. package/dist/core/modules/system-setup/core-system-setup.service.js +118 -15
  21. package/dist/core/modules/system-setup/core-system-setup.service.js.map +1 -1
  22. package/dist/core/modules/system-setup/system-setup-http-init.helper.d.ts +12 -0
  23. package/dist/core/modules/system-setup/system-setup-http-init.helper.js +60 -0
  24. package/dist/core/modules/system-setup/system-setup-http-init.helper.js.map +1 -0
  25. package/dist/index.d.ts +1 -0
  26. package/dist/index.js +1 -0
  27. package/dist/index.js.map +1 -1
  28. package/dist/server/modules/error-code/error-codes.d.ts +2 -0
  29. package/dist/tsconfig.build.tsbuildinfo +1 -1
  30. package/migration-guides/11.45.x-to-11.46.x.md +219 -0
  31. package/migration-guides/11.46.0-to-11.46.1.md +68 -0
  32. package/package.json +3 -3
  33. package/src/core/common/filters/http-exception-log.filter.ts +7 -3
  34. package/src/core/common/helpers/swagger.helper.ts +10 -0
  35. package/src/core/common/interfaces/server-options.interface.ts +60 -3
  36. package/src/core/modules/better-auth/CUSTOMIZATION.md +1 -1
  37. package/src/core/modules/better-auth/INTEGRATION-CHECKLIST.md +4 -0
  38. package/src/core/modules/better-auth/README.md +7 -0
  39. package/src/core/modules/better-auth/better-auth.config.ts +3 -1
  40. package/src/core/modules/better-auth/core-better-auth-email-verification.service.ts +5 -2
  41. package/src/core/modules/error-code/error-codes.ts +27 -0
  42. package/src/core/modules/system-setup/INTEGRATION-CHECKLIST.md +23 -1
  43. package/src/core/modules/system-setup/README.md +54 -12
  44. package/src/core/modules/system-setup/core-system-setup.controller.ts +30 -8
  45. package/src/core/modules/system-setup/core-system-setup.service.ts +251 -23
  46. package/src/core/modules/system-setup/system-setup-http-init.helper.ts +165 -0
  47. package/src/index.ts +1 -0
@@ -0,0 +1,165 @@
1
+ import { createHash, timingSafeEqual } from 'node:crypto';
2
+
3
+ import { isLocalEnv } from '../../common/helpers/cookies.helper';
4
+
5
+ /**
6
+ * The request header `POST /system-setup/init` reads the setup token from (`systemSetup.httpInit.token`).
7
+ *
8
+ * A header, never a body field: the body is the admin's credentials and is shaped by the DTO, while the token is a
9
+ * deployment secret the operator hands out separately. Header names are case-insensitive; Express lowercases them.
10
+ *
11
+ * @since 11.46.0
12
+ */
13
+ export const SYSTEM_SETUP_TOKEN_HEADER = 'x-system-setup-token';
14
+
15
+ /**
16
+ * Minimum length of a configured setup token.
17
+ *
18
+ * The init route is anonymous and not rate-limited, so the token is all that stands between a caller and an
19
+ * administrator account. A shorter value is refused as a misconfiguration (the HTTP init stays closed), never
20
+ * accepted as a weak gate.
21
+ *
22
+ * @since 11.46.0
23
+ */
24
+ export const SYSTEM_SETUP_TOKEN_MIN_LENGTH = 16;
25
+
26
+ /**
27
+ * Whether `POST /system-setup/init` may run, as decided from `systemSetup.httpInit` and the environment.
28
+ *
29
+ * @since 11.46.0
30
+ */
31
+ export interface ISystemSetupHttpInitPolicy {
32
+ /** The HTTP init is open (with or without a token) */
33
+ enabled: boolean;
34
+
35
+ /**
36
+ * What is wrong with the configuration when it is unusable — a token that is missing, empty, too short or not a
37
+ * string. Set only when the configuration asked for a token-protected init and could not get one; the init then
38
+ * stays CLOSED. Meant for the boot log.
39
+ */
40
+ problem?: string;
41
+
42
+ /** Why the decision came out this way: the environment default, or an explicit setting */
43
+ source: 'config' | 'default';
44
+
45
+ /** The configured token, when the init is open and token-protected */
46
+ token?: string;
47
+
48
+ /** A token must be sent in {@link SYSTEM_SETUP_TOKEN_HEADER} */
49
+ tokenRequired: boolean;
50
+ }
51
+
52
+ /**
53
+ * Decide whether the anonymous `POST /system-setup/init` is open, from `systemSetup.httpInit` and the environment.
54
+ *
55
+ * Fail-closed in every branch that is not an explicit, well-formed opt-in:
56
+ *
57
+ * | `httpInit` | `local` / `ci` / `e2e` | every other environment |
58
+ * |---------------------------------------|------------------------|-------------------------|
59
+ * | not set (or not a boolean / object) | open, no token | **closed** |
60
+ * | `true` | open, no token | open, no token |
61
+ * | `false` / `{ enabled: false, … }` | closed | closed |
62
+ * | `{ token: '<≥ 16 characters>' }` | open, token required | open, token required |
63
+ * | an object without a usable token | closed + `problem` | closed + `problem` |
64
+ *
65
+ * The environment test is {@link isLocalEnv}: the deployed `develop` and `test` stages of a starter-based project,
66
+ * `production`, `staging` and an unknown or missing name all count as deployed.
67
+ *
68
+ * Why an object without a usable token CLOSES rather than opening without one: the natural way to write the option is
69
+ * `httpInit: { token: process.env.SETUP_TOKEN }`, and with the variable missing that is `{ token: undefined }`. Reading
70
+ * it as "open, no token" would turn a forgotten secret into an anonymous administrator endpoint — the incident this
71
+ * exists for. A value from `NSC__SYSTEM_SETUP__HTTP_INIT__TOKEN` that is empty or all digits arrives as a NUMBER
72
+ * (`getEnvironmentObject()` converts numeric strings, `''` included, and a long digit string loses digits on the way),
73
+ * so a non-string token is refused too. And a non-boolean `httpInit` (the string `'true'` from `NEST_SERVER_CONFIG`)
74
+ * reads as "not set", so a string cannot open the route by truthiness.
75
+ *
76
+ * @since 11.46.0
77
+ */
78
+ export function resolveSystemSetupHttpInit(httpInit: unknown, env: string | undefined): ISystemSetupHttpInitPolicy {
79
+ if (httpInit === true) {
80
+ return { enabled: true, source: 'config', tokenRequired: false };
81
+ }
82
+ if (httpInit === false) {
83
+ return { enabled: false, source: 'config', tokenRequired: false };
84
+ }
85
+ if (httpInit && typeof httpInit === 'object' && !Array.isArray(httpInit)) {
86
+ const { enabled, token } = httpInit as { enabled?: unknown; token?: unknown };
87
+ if (enabled === false) {
88
+ return { enabled: false, source: 'config', tokenRequired: false };
89
+ }
90
+ const problem = tokenProblem(token);
91
+ if (problem) {
92
+ return { enabled: false, problem, source: 'config', tokenRequired: false };
93
+ }
94
+ return { enabled: true, source: 'config', token: token as string, tokenRequired: true };
95
+ }
96
+ return { enabled: isLocalEnv(env), source: 'default', tokenRequired: false };
97
+ }
98
+
99
+ /**
100
+ * The environment the HTTP-init policy is decided for: the configured `env`, unless it is local only because 11.x fell
101
+ * back to the `local` block.
102
+ *
103
+ * On 11.x `getEnvironmentConfig()` loads `config[NODE_ENV] || config.local`: a stage whose `NODE_ENV` names no block
104
+ * (`staging`, `prod`) or is empty (`NODE_ENV=${NODE_ENV}` in a compose file with the variable unset) runs on the
105
+ * `local` block, and its `env` reads `local` — which would keep the anonymous init open on a deployed instance. So a
106
+ * local configured `env` counts only when `NODE_ENV` is unset or local too. A test runner's `NODE_ENV=test` (vitest and
107
+ * jest set it when nothing else does) stays local while `VITEST` / `JEST_WORKER_ID` is set, as 12.7.0 does it.
108
+ *
109
+ * | configured `env` | `NODE_ENV` | decided for |
110
+ * |------------------|------------------------------------|-----------------------------|
111
+ * | not local | any | the configured `env` |
112
+ * | not set | any | `NODE_ENV` |
113
+ * | local | unset, or local | the configured `env` |
114
+ * | local | `test` under vitest / jest | the configured `env` |
115
+ * | local | anything else, `''` included | `NODE_ENV` (deployed) |
116
+ *
117
+ * @param configuredEnv the `env` of the loaded configuration
118
+ * @param nodeEnv `process.env.NODE_ENV` by default
119
+ * @since 11.46.0
120
+ */
121
+ export function resolveSystemSetupEnv(
122
+ configuredEnv: string | undefined,
123
+ nodeEnv: string | undefined = process.env.NODE_ENV,
124
+ ): string | undefined {
125
+ if (nodeEnv === undefined || !isLocalEnv(configuredEnv)) {
126
+ return configuredEnv ?? nodeEnv;
127
+ }
128
+ if (isLocalEnv(nodeEnv) || (nodeEnv === 'test' && (process.env.VITEST || process.env.JEST_WORKER_ID))) {
129
+ return configuredEnv;
130
+ }
131
+ return nodeEnv;
132
+ }
133
+
134
+ /**
135
+ * Whether `provided` (the header value) matches the configured `expected` token.
136
+ *
137
+ * Both sides are hashed to the same length before `timingSafeEqual`, so neither the content nor the LENGTH of the
138
+ * configured token leaks through the response time. A missing, empty or non-string value never matches.
139
+ *
140
+ * @since 11.46.0
141
+ */
142
+ export function isSystemSetupTokenValid(expected: string | undefined, provided: unknown): boolean {
143
+ if (typeof expected !== 'string' || !expected || typeof provided !== 'string' || !provided) {
144
+ return false;
145
+ }
146
+ const digest = (value: string) => createHash('sha256').update(value, 'utf8').digest();
147
+ return timingSafeEqual(digest(expected), digest(provided));
148
+ }
149
+
150
+ /** Why a configured token cannot be used, or `undefined` when it can */
151
+ function tokenProblem(token: unknown): string | undefined {
152
+ if (token === undefined || token === null) {
153
+ return 'systemSetup.httpInit is an object without a token';
154
+ }
155
+ if (typeof token !== 'string') {
156
+ return (
157
+ `systemSetup.httpInit.token is a ${typeof token}, not a string — an empty or all-digit ` +
158
+ 'NSC__SYSTEM_SETUP__HTTP_INIT__TOKEN is converted to a number; use a token with letters'
159
+ );
160
+ }
161
+ if (token.trim().length < SYSTEM_SETUP_TOKEN_MIN_LENGTH) {
162
+ return `systemSetup.httpInit.token is shorter than ${SYSTEM_SETUP_TOKEN_MIN_LENGTH} characters`;
163
+ }
164
+ return undefined;
165
+ }
package/src/index.ts CHANGED
@@ -250,6 +250,7 @@ export * from './core/modules/permissions/permissions-scanner';
250
250
  export * from './core/modules/system-setup/core-system-setup.controller';
251
251
  export * from './core/modules/system-setup/core-system-setup.module';
252
252
  export * from './core/modules/system-setup/core-system-setup.service';
253
+ export * from './core/modules/system-setup/system-setup-http-init.helper';
253
254
 
254
255
  // =====================================================================================================================
255
256
  // Core - Modules - Tus