@nathapp/nestjs-common 2.1.5 → 3.0.0-rc.4
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.md +29 -0
- package/README.md +134 -0
- package/dist/config/index.d.ts +4 -4
- package/dist/config/index.js.map +1 -1
- package/dist/config/server-security.config.d.ts +1 -0
- package/dist/config/server-security.config.js +19 -2
- package/dist/config/server-security.config.js.map +1 -1
- package/dist/decorator/index.d.ts +2 -2
- package/dist/decorator/index.js.map +1 -1
- package/dist/entities/base-entity.d.ts +3 -0
- package/dist/entities/base-entity.js +8 -0
- package/dist/entities/base-entity.js.map +1 -0
- package/dist/entities/index.d.ts +1 -0
- package/dist/entities/index.js +18 -0
- package/dist/entities/index.js.map +1 -0
- package/dist/exception/app.exception.d.ts +4 -3
- package/dist/exception/app.exception.js +1 -1
- package/dist/exception/app.exception.js.map +1 -1
- package/dist/exception/default-i18n-validation-exception.filter.js +1 -1
- package/dist/exception/default-i18n-validation-exception.filter.js.map +1 -1
- package/dist/exception/exception.constants.d.ts +1 -0
- package/dist/exception/exception.constants.js +5 -0
- package/dist/exception/exception.constants.js.map +1 -0
- package/dist/exception/exception.module.d.ts +7 -0
- package/dist/exception/exception.module.js +41 -0
- package/dist/exception/exception.module.js.map +1 -0
- package/dist/exception/exceptions/auth.exception.d.ts +4 -0
- package/dist/exception/exceptions/auth.exception.js +12 -0
- package/dist/exception/exceptions/auth.exception.js.map +1 -0
- package/dist/exception/exceptions/forbidden-app.exception.d.ts +4 -0
- package/dist/exception/exceptions/forbidden-app.exception.js +12 -0
- package/dist/exception/exceptions/forbidden-app.exception.js.map +1 -0
- package/dist/exception/exceptions/index.d.ts +6 -0
- package/dist/exception/exceptions/index.js +16 -0
- package/dist/exception/exceptions/index.js.map +1 -0
- package/dist/exception/exceptions/internal-app.exception.d.ts +4 -0
- package/dist/exception/exceptions/internal-app.exception.js +12 -0
- package/dist/exception/exceptions/internal-app.exception.js.map +1 -0
- package/dist/exception/exceptions/not-found-app.exception.d.ts +4 -0
- package/dist/exception/exceptions/not-found-app.exception.js +12 -0
- package/dist/exception/exceptions/not-found-app.exception.js.map +1 -0
- package/dist/exception/exceptions/throttle-app.exception.d.ts +4 -0
- package/dist/exception/exceptions/throttle-app.exception.js +12 -0
- package/dist/exception/exceptions/throttle-app.exception.js.map +1 -0
- package/dist/exception/exceptions/validation-app.exception.d.ts +4 -0
- package/dist/exception/exceptions/validation-app.exception.js +12 -0
- package/dist/exception/exceptions/validation-app.exception.js.map +1 -0
- package/dist/exception/global-exception.filter.d.ts +8 -54
- package/dist/exception/global-exception.filter.js +45 -163
- package/dist/exception/global-exception.filter.js.map +1 -1
- package/dist/exception/handler/app-exception.handler.d.ts +7 -0
- package/dist/exception/handler/app-exception.handler.js +44 -0
- package/dist/exception/handler/app-exception.handler.js.map +1 -0
- package/dist/exception/handler/bad-request.handler.d.ts +6 -0
- package/dist/exception/handler/bad-request.handler.js +30 -0
- package/dist/exception/handler/bad-request.handler.js.map +1 -0
- package/dist/exception/handler/exception-handler.interface.d.ts +14 -0
- package/dist/exception/handler/exception-handler.interface.js +3 -0
- package/dist/exception/handler/exception-handler.interface.js.map +1 -0
- package/dist/exception/handler/forbidden.handler.d.ts +6 -0
- package/dist/exception/handler/forbidden.handler.js +27 -0
- package/dist/exception/handler/forbidden.handler.js.map +1 -0
- package/dist/exception/handler/index.d.ts +11 -0
- package/dist/exception/handler/index.js +38 -0
- package/dist/exception/handler/index.js.map +1 -0
- package/dist/exception/handler/microservice.handler.d.ts +6 -0
- package/dist/exception/handler/microservice.handler.js +27 -0
- package/dist/exception/handler/microservice.handler.js.map +1 -0
- package/dist/exception/handler/not-found.handler.d.ts +6 -0
- package/dist/exception/handler/not-found.handler.js +27 -0
- package/dist/exception/handler/not-found.handler.js.map +1 -0
- package/dist/exception/handler/throttler.handler.d.ts +6 -0
- package/dist/exception/handler/throttler.handler.js +33 -0
- package/dist/exception/handler/throttler.handler.js.map +1 -0
- package/dist/exception/handler/unknown.handler.d.ts +6 -0
- package/dist/exception/handler/unknown.handler.js +35 -0
- package/dist/exception/handler/unknown.handler.js.map +1 -0
- package/dist/exception/handler/username-not-found.handler.d.ts +6 -0
- package/dist/exception/handler/username-not-found.handler.js +28 -0
- package/dist/exception/handler/username-not-found.handler.js.map +1 -0
- package/dist/exception/index.d.ts +7 -4
- package/dist/exception/index.js +12 -3
- package/dist/exception/index.js.map +1 -1
- package/dist/exception/micro-service.exception.js +1 -1
- package/dist/exception/micro-service.exception.js.map +1 -1
- package/dist/exception/user-not-found.exception.js +1 -1
- package/dist/http/index.d.ts +3 -3
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/interceptor/serializable.interceptor.d.ts +4 -2
- package/dist/interceptor/serializable.interceptor.js +75 -18
- package/dist/interceptor/serializable.interceptor.js.map +1 -1
- package/dist/ip/index.d.ts +3 -2
- package/dist/ip/index.js +4 -1
- package/dist/ip/index.js.map +1 -1
- package/dist/ip/request-ip.d.ts +4 -2
- package/dist/ip/request-ip.js +92 -30
- package/dist/ip/request-ip.js.map +1 -1
- package/dist/ip/trusted-proxy-presets.d.ts +3 -0
- package/dist/ip/trusted-proxy-presets.js +114 -0
- package/dist/ip/trusted-proxy-presets.js.map +1 -0
- package/dist/module/configurable-module.builder.js +3 -1
- package/dist/module/configurable-module.builder.js.map +1 -1
- package/dist/module/index.d.ts +5 -5
- package/dist/module/index.js.map +1 -1
- package/dist/pagination/index.d.ts +1 -0
- package/dist/pagination/index.js +18 -0
- package/dist/pagination/index.js.map +1 -0
- package/dist/pagination/page.d.ts +39 -0
- package/dist/pagination/page.js +87 -0
- package/dist/pagination/page.js.map +1 -0
- package/dist/serialize/localize.string.js.map +1 -1
- package/dist/utils/package-utils.js +3 -0
- package/dist/utils/package-utils.js.map +1 -1
- package/dist/validation/index.d.ts +1 -0
- package/dist/validation/index.js +1 -0
- package/dist/validation/index.js.map +1 -1
- package/dist/validation/validators.d.ts +3 -0
- package/dist/validation/validators.js +29 -0
- package/dist/validation/validators.js.map +1 -0
- package/docs/20260214-coverage-plan.md +52 -0
- package/docs/20260214-fix-plan-common-bugs.md +90 -0
- package/docs/20260214-fix-plan-common-cleanup.md +119 -0
- package/docs/20260214-fix-plan-exception-redesign.md +274 -0
- package/docs/20260214-fix-plan-nestjs-prisma.md +454 -0
- package/docs/20260214-fix-plan-pagination-move-and-rename.md +148 -0
- package/docs/20260214-review-common.md +426 -0
- package/docs/20260215-fix-plan-review-enhancements.md +110 -0
- package/docs/20260215-fix-plan-trust-proxy-presets.md +227 -0
- package/docs/20260215-review-nestjs-common.md +1015 -0
- package/docs/trust-proxy-security.md +475 -0
- package/package.json +1 -1
- package/dist/exception/app-exception.filter.d.ts +0 -9
- package/dist/exception/app-exception.filter.js +0 -52
- package/dist/exception/app-exception.filter.js.map +0 -1
- package/dist/i18n/i18n.constants.d.ts +0 -0
- package/dist/i18n/i18n.constants.js +0 -1
- package/dist/i18n/i18n.constants.js.map +0 -1
- package/dist/middleware/index.d.ts +0 -4
- package/dist/middleware/index.js +0 -14
- package/dist/middleware/index.js.map +0 -1
- package/dist/middleware/log.provider.d.ts +0 -38
- package/dist/middleware/log.provider.js +0 -147
- package/dist/middleware/log.provider.js.map +0 -1
- package/dist/middleware/logger-option.factory.d.ts +0 -17
- package/dist/middleware/logger-option.factory.js +0 -73
- package/dist/middleware/logger-option.factory.js.map +0 -1
- package/dist/middleware/logger.middleware.d.ts +0 -8
- package/dist/middleware/logger.middleware.js +0 -97
- package/dist/middleware/logger.middleware.js.map +0 -1
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
# @nathapp/nestjs-common — Deep Code Review
|
|
2
|
+
|
|
3
|
+
**Date:** 2026-02-14
|
|
4
|
+
**Branch:** `feat/monorepo-enterprise-upgrade`
|
|
5
|
+
**Reviewer:** Subrina (automated deep review)
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Summary
|
|
10
|
+
|
|
11
|
+
The `nestjs-common` package is a solid foundation with good test coverage for core paths (exception filters, IP resolution, config validation, configurable modules). However, several issues need attention — particularly around security, type safety, and a few potential bugs.
|
|
12
|
+
|
|
13
|
+
**Stats:** ~2,860 lines across 40+ source files, 15 test files
|
|
14
|
+
**Overall Grade:** B+ — Good structure, needs hardening for enterprise
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 1. 🔴 Security
|
|
19
|
+
|
|
20
|
+
### 1.1 🔴 `AppException` always returns HTTP 500, ignoring `httpStatus` parameter
|
|
21
|
+
|
|
22
|
+
**File:** `lib/exception/app.exception.ts:8`
|
|
23
|
+
|
|
24
|
+
The constructor passes `500` to `HttpException` regardless of the `httpStatus` parameter. The `getStatus()` method will always return 500.
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
// PROBLEM
|
|
28
|
+
constructor(code: number, args?: any, prefix?: string, httpStatus?: number) {
|
|
29
|
+
super({ code, args, prefix }, 500); // ← always 500
|
|
30
|
+
this._httpStatus = httpStatus;
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
The `GlobalExceptionsFilter` works around this via `exception.httpStatus || this.getStatusCodeFromErrorCode(errorCode)`, but `AppExceptionFilter` calls `exception.getStatus()` which always returns 500.
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
// FIX
|
|
38
|
+
constructor(code: number, args?: any, prefix?: string, httpStatus?: number) {
|
|
39
|
+
super({ code, args, prefix }, httpStatus || 500);
|
|
40
|
+
// ...
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 1.2 🔴 `UsernameNotFoundException` returns HTTP 500 instead of 404
|
|
45
|
+
|
|
46
|
+
**File:** `lib/exception/user-not-found.exception.ts:4`
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
// PROBLEM
|
|
50
|
+
export class UsernameNotFoundException extends HttpException {
|
|
51
|
+
constructor(message: string) {
|
|
52
|
+
super(message, 500); // ← Should be 404
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The `GlobalExceptionsFilter` maps it to 404, but if used outside that filter, it's a 500. Fix: `super(message, 404)`.
|
|
58
|
+
|
|
59
|
+
### 1.3 🔴 `ThrottlerException` returns HTTP 500 instead of 429
|
|
60
|
+
|
|
61
|
+
**File:** `lib/exception/global-exception.filter.ts:161`
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
async handleThrottlerException(exception: any, lang: string) {
|
|
65
|
+
const httpStatus = 500; // ← Should be 429
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Rate-limited responses should return `429 Too Many Requests`.
|
|
69
|
+
|
|
70
|
+
### 1.4 🟡 Global mutable state in `request-ip.ts` — `headerPriority` mutation
|
|
71
|
+
|
|
72
|
+
**File:** `lib/ip/request-ip.ts:28-30` and `mw()` function
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
let headerPriority = DEFAULT; // ← shares reference with DEFAULT array
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
The `mw()` function mutates `headerPriority` via `splice` and `unshift`. Since `headerPriority` initially references `DEFAULT`, the first call to `mw()` with `prioritize` will also mutate the `DEFAULT` const array. Subsequent calls will stack priorities.
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
// FIX: Clone on init
|
|
82
|
+
let headerPriority = [...DEFAULT];
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### 1.5 🟡 `loadPackage` uses dynamic `require` without validation
|
|
86
|
+
|
|
87
|
+
**File:** `lib/utils/package-utils.ts:8`
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
export const loadPackage = (packageName: string) => {
|
|
91
|
+
return require(packageName); // ← No input sanitization
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
If `packageName` comes from user input, this is a code injection vector. Add validation:
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
export const loadPackage = (packageName: string) => {
|
|
99
|
+
if (!packageName || /[\/\\]/.test(packageName)) {
|
|
100
|
+
throw new Error('Invalid package name');
|
|
101
|
+
}
|
|
102
|
+
return require(packageName);
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### 1.6 🟡 Sensitive data in `LoggerMiddleware` response body capture
|
|
107
|
+
|
|
108
|
+
**File:** `lib/middleware/logger.middleware.ts:46-72`
|
|
109
|
+
|
|
110
|
+
The middleware monkey-patches `response.write` and `response.end` to capture response chunks. While sensitive keys are censored on output, the raw response body is held in memory as `Buffer[]`. If the process crashes between capture and censoring, raw data could end up in core dumps.
|
|
111
|
+
|
|
112
|
+
Also, the `subscribeResponseChunk` method uses `arguments` keyword which breaks in strict mode and arrow functions.
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
## 2. 🔴 Possible Bugs
|
|
117
|
+
|
|
118
|
+
### 2.1 🔴 `defaultResponseFormatter` crashes on `null` constraints
|
|
119
|
+
|
|
120
|
+
**File:** `lib/exception/default-i18n-validation-exception.filter.ts:31-45`
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
const firstError = findFirstError(error);
|
|
124
|
+
if(firstError) {
|
|
125
|
+
message = firstError.constraints[Object.keys(firstError.constraints)[0]];
|
|
126
|
+
// ^^^^^^^^^^^ can be undefined if no constraints
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
If a validation error has children but the leaf node has no `constraints`, this will throw `TypeError: Cannot read properties of undefined`. Add null check:
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
if(firstError?.constraints) {
|
|
133
|
+
message = firstError.constraints[Object.keys(firstError.constraints)[0]];
|
|
134
|
+
// ...
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### 2.2 🟡 `SerializableInterceptor.transformObject` — Array check is unreachable
|
|
139
|
+
|
|
140
|
+
**File:** `lib/interceptor/serializable.interceptor.ts:62-64`
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
144
|
+
if (this.isObject(value)) {
|
|
145
|
+
// ...
|
|
146
|
+
} else if (Array.isArray(obj)) { // ← checks `obj`, not `value`
|
|
147
|
+
obj[key] = value.map(...) // ← `value` is not an array here
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Two bugs:
|
|
153
|
+
1. `Array.isArray(obj)` checks the parent, not the value
|
|
154
|
+
2. Even if it's the parent, `value` already failed `isObject` check, so `.map()` will fail on primitives
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
// FIX
|
|
158
|
+
} else if (Array.isArray(value)) {
|
|
159
|
+
obj[key] = value.map((item: PlainLiteralObject) => this.transformObject(item, lang));
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### 2.3 🟡 `HttpMicroServiceException` always returns HTTP 200 to Express
|
|
164
|
+
|
|
165
|
+
**File:** `lib/exception/micro-service.exception.ts:5`
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
super({ ret, message }, 200); // ← HTTP 200 for an error?
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
This means `exception.getStatus()` returns 200, but `GlobalExceptionsFilter` overrides with 500. If anyone catches this exception independently, they'll send a `200 OK` with error content — confusing for clients.
|
|
172
|
+
|
|
173
|
+
### 2.4 🟡 `AppExceptionFilter` casts response to Express `Response` only
|
|
174
|
+
|
|
175
|
+
**File:** `lib/exception/app-exception.filter.ts:22`
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
const response = ctx.getResponse<Response>(); // Express Response
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
This won't work with Fastify. The `GlobalExceptionsFilter` handles this correctly via `httpAdapterHost`, but `AppExceptionFilter` is Fastify-incompatible.
|
|
182
|
+
|
|
183
|
+
### 2.5 🟢 `parseLocaleString` / `parseLocaleObject` — JSON.parse result not validated
|
|
184
|
+
|
|
185
|
+
**File:** `lib/serialize/localize.string.ts:26-32`
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
try {
|
|
189
|
+
result = JSON.parse(source);
|
|
190
|
+
} catch (e) {
|
|
191
|
+
result[defaultLocale] = defaultValue;
|
|
192
|
+
}
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
If `source` is `"42"` or `"true"`, `JSON.parse` succeeds but returns a non-object. Should validate the parsed result is actually an object.
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
## 3. 🟡 Type Safety
|
|
200
|
+
|
|
201
|
+
### 3.1 🟡 Excessive `any` usage throughout the codebase
|
|
202
|
+
|
|
203
|
+
Occurrences of untyped `any`:
|
|
204
|
+
|
|
205
|
+
| File | Count | Notable |
|
|
206
|
+
|------|-------|---------|
|
|
207
|
+
| `global-exception.filter.ts` | 8 | `exception: any`, `args?: any` |
|
|
208
|
+
| `app.exception.ts` | 2 | `args?: any` |
|
|
209
|
+
| `serializable.interceptor.ts` | 5 | `isObject(fn: any)`, `isNil(val: any)` |
|
|
210
|
+
| `log.provider.ts` | 6 | `processOutput(data: any)` |
|
|
211
|
+
| `logger.middleware.ts` | 4 | `request: any`, `response: any` |
|
|
212
|
+
| `request-ip.ts` | 5 | `req: any` throughout |
|
|
213
|
+
| `configurable-module.builder.ts` | 4 | `sanitizeOptions`, `omitExtras` |
|
|
214
|
+
| `async-options-provider.ts` | 2 | fallback provider |
|
|
215
|
+
|
|
216
|
+
**Recommendation:** Create a `RequestLike` interface for the IP module. Type `args` as `Record<string, unknown>` where possible.
|
|
217
|
+
|
|
218
|
+
### 3.2 🟡 `i18n.constants.ts` is empty
|
|
219
|
+
|
|
220
|
+
**File:** `lib/i18n/i18n.constants.ts`
|
|
221
|
+
|
|
222
|
+
Empty file exported via barrel. Either add constants or remove the file.
|
|
223
|
+
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
## 4. 🟡 Code Cleanliness
|
|
227
|
+
|
|
228
|
+
### 4.1 🟡 Deprecated code still shipped — entire `middleware/` module
|
|
229
|
+
|
|
230
|
+
**Files:** `lib/middleware/log.provider.ts`, `logger-option.factory.ts`, `logger.middleware.ts`
|
|
231
|
+
|
|
232
|
+
All classes are marked `@deprecated` with "use `@nathapp/nestjs-logging` instead", yet they're still exported and tested. This is ~300 lines of deprecated code in the foundation package.
|
|
233
|
+
|
|
234
|
+
**Recommendation:** Move to a separate `@nathapp/nestjs-common-compat` package or set a removal timeline.
|
|
235
|
+
|
|
236
|
+
### 4.2 🟡 Commented-out code in `GlobalExceptionsFilter`
|
|
237
|
+
|
|
238
|
+
**File:** `lib/exception/global-exception.filter.ts:179-188`
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
// async HandleTwoFactorException(...) {
|
|
242
|
+
// ...
|
|
243
|
+
// }
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Dead commented code. Remove it — it's in git history.
|
|
247
|
+
|
|
248
|
+
### 4.3 🟡 Inconsistent barrel export style
|
|
249
|
+
|
|
250
|
+
**Files:** `lib/config/index.ts`, `lib/constant/index.ts`, `lib/decorator/index.ts`
|
|
251
|
+
|
|
252
|
+
Some use named imports + re-exports, others use `export *`. The main `index.ts` uses `export *` but sub-barrels mix styles. Standardize on `export *` or named re-exports.
|
|
253
|
+
|
|
254
|
+
### 4.4 🟢 Typo: `handleCaptchException` → `handleCaptchaException`
|
|
255
|
+
|
|
256
|
+
**File:** `lib/exception/global-exception.filter.ts:167`
|
|
257
|
+
|
|
258
|
+
### 4.5 🟢 Typo in configurable-module.builder.ts error message
|
|
259
|
+
|
|
260
|
+
**File:** `lib/module/configurable-module.builder.ts:214`
|
|
261
|
+
|
|
262
|
+
```typescript
|
|
263
|
+
`Expected ${this.name} to be configured by at last one Module`
|
|
264
|
+
// ^^^^ → "least"
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
### 4.6 🟢 `serialize/index.ts` mixes export styles
|
|
268
|
+
|
|
269
|
+
```typescript
|
|
270
|
+
export { JsonResponse, Serializable };
|
|
271
|
+
export * from "./localize.string"; // ← mixed style
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### 4.7 🟢 Unused import: `InjectableOptions` and `InjectionToken`
|
|
275
|
+
|
|
276
|
+
**File:** `lib/module/configurable-module.builder.ts:4`
|
|
277
|
+
|
|
278
|
+
`InjectableOptions` is imported but never used.
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## 5. 🟡 Memory & Resource Management
|
|
283
|
+
|
|
284
|
+
### 5.1 🟡 `ConfigurableModuleBuilder` — `Subject` never completes
|
|
285
|
+
|
|
286
|
+
**File:** `lib/module/configurable-module.builder.ts:183`
|
|
287
|
+
|
|
288
|
+
```typescript
|
|
289
|
+
static moduleSubject = new Subject<DynamicModule>();
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
This `Subject` is never completed. While not a major leak (it's static), if `externallyConfigured()` is called after the module is already registered, the subscription from `race()` on `timeout$` interval will keep ticking. The `first()` operator handles cleanup for the happy path, but the `interval` in the timeout branch creates a subscription that needs cleanup.
|
|
293
|
+
|
|
294
|
+
### 5.2 🟢 `LoggerMiddleware` response listener not cleaned up on error
|
|
295
|
+
|
|
296
|
+
**File:** `lib/middleware/logger.middleware.ts:57`
|
|
297
|
+
|
|
298
|
+
The `response.on('finish', ...)` listener is added but if the response is destroyed before 'finish' fires (e.g., client disconnect), the chunks array is never freed. Consider also listening for `'close'` event.
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
302
|
+
## 6. 🟡 Extensibility
|
|
303
|
+
|
|
304
|
+
### 6.1 🟡 `GlobalExceptionsFilter` is a monolith — hard to extend
|
|
305
|
+
|
|
306
|
+
**File:** `lib/exception/global-exception.filter.ts`
|
|
307
|
+
|
|
308
|
+
At ~200 lines with hardcoded exception type checks and dynamic `require` for `@nestjs/throttler`, this filter is difficult to extend. Adding a new exception type requires modifying the filter itself.
|
|
309
|
+
|
|
310
|
+
**Suggestion:** Use a strategy/chain-of-responsibility pattern:
|
|
311
|
+
|
|
312
|
+
```typescript
|
|
313
|
+
interface ExceptionHandler {
|
|
314
|
+
canHandle(exception: unknown): boolean;
|
|
315
|
+
handle(exception: unknown, lang: string): Promise<ExceptionResult>;
|
|
316
|
+
}
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
### 6.2 🟡 `GlobalExceptionsFilter` has hard dependency on `nestjs-i18n`
|
|
320
|
+
|
|
321
|
+
The constructor requires `I18nService` which means every consumer must install `nestjs-i18n` even if they don't need i18n. The `translate` method handles null gracefully but the constructor signature doesn't use `@Optional()`.
|
|
322
|
+
|
|
323
|
+
### 6.3 🟢 `Serializable` is an empty abstract class
|
|
324
|
+
|
|
325
|
+
**File:** `lib/serialize/serializable.ts`
|
|
326
|
+
|
|
327
|
+
This is a marker class with no methods or properties. Consider using a symbol-based marker or interface instead, to avoid forcing class inheritance.
|
|
328
|
+
|
|
329
|
+
---
|
|
330
|
+
|
|
331
|
+
## 7. Test Coverage Gaps
|
|
332
|
+
|
|
333
|
+
### 7.1 🔴 No tests for `SerializableInterceptor`
|
|
334
|
+
|
|
335
|
+
The interceptor has complex locale-aware serialization logic including `LocaleString`, `LocaleObject`, nested object traversal, and the buggy array handling (see 2.2). Zero test coverage.
|
|
336
|
+
|
|
337
|
+
### 7.2 🔴 No tests for `I18nValidationExceptionFilter`
|
|
338
|
+
|
|
339
|
+
The custom i18n validation exception filter with response formatting, error normalization, and GraphQL support has no direct unit tests.
|
|
340
|
+
|
|
341
|
+
### 7.3 🟡 No tests for `AppException` class itself
|
|
342
|
+
|
|
343
|
+
Only tested indirectly through e2e filter tests. The `httpStatus` getter, `args`, `prefix` accessors are untested.
|
|
344
|
+
|
|
345
|
+
### 7.4 🟡 No tests for `validation.ts` i18n wrappers
|
|
346
|
+
|
|
347
|
+
17 exported decorator wrappers (`IsNotEmptyWithI18n`, `IsStringWithI18n`, etc.) with zero tests.
|
|
348
|
+
|
|
349
|
+
### 7.5 🟡 No tests for `BaseI18nValidatorConstraint`
|
|
350
|
+
|
|
351
|
+
The abstract validator constraint class has no tests.
|
|
352
|
+
|
|
353
|
+
### 7.6 🟡 No tests for `LocaleString`, `LocaleObject`, `parseLocaleString`, `parseLocaleObject`, `remapLocaleObject`
|
|
354
|
+
|
|
355
|
+
All serialization primitives are untested.
|
|
356
|
+
|
|
357
|
+
### 7.7 🟡 No tests for `package-utils.ts`
|
|
358
|
+
|
|
359
|
+
`isPackageInstalled`, `loadPackage`, `resolvePackagePath` are untested.
|
|
360
|
+
|
|
361
|
+
### 7.8 🟢 No tests for `EmptyLogger`
|
|
362
|
+
|
|
363
|
+
Trivial, but given it's exported as public API.
|
|
364
|
+
|
|
365
|
+
### 7.9 🟢 No tests for `CaptchaException`, `TwoFactorRequiredException`, `TwoFactorInvalidException`
|
|
366
|
+
|
|
367
|
+
Only `ThrottlerException` path is e2e tested in the global filter.
|
|
368
|
+
|
|
369
|
+
### Coverage Matrix
|
|
370
|
+
|
|
371
|
+
| Module | Unit | E2E | Coverage |
|
|
372
|
+
|--------|------|-----|----------|
|
|
373
|
+
| config/ | ✅ | — | Good |
|
|
374
|
+
| decorator/ | — | ✅ | Good |
|
|
375
|
+
| exception/ (filters) | — | ✅ | Good |
|
|
376
|
+
| exception/ (classes) | ❌ | Partial | Gaps |
|
|
377
|
+
| i18n/ | ✅ | — | Good |
|
|
378
|
+
| interceptor/ | ❌ | ❌ | **None** |
|
|
379
|
+
| ip/ | ✅ | — | Excellent |
|
|
380
|
+
| middleware/ | ✅ | ✅ | Good |
|
|
381
|
+
| module/ | ✅ | — | Good |
|
|
382
|
+
| serialize/ | Partial | — | **Weak** |
|
|
383
|
+
| utils/ | ❌ | ❌ | **None** |
|
|
384
|
+
| validation/ | ❌ | ❌ | **None** |
|
|
385
|
+
|
|
386
|
+
---
|
|
387
|
+
|
|
388
|
+
## 8. Public API Surface
|
|
389
|
+
|
|
390
|
+
### 8.1 🟡 Over-exporting internal implementation details
|
|
391
|
+
|
|
392
|
+
- `getClientIpFromXForwardedFor` and `getClientIpByHeader` are exported from `request-ip.ts` — these are implementation details
|
|
393
|
+
- `defaultResponseFormatter` is exported — internal to `DefaultI18nValidationExceptionFilter`
|
|
394
|
+
- `ConfigurableModuleBuilderOptions` extends an internal NestJS type and is exported
|
|
395
|
+
|
|
396
|
+
### 8.2 🟢 Clean top-level barrel
|
|
397
|
+
|
|
398
|
+
The main `lib/index.ts` uses clean `export *` re-exports from all sub-modules. Good structure.
|
|
399
|
+
|
|
400
|
+
### 8.3 🟢 `i18n.constants.ts` exported but empty
|
|
401
|
+
|
|
402
|
+
Adds to the public API surface with no value. Remove or populate.
|
|
403
|
+
|
|
404
|
+
---
|
|
405
|
+
|
|
406
|
+
## Priority Action Items
|
|
407
|
+
|
|
408
|
+
| Priority | Issue | Effort |
|
|
409
|
+
|----------|-------|--------|
|
|
410
|
+
| 🔴 P0 | Fix `AppException` ignoring `httpStatus` in super call (1.1) | 5 min |
|
|
411
|
+
| 🔴 P0 | Fix `UsernameNotFoundException` returning 500 (1.2) | 5 min |
|
|
412
|
+
| 🔴 P0 | Fix `ThrottlerException` returning 500 instead of 429 (1.3) | 5 min |
|
|
413
|
+
| 🔴 P0 | Fix null constraint crash in `defaultResponseFormatter` (2.1) | 10 min |
|
|
414
|
+
| 🔴 P0 | Fix array check bug in `SerializableInterceptor` (2.2) | 10 min |
|
|
415
|
+
| 🔴 P0 | Add tests for `SerializableInterceptor` (7.1) | 2 hrs |
|
|
416
|
+
| 🟡 P1 | Fix `headerPriority` mutation of DEFAULT array (1.4) | 5 min |
|
|
417
|
+
| 🟡 P1 | Add tests for serialization utils (7.6) | 1 hr |
|
|
418
|
+
| 🟡 P1 | Add tests for validation wrappers (7.4) | 1 hr |
|
|
419
|
+
| 🟡 P1 | Remove commented-out code (4.2) | 5 min |
|
|
420
|
+
| 🟡 P1 | Fix `HttpMicroServiceException` HTTP 200 status (2.3) | 15 min |
|
|
421
|
+
| 🟡 P1 | Make `AppExceptionFilter` Fastify-compatible (2.4) | 30 min |
|
|
422
|
+
| 🟡 P2 | Reduce `any` usage — create proper interfaces (3.1) | 2 hrs |
|
|
423
|
+
| 🟡 P2 | Plan deprecation removal for middleware module (4.1) | Planning |
|
|
424
|
+
| 🟢 P3 | Fix typos (4.4, 4.5) | 5 min |
|
|
425
|
+
| 🟢 P3 | Standardize barrel exports (4.3, 4.6) | 30 min |
|
|
426
|
+
| 🟢 P3 | Remove empty `i18n.constants.ts` (3.2) | 5 min |
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# Fix Plan: nestjs-common Review Enhancements
|
|
2
|
+
|
|
3
|
+
**Date:** 2026-02-15
|
|
4
|
+
**Branch:** feat/monorepo-enterprise-upgrade
|
|
5
|
+
**Package:** `packages/nestjs-common`
|
|
6
|
+
|
|
7
|
+
## Phase 1: Code Hardening (PERF-1, ENH-8, ENH-14, WARN-8)
|
|
8
|
+
|
|
9
|
+
### PERF-1: Depth limit on transformObject
|
|
10
|
+
**File:** `lib/interceptor/serializable.interceptor.ts`
|
|
11
|
+
**Change:** Add `depth` parameter with MAX_DEPTH=100. Log warning when exceeded, return obj as-is.
|
|
12
|
+
|
|
13
|
+
### ENH-8: Improve error messages
|
|
14
|
+
**File:** `lib/module/configurable-module.builder.ts`
|
|
15
|
+
**Change:** Make `externallyConfigured()` timeout error message actionable — include module name, timeout value, and common fix suggestion.
|
|
16
|
+
|
|
17
|
+
### ENH-14: Config validation for trust proxy
|
|
18
|
+
**File:** `lib/ip/request-ip.ts`
|
|
19
|
+
**Change:** In `configureTrustProxy()`, validate each trustedProxies entry is valid IP or CIDR. Throw descriptive error for invalid entries. Also validate provider names.
|
|
20
|
+
|
|
21
|
+
### WARN-8: ConfigurableModuleBuilder any types
|
|
22
|
+
**File:** `lib/module/configurable-module.builder.ts`
|
|
23
|
+
**Change:** Replace `any` with proper generic types where possible. At minimum add `unknown` where `any` is used as a type parameter.
|
|
24
|
+
|
|
25
|
+
### Commit: `fix(nestjs-common): add depth limit, config validation, improve error messages`
|
|
26
|
+
|
|
27
|
+
## Phase 2: New Utilities (ENH-5)
|
|
28
|
+
|
|
29
|
+
### ENH-5: Validation helper utilities
|
|
30
|
+
**File:** `lib/validation/validators.ts` (NEW)
|
|
31
|
+
**Change:** Add utility functions:
|
|
32
|
+
- `isValidLocaleCode(code: string): boolean` — validates `xx_XX` format
|
|
33
|
+
- `sanitizeHeader(value: string, maxLength?: number): string` — strip dangerous chars
|
|
34
|
+
- `clampNumber(value: number, min: number, max: number): number` — bounded numeric
|
|
35
|
+
Export from `lib/validation/index.ts` and `lib/index.ts`
|
|
36
|
+
|
|
37
|
+
### Commit: `feat(nestjs-common): add validation helper utilities`
|
|
38
|
+
|
|
39
|
+
## Phase 3: JSDoc Documentation (ENH-6)
|
|
40
|
+
|
|
41
|
+
### ENH-6: JSDoc on public APIs
|
|
42
|
+
Add JSDoc to all exported classes/interfaces/functions in:
|
|
43
|
+
- `lib/exception/global-exception.filter.ts` — handler priority, error format, registration methods
|
|
44
|
+
- `lib/exception/handler/exception-handler.interface.ts` — contract, examples
|
|
45
|
+
- `lib/exception/app.exception.ts` — when to use, args type
|
|
46
|
+
- `lib/exception/exception.module.ts` — usage example
|
|
47
|
+
- `lib/interceptor/serializable.interceptor.ts` — behavior, locale resolution, depth limit
|
|
48
|
+
- `lib/pagination/page.ts` — Page, PageOption, resolvePageOption, input validation
|
|
49
|
+
- `lib/module/configurable-module.builder.ts` — externallyConfigured behavior
|
|
50
|
+
- `lib/ip/request-ip.ts` — configureTrustProxy security warnings, provider presets
|
|
51
|
+
- `lib/ip/trusted-proxy-presets.ts` — provider list, resolveProviderCidrs
|
|
52
|
+
- `lib/serialize/localize.string.ts` — LocaleString, LocaleObject, parse functions
|
|
53
|
+
- `lib/http/response.ts` — JsonResponse
|
|
54
|
+
- `lib/config/server-security.config.ts` — CORS regex, env vars
|
|
55
|
+
|
|
56
|
+
Keep JSDoc concise — 2-5 lines per export. Focus on non-obvious behavior and security implications.
|
|
57
|
+
|
|
58
|
+
### Commit: `docs(nestjs-common): add JSDoc documentation to public APIs`
|
|
59
|
+
|
|
60
|
+
## Phase 4: Edge Case Tests (ENH-7)
|
|
61
|
+
|
|
62
|
+
### ENH-7: Test coverage for edge cases
|
|
63
|
+
**File:** `test/interceptor/serializable-edge-cases.spec.ts` (NEW)
|
|
64
|
+
- Deeply nested objects (50+ levels) — should not stack overflow with depth limit
|
|
65
|
+
- Circular reference handling — should not infinite loop
|
|
66
|
+
- Arrays with mixed types (strings, objects, nulls, LocaleString)
|
|
67
|
+
- Empty object/array handling
|
|
68
|
+
|
|
69
|
+
**File:** `test/exception/global-exception-filter-edge-cases.spec.ts` (NEW)
|
|
70
|
+
- Handler that throws during canHandle()
|
|
71
|
+
- Handler that throws during handle()
|
|
72
|
+
- No matching handler falls through to default
|
|
73
|
+
- Multiple handlers match — first wins
|
|
74
|
+
|
|
75
|
+
**File:** `test/ip/request-ip-spoofing.spec.ts` (NEW)
|
|
76
|
+
- Multiple X-Forwarded-For headers
|
|
77
|
+
- Headers with invalid IP formats
|
|
78
|
+
- IPv4-mapped IPv6 addresses
|
|
79
|
+
- Empty/whitespace-only header values
|
|
80
|
+
|
|
81
|
+
**File:** `test/pagination/page-validation.spec.ts` (NEW)
|
|
82
|
+
- Negative page numbers → clamped to 1
|
|
83
|
+
- Zero/negative size → clamped to 1
|
|
84
|
+
- NaN/undefined/null inputs → defaults
|
|
85
|
+
- Float inputs → floored
|
|
86
|
+
- Very large numbers
|
|
87
|
+
|
|
88
|
+
Run tests: `npx jest --config packages/nestjs-common/test/jest-test.json --no-coverage --forceExit`
|
|
89
|
+
### Commit: `test(nestjs-common): add edge case tests for interceptor, exception filter, IP, pagination`
|
|
90
|
+
|
|
91
|
+
## Phase 5: Trust Proxy Documentation (ENH-3)
|
|
92
|
+
|
|
93
|
+
### ENH-3: README documentation
|
|
94
|
+
**File:** `packages/nestjs-common/README.md` (update or create section)
|
|
95
|
+
Add a "Trust Proxy Configuration" section covering:
|
|
96
|
+
- Why trust proxy matters (IP spoofing attacks)
|
|
97
|
+
- Default behavior in 3.0.0 (disabled, fail-closed)
|
|
98
|
+
- Provider presets with examples (cloudflare, aws-alb, private, etc.)
|
|
99
|
+
- Custom CIDR configuration
|
|
100
|
+
- Migration from 2.x (was trust-all, now explicit)
|
|
101
|
+
- Common deployment examples
|
|
102
|
+
|
|
103
|
+
If README.md doesn't exist, create a minimal one focused on trust proxy.
|
|
104
|
+
|
|
105
|
+
### Commit: `docs(nestjs-common): add trust proxy security documentation`
|
|
106
|
+
|
|
107
|
+
## Test Strategy
|
|
108
|
+
- Mode: test-after (internal enhancements, not public API)
|
|
109
|
+
- All phases should run existing tests to verify no regressions
|
|
110
|
+
- Phase 4 adds new tests
|