@lenne.tech/nest-server 12.10.1 → 12.11.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 +28 -0
- package/.claude/rules/configurable-features.md +11 -9
- package/.claude/rules/package-management.md +1 -1
- package/.claude/rules/role-system.md +4 -1
- package/.claude/rules/testing.md +4 -4
- package/CLAUDE.md +6 -1
- package/FRAMEWORK-API.md +2 -1
- package/dist/core/common/decorators/unified-field.decorator.js +5 -2
- package/dist/core/common/decorators/unified-field.decorator.js.map +1 -1
- package/dist/core/common/helpers/config.helper.js +31 -9
- package/dist/core/common/helpers/config.helper.js.map +1 -1
- package/dist/core/common/helpers/graceful-shutdown.helper.js +3 -1
- package/dist/core/common/helpers/graceful-shutdown.helper.js.map +1 -1
- package/dist/core/common/helpers/graphql-ws-connection.helper.d.ts +8 -3
- package/dist/core/common/helpers/graphql-ws-connection.helper.js +44 -0
- package/dist/core/common/helpers/graphql-ws-connection.helper.js.map +1 -1
- package/dist/core/common/helpers/http-stream-auth.helper.d.ts +13 -0
- package/dist/core/common/helpers/http-stream-auth.helper.js +90 -0
- package/dist/core/common/helpers/http-stream-auth.helper.js.map +1 -0
- package/dist/core/common/helpers/shutdown-state.helper.d.ts +3 -0
- package/dist/core/common/helpers/shutdown-state.helper.js +11 -0
- package/dist/core/common/helpers/shutdown-state.helper.js.map +1 -0
- package/dist/core/common/interfaces/cron-job-config-with-utc-offset.interface.d.ts +1 -1
- package/dist/core/common/interfaces/cron-job-config.interface.d.ts +1 -1
- package/dist/core/common/interfaces/server-options.interface.d.ts +14 -0
- package/dist/core/common/services/core-cron-jobs.service.d.ts +13 -0
- package/dist/core/common/services/core-cron-jobs.service.js +129 -4
- package/dist/core/common/services/core-cron-jobs.service.js.map +1 -1
- package/dist/core/modules/better-auth/better-auth.config.js +48 -4
- 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 +7 -3
- package/dist/core/modules/better-auth/core-better-auth-api.middleware.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-jwt-session.cache.d.ts +8 -0
- package/dist/core/modules/better-auth/core-better-auth-jwt-session.cache.js +47 -0
- package/dist/core/modules/better-auth/core-better-auth-jwt-session.cache.js.map +1 -0
- package/dist/core/modules/better-auth/core-better-auth-models.d.ts +1 -0
- package/dist/core/modules/better-auth/core-better-auth-models.js +7 -0
- package/dist/core/modules/better-auth/core-better-auth-models.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-session-token.helper.d.ts +1 -0
- package/dist/core/modules/better-auth/core-better-auth-session-token.helper.js +3 -1
- package/dist/core/modules/better-auth/core-better-auth-session-token.helper.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-sign-in-factors.helper.d.ts +11 -0
- package/dist/core/modules/better-auth/core-better-auth-sign-in-factors.helper.js +62 -0
- package/dist/core/modules/better-auth/core-better-auth-sign-in-factors.helper.js.map +1 -0
- package/dist/core/modules/better-auth/core-better-auth-user.mapper.d.ts +1 -0
- package/dist/core/modules/better-auth/core-better-auth-user.mapper.js +28 -1
- 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 +3 -2
- package/dist/core/modules/better-auth/core-better-auth.controller.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.middleware.js +2 -2
- package/dist/core/modules/better-auth/core-better-auth.middleware.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.resolver.js +1 -0
- 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 +16 -2
- package/dist/core/modules/better-auth/core-better-auth.service.js +109 -22
- package/dist/core/modules/better-auth/core-better-auth.service.js.map +1 -1
- package/dist/core/modules/health-check/core-health-check-readiness.controller.d.ts +6 -0
- package/dist/core/modules/health-check/core-health-check-readiness.controller.js +36 -0
- package/dist/core/modules/health-check/core-health-check-readiness.controller.js.map +1 -0
- package/dist/core/modules/health-check/core-health-check.helpers.d.ts +10 -0
- package/dist/core/modules/health-check/core-health-check.helpers.js +31 -0
- package/dist/core/modules/health-check/core-health-check.helpers.js.map +1 -0
- package/dist/core/modules/health-check/core-health-check.module.d.ts +9 -0
- package/dist/core/modules/health-check/core-health-check.module.js +13 -2
- package/dist/core/modules/health-check/core-health-check.module.js.map +1 -1
- package/dist/core/modules/health-check/core-health-check.service.d.ts +8 -1
- package/dist/core/modules/health-check/core-health-check.service.js +66 -29
- package/dist/core/modules/health-check/core-health-check.service.js.map +1 -1
- package/dist/core/modules/sync/core-sync.service.d.ts +2 -0
- package/dist/core/modules/sync/core-sync.service.js +11 -1
- package/dist/core/modules/sync/core-sync.service.js.map +1 -1
- package/dist/core.module.js +5 -1
- package/dist/core.module.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/main.js.map +1 -1
- package/docs/.navigation.yml +4 -0
- package/docs/REQUEST-LIFECYCLE.md +8 -5
- package/docs/choosing-a-database.md +4 -1
- package/docs/error-codes.md +4 -1
- package/docs/index.md +34 -0
- package/docs/{brevo-manual-test.md → internal/brevo-manual-test.md} +1 -1
- package/docs/native-driver-security.md +4 -1
- package/docs/offline-sync-protocol.md +9 -4
- package/docs/process-performance-optimization.md +158 -126
- package/docs/security-overrides.md +5 -2
- package/migration-guides/12.10.x-to-12.11.x.md +297 -0
- package/migration-guides/12.11.0-to-12.11.1.md +86 -0
- package/package.json +20 -20
- package/src/core/common/decorators/unified-field.decorator.ts +14 -4
- package/src/core/common/helpers/config.helper.ts +55 -8
- package/src/core/common/helpers/graceful-shutdown.helper.ts +10 -3
- package/src/core/common/helpers/graphql-ws-connection.helper.ts +99 -4
- package/src/core/common/helpers/http-stream-auth.helper.ts +197 -0
- package/src/core/common/helpers/shutdown-state.helper.ts +28 -0
- package/src/core/common/interfaces/cron-job-config-with-utc-offset.interface.ts +1 -1
- package/src/core/common/interfaces/cron-job-config.interface.ts +1 -1
- package/src/core/common/interfaces/server-options.interface.ts +113 -1
- package/src/core/common/services/core-cron-jobs.service.ts +241 -14
- package/src/core/modules/better-auth/INTEGRATION-CHECKLIST.md +27 -10
- package/src/core/modules/better-auth/README.md +37 -8
- package/src/core/modules/better-auth/better-auth.config.ts +83 -12
- package/src/core/modules/better-auth/core-better-auth-api.middleware.ts +8 -4
- package/src/core/modules/better-auth/core-better-auth-jwt-session.cache.ts +104 -0
- package/src/core/modules/better-auth/core-better-auth-models.ts +5 -0
- package/src/core/modules/better-auth/core-better-auth-session-token.helper.ts +18 -1
- package/src/core/modules/better-auth/core-better-auth-sign-in-factors.helper.ts +105 -0
- package/src/core/modules/better-auth/core-better-auth-user.mapper.ts +37 -1
- package/src/core/modules/better-auth/core-better-auth.controller.ts +7 -4
- package/src/core/modules/better-auth/core-better-auth.middleware.ts +4 -3
- package/src/core/modules/better-auth/core-better-auth.module.ts +1 -1
- package/src/core/modules/better-auth/core-better-auth.resolver.ts +1 -0
- package/src/core/modules/better-auth/core-better-auth.service.ts +177 -31
- package/src/core/modules/health-check/core-health-check-readiness.controller.ts +27 -0
- package/src/core/modules/health-check/core-health-check.helpers.ts +65 -0
- package/src/core/modules/health-check/core-health-check.module.ts +37 -2
- package/src/core/modules/health-check/core-health-check.service.ts +154 -59
- package/src/core/modules/sync/README.md +18 -6
- package/src/core/modules/sync/core-sync.service.ts +35 -6
- package/src/core.module.ts +9 -1
- package/src/index.ts +4 -0
- package/src/main.ts +2 -1
|
@@ -1,22 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
---
|
|
2
|
+
title: "process() Performance Optimization"
|
|
3
|
+
description: "How the process() pipeline of CrudService avoids memory and CPU overhead on hot paths and service cascades, without weakening any security check."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
> **Status: implemented in 11.23.0** (see `migration-guides/11.22.x-to-11.23.0.md`). This page records the changes as
|
|
7
|
+
> they were made. The code excerpts and line numbers show the state at that time; the current implementation may
|
|
8
|
+
> differ in detail — the source files named below are authoritative.
|
|
2
9
|
|
|
3
|
-
##
|
|
10
|
+
## Context
|
|
4
11
|
|
|
5
|
-
|
|
12
|
+
The `process()` pipeline in `ModuleService` is the central processing path for all CRUD operations. With
|
|
13
|
+
high-frequency or nested calls (service cascades) it caused unnecessarily high memory and CPU usage. The optimizations
|
|
14
|
+
below reduce the overhead without weakening security.
|
|
6
15
|
|
|
7
|
-
**
|
|
16
|
+
**Basis of the analysis:** 8 customer projects and 13 lenne.tech projects were checked — all changes are backward
|
|
17
|
+
compatible.
|
|
8
18
|
|
|
9
19
|
---
|
|
10
20
|
|
|
11
|
-
##
|
|
21
|
+
## Change 1: Make the JSON.stringify debug output configurable
|
|
12
22
|
|
|
13
|
-
###
|
|
23
|
+
### File: `src/core/common/services/module.service.ts`
|
|
14
24
|
|
|
15
25
|
### Problem
|
|
16
26
|
|
|
17
|
-
|
|
27
|
+
Lines 149-164 serialized the input **twice** on every `process()` call (before and after) — only for a
|
|
28
|
+
`console.debug` comparison. The `new Promise(() => ...)` was fire-and-forget without `await`.
|
|
18
29
|
|
|
19
|
-
###
|
|
30
|
+
### Code before (lines 149-165)
|
|
20
31
|
|
|
21
32
|
```typescript
|
|
22
33
|
const originalInput = config.input;
|
|
@@ -38,12 +49,12 @@ new Promise(() => {
|
|
|
38
49
|
config.input = preparedInput;
|
|
39
50
|
```
|
|
40
51
|
|
|
41
|
-
###
|
|
52
|
+
### New code
|
|
42
53
|
|
|
43
54
|
```typescript
|
|
44
55
|
const preparedInput = await this.prepareInput(config.input, config);
|
|
45
56
|
|
|
46
|
-
// Debug
|
|
57
|
+
// Debug comparison only when explicitly configured (default: false)
|
|
47
58
|
if (this.configService?.getFastButReadOnly('debugProcessInput', false)) {
|
|
48
59
|
try {
|
|
49
60
|
const originalJSON = JSON.stringify(config.input)?.replace(/"password":\s*"[^"]*"/, '');
|
|
@@ -57,16 +68,16 @@ if (this.configService?.getFastButReadOnly('debugProcessInput', false)) {
|
|
|
57
68
|
);
|
|
58
69
|
}
|
|
59
70
|
} catch {
|
|
60
|
-
// JSON.stringify
|
|
71
|
+
// JSON.stringify can fail on circular references — ignore
|
|
61
72
|
}
|
|
62
73
|
}
|
|
63
74
|
|
|
64
75
|
config.input = preparedInput;
|
|
65
76
|
```
|
|
66
77
|
|
|
67
|
-
###
|
|
78
|
+
### File: `src/core/common/interfaces/server-options.interface.ts`
|
|
68
79
|
|
|
69
|
-
|
|
80
|
+
Added to the `IServerOptions` interface:
|
|
70
81
|
|
|
71
82
|
```typescript
|
|
72
83
|
/**
|
|
@@ -76,27 +87,30 @@ In das `IServerOptions`-Interface (oder dem passenden Config-Bereich) hinzufuege
|
|
|
76
87
|
debugProcessInput?: boolean;
|
|
77
88
|
```
|
|
78
89
|
|
|
79
|
-
###
|
|
90
|
+
### Security
|
|
80
91
|
|
|
81
|
-
|
|
92
|
+
Not affected — purely diagnostic.
|
|
82
93
|
|
|
83
|
-
###
|
|
94
|
+
### Compatibility
|
|
84
95
|
|
|
85
|
-
Default `false` —
|
|
96
|
+
Default `false` — no existing project changes its behavior.
|
|
86
97
|
|
|
87
98
|
---
|
|
88
99
|
|
|
89
|
-
##
|
|
100
|
+
## Change 2: Replace this.get(dbObject) with a lean query
|
|
90
101
|
|
|
91
|
-
###
|
|
102
|
+
### File: `src/core/common/services/module.service.ts`
|
|
92
103
|
|
|
93
104
|
### Problem
|
|
94
105
|
|
|
95
|
-
|
|
106
|
+
Lines 168-176 called `this.get()`, which runs the whole `process()` pipeline **recursively** — including
|
|
107
|
+
`prepareInput`, `checkRights`, `processFieldSelection` and `prepareOutput`. That is unnecessary, because the
|
|
108
|
+
`dbObject` is only needed as context for the rights check.
|
|
96
109
|
|
|
97
|
-
|
|
110
|
+
It was even **counterproductive**: `this.get()` without `force` can remove fields such as `createdBy` (because of
|
|
111
|
+
`@Restricted(RoleEnum.ADMIN)`), which are then missing for the `S_CREATOR` check.
|
|
98
112
|
|
|
99
|
-
###
|
|
113
|
+
### Code before (lines 168-176)
|
|
100
114
|
|
|
101
115
|
```typescript
|
|
102
116
|
// Get DB object
|
|
@@ -110,7 +124,7 @@ if (config.dbObject && config.checkRights && this.checkRights) {
|
|
|
110
124
|
}
|
|
111
125
|
```
|
|
112
126
|
|
|
113
|
-
###
|
|
127
|
+
### New code
|
|
114
128
|
|
|
115
129
|
```typescript
|
|
116
130
|
// Get DB object for rights checking — lean query to avoid recursive process() call.
|
|
@@ -132,38 +146,45 @@ if (config.dbObject && config.checkRights && this.checkRights) {
|
|
|
132
146
|
}
|
|
133
147
|
```
|
|
134
148
|
|
|
135
|
-
###
|
|
149
|
+
### Security
|
|
136
150
|
|
|
137
|
-
**
|
|
151
|
+
**Improved** — the lean query keeps all fields (e.g. `createdBy`), which makes the `S_CREATOR` check more reliable
|
|
152
|
+
than the previous code.
|
|
138
153
|
|
|
139
|
-
###
|
|
154
|
+
### Downstream steps checked
|
|
140
155
|
|
|
141
|
-
- `checkRights(input)` (
|
|
142
|
-
|
|
143
|
-
- `
|
|
144
|
-
- `
|
|
156
|
+
- `checkRights(input)` (lines 179-184): uses `config.dbObject` for the `S_CREATOR` / `S_SELF` / `memberOf` checks via
|
|
157
|
+
`equalIds()` — works with lean + map, because `equalIds` compares ObjectIds as well as strings.
|
|
158
|
+
- `checkRights(output)` (lines 239-250): same usage — works.
|
|
159
|
+
- `validateRestricted()` in `checkRestricted()` (`restricted.decorator.ts`, lines 172-174): checks
|
|
160
|
+
`'createdBy' in data && equalIds(data.createdBy, user)` — the lean object always has `createdBy` (not removed by the
|
|
161
|
+
pipeline).
|
|
162
|
+
- `memberOf` check (lines 196-218): reads `config.dbObject?.[property]` — the lean object has all properties.
|
|
145
163
|
|
|
146
|
-
###
|
|
164
|
+
### Compatibility
|
|
147
165
|
|
|
148
|
-
- `CrudService.update()` (
|
|
149
|
-
-
|
|
150
|
-
- Services
|
|
166
|
+
- `CrudService.update()` (line 552) already sets `dbObject` as a lean object (`findById().lean()`) → consistent.
|
|
167
|
+
- No project passes a Mongoose document as `dbObject` → no conflict.
|
|
168
|
+
- Services that call `process()` directly with `dbObject` as a string (for example a project's own offer service)
|
|
169
|
+
benefit from the optimization.
|
|
151
170
|
|
|
152
171
|
---
|
|
153
172
|
|
|
154
|
-
##
|
|
173
|
+
## Change 3: Depth tracking in RequestContext
|
|
155
174
|
|
|
156
|
-
###
|
|
175
|
+
### File: `src/core/common/services/request-context.service.ts`
|
|
157
176
|
|
|
158
177
|
### Problem
|
|
159
178
|
|
|
160
|
-
|
|
179
|
+
In service cascades (A.create → B.create → C.create) the full `process()` pipeline ran on every level. Populate,
|
|
180
|
+
`prepareOutput` mapping and the output `checkRights` on inner levels are unnecessary, because the outer level and the
|
|
181
|
+
`CheckSecurityInterceptor` take care of them.
|
|
161
182
|
|
|
162
|
-
###
|
|
183
|
+
### Change to the interface
|
|
163
184
|
|
|
164
185
|
```typescript
|
|
165
186
|
export interface IRequestContext {
|
|
166
|
-
// ...
|
|
187
|
+
// ... existing fields ...
|
|
167
188
|
|
|
168
189
|
/**
|
|
169
190
|
* Tracks the nesting depth of process() calls.
|
|
@@ -175,7 +196,7 @@ export interface IRequestContext {
|
|
|
175
196
|
}
|
|
176
197
|
```
|
|
177
198
|
|
|
178
|
-
###
|
|
199
|
+
### New methods
|
|
179
200
|
|
|
180
201
|
```typescript
|
|
181
202
|
/**
|
|
@@ -201,19 +222,19 @@ static runWithIncrementedProcessDepth<T>(fn: () => T): T {
|
|
|
201
222
|
}
|
|
202
223
|
```
|
|
203
224
|
|
|
204
|
-
###
|
|
225
|
+
### Security
|
|
205
226
|
|
|
206
|
-
|
|
227
|
+
Not affected — depth tracking is purely informational and changes no permissions.
|
|
207
228
|
|
|
208
229
|
---
|
|
209
230
|
|
|
210
|
-
##
|
|
231
|
+
## Change 4: Depth-based optimization in process()
|
|
211
232
|
|
|
212
|
-
###
|
|
233
|
+
### File: `src/core/common/services/module.service.ts`
|
|
213
234
|
|
|
214
|
-
###
|
|
235
|
+
### Change in process() (line 81 ff.)
|
|
215
236
|
|
|
216
|
-
|
|
237
|
+
At the start of the method, after the config is created (after line 108):
|
|
217
238
|
|
|
218
239
|
```typescript
|
|
219
240
|
// Detect nested process() calls
|
|
@@ -221,7 +242,7 @@ const currentDepth = RequestContext.getProcessDepth();
|
|
|
221
242
|
const isNested = currentDepth > 0;
|
|
222
243
|
```
|
|
223
244
|
|
|
224
|
-
### serviceFunc
|
|
245
|
+
### Run serviceFunc with depth tracking (replaces lines 201-205)
|
|
225
246
|
|
|
226
247
|
```typescript
|
|
227
248
|
// Run service function with incremented depth
|
|
@@ -234,7 +255,7 @@ let result = config.force
|
|
|
234
255
|
: await executeServiceFunc();
|
|
235
256
|
```
|
|
236
257
|
|
|
237
|
-
### processFieldSelection
|
|
258
|
+
### Skip processFieldSelection on inner calls (replaces lines 207-217)
|
|
238
259
|
|
|
239
260
|
```typescript
|
|
240
261
|
// Pop and map main model
|
|
@@ -254,12 +275,15 @@ if (config.processFieldSelection && config.fieldSelection && this.processFieldSe
|
|
|
254
275
|
}
|
|
255
276
|
```
|
|
256
277
|
|
|
257
|
-
**
|
|
258
|
-
- `isNested = false` (Depth 0, aeusserster Call): Populate laeuft immer → User bekommt vollstaendiges Ergebnis.
|
|
259
|
-
- `isNested = true` (Depth > 0, innerer Call) OHNE `config.populate`: Populate wird uebersprungen → Ergebnis wird intern weiterverarbeitet.
|
|
260
|
-
- `isNested = true` MIT `config.populate` (explizit vom Caller gesetzt): Populate laeuft → der innere Service hat das explizit angefordert weil er die Daten braucht.
|
|
278
|
+
**Logic:**
|
|
261
279
|
|
|
262
|
-
|
|
280
|
+
- `isNested = false` (depth 0, outermost call): populate always runs → the user gets the complete result.
|
|
281
|
+
- `isNested = true` (depth > 0, inner call) WITHOUT `config.populate`: populate is skipped → the result is processed
|
|
282
|
+
further internally.
|
|
283
|
+
- `isNested = true` WITH `config.populate` (set explicitly by the caller): populate runs → the inner service asked for
|
|
284
|
+
it explicitly because it needs the data.
|
|
285
|
+
|
|
286
|
+
### Reduce prepareOutput on inner calls (replaces lines 219-236)
|
|
263
287
|
|
|
264
288
|
```typescript
|
|
265
289
|
// Prepare output
|
|
@@ -290,7 +314,7 @@ if (config.prepareOutput && this.prepareOutput) {
|
|
|
290
314
|
}
|
|
291
315
|
```
|
|
292
316
|
|
|
293
|
-
###
|
|
317
|
+
### Skip the output checkRights on inner calls (replaces lines 238-250)
|
|
294
318
|
|
|
295
319
|
```typescript
|
|
296
320
|
// Check output rights
|
|
@@ -310,38 +334,44 @@ if (!isNested && config.checkRights && (await this.checkRights(undefined, config
|
|
|
310
334
|
}
|
|
311
335
|
```
|
|
312
336
|
|
|
313
|
-
###
|
|
337
|
+
### Security
|
|
338
|
+
|
|
339
|
+
**Ensured by three layers:**
|
|
340
|
+
|
|
341
|
+
1. **The input checkRights ALWAYS runs** (also when `isNested`) — unauthorized input is caught on every level.
|
|
342
|
+
2. **The output checkRights runs at depth 0** — the outermost call filters the final result.
|
|
343
|
+
3. **CheckSecurityInterceptor** — the last safety net in the HTTP response pipeline calls `securityCheck()`
|
|
344
|
+
recursively on the whole response tree, including every nested object.
|
|
314
345
|
|
|
315
|
-
**
|
|
346
|
+
**Counter-check:** What happens when an inner service returns an object the user may not see?
|
|
316
347
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
348
|
+
- The inner `process()` skips the output rights check → the object stays unchanged.
|
|
349
|
+
- The outer `process()` runs the output rights check → the fields are removed.
|
|
350
|
+
- If the outer `process()` misses it as well → `CheckSecurityInterceptor` removes the fields.
|
|
320
351
|
|
|
321
|
-
|
|
322
|
-
- Der innere process() ueberspringt den Output-Rights-Check → Objekt bleibt unveraendert
|
|
323
|
-
- Der aeussere process() fuehrt den Output-Rights-Check durch → Felder werden entfernt
|
|
324
|
-
- Falls der aeussere process() es auch verpasst → CheckSecurityInterceptor entfernt die Felder
|
|
352
|
+
### Compatibility
|
|
325
353
|
|
|
326
|
-
|
|
354
|
+
Checked against real cascades:
|
|
327
355
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
- **
|
|
331
|
-
- **
|
|
332
|
-
- **Services
|
|
356
|
+
- **A five-level create cascade** (a company with its profile sites, categories, entries and forms): the inner creates
|
|
357
|
+
need no populate — their results are only used for their ids.
|
|
358
|
+
- **A participation service calling `UserService.create()`**: the result is used inline, not returned to the user.
|
|
359
|
+
- **A shipping service calling bounce and e-mail services**: no populate needed on inner levels.
|
|
360
|
+
- **Services with an explicit `populate:`** keep working, because `config.populate` overrides the `isNested` skip.
|
|
333
361
|
|
|
334
362
|
---
|
|
335
363
|
|
|
336
|
-
##
|
|
364
|
+
## Change 5: Caching of the checkRestricted metadata
|
|
337
365
|
|
|
338
|
-
###
|
|
366
|
+
### File: `src/core/common/decorators/restricted.decorator.ts`
|
|
339
367
|
|
|
340
368
|
### Problem
|
|
341
369
|
|
|
342
|
-
`getRestricted()` (
|
|
370
|
+
`getRestricted()` (lines 50-58) called `Reflect.getMetadata()` for every property check. For an object with 15
|
|
371
|
+
properties and nested objects, this adds up to hundreds of Reflect lookups per request. The metadata never changes at
|
|
372
|
+
runtime (decorators are static).
|
|
343
373
|
|
|
344
|
-
###
|
|
374
|
+
### Code before (lines 50-58)
|
|
345
375
|
|
|
346
376
|
```typescript
|
|
347
377
|
export const getRestricted = (object: unknown, propertyKey?: string): RestrictedType => {
|
|
@@ -355,7 +385,7 @@ export const getRestricted = (object: unknown, propertyKey?: string): Restricted
|
|
|
355
385
|
};
|
|
356
386
|
```
|
|
357
387
|
|
|
358
|
-
###
|
|
388
|
+
### New code
|
|
359
389
|
|
|
360
390
|
```typescript
|
|
361
391
|
// Cache for Restricted metadata — decorators are static, metadata never changes at runtime.
|
|
@@ -404,90 +434,92 @@ export const getRestricted = (object: unknown, propertyKey?: string): Restricted
|
|
|
404
434
|
};
|
|
405
435
|
```
|
|
406
436
|
|
|
407
|
-
###
|
|
437
|
+
### Additionally: _.uniq() optimization in checkRestricted (line 260)
|
|
408
438
|
|
|
409
439
|
```typescript
|
|
410
|
-
//
|
|
440
|
+
// Before:
|
|
411
441
|
const concatenatedRestrictions = config.mergeRoles ? _.uniq(objectRestrictions.concat(restricted)) : restricted;
|
|
412
442
|
|
|
413
|
-
//
|
|
443
|
+
// Optimized — avoids an array allocation when objectRestrictions is empty:
|
|
414
444
|
const concatenatedRestrictions = config.mergeRoles && objectRestrictions.length
|
|
415
445
|
? _.uniq(objectRestrictions.concat(restricted))
|
|
416
446
|
: restricted;
|
|
417
447
|
```
|
|
418
448
|
|
|
419
|
-
|
|
449
|
+
This line runs for **every property of every object**. When `objectRestrictions` is empty (common for models without
|
|
450
|
+
a class-level `@Restricted`), it saves one array allocation and one `uniq` computation per property.
|
|
420
451
|
|
|
421
|
-
###
|
|
452
|
+
### Security
|
|
422
453
|
|
|
423
|
-
**
|
|
454
|
+
**Not affected** — same results, because decorator metadata does not change at runtime. The cache returns exactly the
|
|
455
|
+
values of the direct Reflect lookup.
|
|
424
456
|
|
|
425
|
-
###
|
|
457
|
+
### Compatibility
|
|
426
458
|
|
|
427
|
-
Transparent —
|
|
459
|
+
Transparent — no project needs to be adapted.
|
|
428
460
|
|
|
429
461
|
---
|
|
430
462
|
|
|
431
|
-
##
|
|
463
|
+
## Summary of the savings
|
|
432
464
|
|
|
433
|
-
###
|
|
465
|
+
### Single API call (depth 0, no nesting)
|
|
434
466
|
|
|
435
|
-
|
|
|
436
|
-
|
|
437
|
-
| JSON.stringify
|
|
438
|
-
| Lean dbObject
|
|
439
|
-
| Metadata
|
|
467
|
+
| Change | Saving |
|
|
468
|
+
|--------|--------|
|
|
469
|
+
| No JSON.stringify | ~0.1-1 KB + 2 serializations |
|
|
470
|
+
| Lean dbObject instead of this.get() | ~5-15 KB + 1 recursive process() run |
|
|
471
|
+
| Metadata cache | CPU: hundreds of Reflect lookups fewer |
|
|
440
472
|
|
|
441
|
-
###
|
|
473
|
+
### Nested call (depth > 0)
|
|
442
474
|
|
|
443
|
-
|
|
|
444
|
-
|
|
445
|
-
| JSON.stringify |
|
|
446
|
-
| prepareInput |
|
|
447
|
-
| this.get(dbObject) |
|
|
448
|
-
| checkRights(input) |
|
|
449
|
-
| serviceFunc |
|
|
450
|
-
| processFieldSelection |
|
|
451
|
-
| prepareOutput |
|
|
452
|
-
| checkRights(output) |
|
|
475
|
+
| Step | Before | After |
|
|
476
|
+
|------|--------|-------|
|
|
477
|
+
| JSON.stringify | 2 serializations | Skipped |
|
|
478
|
+
| prepareInput | Runs | Runs (unchanged) |
|
|
479
|
+
| this.get(dbObject) | Recursive process() | Lean query + map |
|
|
480
|
+
| checkRights(input) | Runs | Runs (unchanged) |
|
|
481
|
+
| serviceFunc | Runs | Runs (unchanged) |
|
|
482
|
+
| processFieldSelection | Full population | Skipped (unless populate is explicit) |
|
|
483
|
+
| prepareOutput | Full mapping | Secret removal only |
|
|
484
|
+
| checkRights(output) | Runs | Skipped |
|
|
453
485
|
|
|
454
|
-
**
|
|
486
|
+
**Per nested call: ~30-70 KB and 2-5 database queries fewer.**
|
|
455
487
|
|
|
456
|
-
###
|
|
488
|
+
### Example: an 8-level service cascade (e.g. incident handling)
|
|
457
489
|
|
|
458
|
-
-
|
|
459
|
-
-
|
|
460
|
-
- **~70%
|
|
490
|
+
- Before: 8 × ~50 KB = ~400 KB
|
|
491
|
+
- After: 1 × ~50 KB (outer) + 7 × ~10 KB (inner) = ~120 KB
|
|
492
|
+
- **~70% less memory**
|
|
461
493
|
|
|
462
494
|
---
|
|
463
495
|
|
|
464
|
-
##
|
|
496
|
+
## Affected files
|
|
465
497
|
|
|
466
|
-
|
|
|
467
|
-
|
|
468
|
-
| `src/core/common/services/module.service.ts` |
|
|
469
|
-
| `src/core/common/services/request-context.service.ts` |
|
|
470
|
-
| `src/core/common/decorators/restricted.decorator.ts` |
|
|
471
|
-
| `src/core/common/interfaces/server-options.interface.ts` | Config
|
|
498
|
+
| File | Change |
|
|
499
|
+
|------|--------|
|
|
500
|
+
| `src/core/common/services/module.service.ts` | Changes 1, 2, 4 |
|
|
501
|
+
| `src/core/common/services/request-context.service.ts` | Change 3 |
|
|
502
|
+
| `src/core/common/decorators/restricted.decorator.ts` | Change 5 |
|
|
503
|
+
| `src/core/common/interfaces/server-options.interface.ts` | Config option `debugProcessInput` |
|
|
472
504
|
|
|
473
505
|
---
|
|
474
506
|
|
|
475
|
-
##
|
|
507
|
+
## Verification
|
|
476
508
|
|
|
477
|
-
1. **
|
|
478
|
-
2. **
|
|
479
|
-
3. **Populate
|
|
480
|
-
4. **securityCheck()**:
|
|
481
|
-
5. **S_CREATOR
|
|
482
|
-
6. **Performance**: `process.memoryUsage()`
|
|
509
|
+
1. **Existing E2E tests** must all stay green — the changes are backward compatible.
|
|
510
|
+
2. **Test a service cascade**: e.g. a `create()` that calls other services internally.
|
|
511
|
+
3. **Populate on the outermost level**: `{ populate: ['user', 'customer'] }` must keep working.
|
|
512
|
+
4. **securityCheck()**: sensitive fields (password etc.) must never appear in the response.
|
|
513
|
+
5. **S_CREATOR check**: the user who created an object must still be able to edit it.
|
|
514
|
+
6. **Performance**: measure `process.memoryUsage()` before and after the optimization for nested calls.
|
|
483
515
|
|
|
484
516
|
---
|
|
485
517
|
|
|
486
|
-
##
|
|
518
|
+
## Risks
|
|
487
519
|
|
|
488
|
-
|
|
|
489
|
-
|
|
490
|
-
|
|
|
491
|
-
|
|
|
492
|
-
|
|
|
493
|
-
|
|
|
520
|
+
| Risk | Safeguard |
|
|
521
|
+
|------|-----------|
|
|
522
|
+
| An inner service needs the populate result | `config.populate` overrides the `isNested` skip — an explicit request is respected |
|
|
523
|
+
| The output rights check on the inner result is missing | `CheckSecurityInterceptor` is the safety net on the HTTP level |
|
|
524
|
+
| The metadata cache becomes stale | Impossible — decorator metadata is fixed when the classes are defined |
|
|
525
|
+
| The lean `dbObject` has a different structure than a hydrated one | `modelConstructor.map()` restores the expected structure |
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
---
|
|
2
|
+
title: "Security Overrides — and why yours are not inherited"
|
|
3
|
+
description: "Why the pnpm overrides of nest-server do not reach your project, and how to keep the dependency tree of your project free of known vulnerabilities."
|
|
4
|
+
---
|
|
2
5
|
|
|
3
6
|
## The problem in one sentence
|
|
4
7
|
|
|
@@ -227,6 +230,6 @@ These are the same rules the framework applies to itself
|
|
|
227
230
|
|
|
228
231
|
## Related
|
|
229
232
|
|
|
230
|
-
- [`.claude/rules/package-management.md`](
|
|
233
|
+
- [`.claude/rules/package-management.md`](https://github.com/lenneTech/nest-server/blob/main/.claude/rules/package-management.md) — fixed-version
|
|
231
234
|
policy, the pnpm pin contract, and the incident behind rule 1
|
|
232
235
|
- `pnpm-workspace.yaml` in this package — the framework's own annotated override block
|