@nathapp/nestjs-common 2.1.5 → 3.0.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.
Files changed (151) hide show
  1. package/CLAUDE.md +29 -0
  2. package/README.md +134 -0
  3. package/dist/config/index.d.ts +4 -4
  4. package/dist/config/index.js.map +1 -1
  5. package/dist/config/server-security.config.d.ts +1 -0
  6. package/dist/config/server-security.config.js +19 -2
  7. package/dist/config/server-security.config.js.map +1 -1
  8. package/dist/decorator/index.d.ts +2 -2
  9. package/dist/decorator/index.js.map +1 -1
  10. package/dist/entities/base-entity.d.ts +3 -0
  11. package/dist/entities/base-entity.js +8 -0
  12. package/dist/entities/base-entity.js.map +1 -0
  13. package/dist/entities/index.d.ts +1 -0
  14. package/dist/entities/index.js +18 -0
  15. package/dist/entities/index.js.map +1 -0
  16. package/dist/exception/app.exception.d.ts +4 -3
  17. package/dist/exception/app.exception.js +1 -1
  18. package/dist/exception/app.exception.js.map +1 -1
  19. package/dist/exception/default-i18n-validation-exception.filter.js +1 -1
  20. package/dist/exception/default-i18n-validation-exception.filter.js.map +1 -1
  21. package/dist/exception/exception.constants.d.ts +1 -0
  22. package/dist/exception/exception.constants.js +5 -0
  23. package/dist/exception/exception.constants.js.map +1 -0
  24. package/dist/exception/exception.module.d.ts +7 -0
  25. package/dist/exception/exception.module.js +41 -0
  26. package/dist/exception/exception.module.js.map +1 -0
  27. package/dist/exception/exceptions/auth.exception.d.ts +4 -0
  28. package/dist/exception/exceptions/auth.exception.js +12 -0
  29. package/dist/exception/exceptions/auth.exception.js.map +1 -0
  30. package/dist/exception/exceptions/forbidden-app.exception.d.ts +4 -0
  31. package/dist/exception/exceptions/forbidden-app.exception.js +12 -0
  32. package/dist/exception/exceptions/forbidden-app.exception.js.map +1 -0
  33. package/dist/exception/exceptions/index.d.ts +6 -0
  34. package/dist/exception/exceptions/index.js +16 -0
  35. package/dist/exception/exceptions/index.js.map +1 -0
  36. package/dist/exception/exceptions/internal-app.exception.d.ts +4 -0
  37. package/dist/exception/exceptions/internal-app.exception.js +12 -0
  38. package/dist/exception/exceptions/internal-app.exception.js.map +1 -0
  39. package/dist/exception/exceptions/not-found-app.exception.d.ts +4 -0
  40. package/dist/exception/exceptions/not-found-app.exception.js +12 -0
  41. package/dist/exception/exceptions/not-found-app.exception.js.map +1 -0
  42. package/dist/exception/exceptions/throttle-app.exception.d.ts +4 -0
  43. package/dist/exception/exceptions/throttle-app.exception.js +12 -0
  44. package/dist/exception/exceptions/throttle-app.exception.js.map +1 -0
  45. package/dist/exception/exceptions/validation-app.exception.d.ts +4 -0
  46. package/dist/exception/exceptions/validation-app.exception.js +12 -0
  47. package/dist/exception/exceptions/validation-app.exception.js.map +1 -0
  48. package/dist/exception/global-exception.filter.d.ts +8 -54
  49. package/dist/exception/global-exception.filter.js +45 -163
  50. package/dist/exception/global-exception.filter.js.map +1 -1
  51. package/dist/exception/handler/app-exception.handler.d.ts +7 -0
  52. package/dist/exception/handler/app-exception.handler.js +44 -0
  53. package/dist/exception/handler/app-exception.handler.js.map +1 -0
  54. package/dist/exception/handler/bad-request.handler.d.ts +6 -0
  55. package/dist/exception/handler/bad-request.handler.js +30 -0
  56. package/dist/exception/handler/bad-request.handler.js.map +1 -0
  57. package/dist/exception/handler/exception-handler.interface.d.ts +14 -0
  58. package/dist/exception/handler/exception-handler.interface.js +3 -0
  59. package/dist/exception/handler/exception-handler.interface.js.map +1 -0
  60. package/dist/exception/handler/forbidden.handler.d.ts +6 -0
  61. package/dist/exception/handler/forbidden.handler.js +27 -0
  62. package/dist/exception/handler/forbidden.handler.js.map +1 -0
  63. package/dist/exception/handler/index.d.ts +11 -0
  64. package/dist/exception/handler/index.js +38 -0
  65. package/dist/exception/handler/index.js.map +1 -0
  66. package/dist/exception/handler/microservice.handler.d.ts +6 -0
  67. package/dist/exception/handler/microservice.handler.js +27 -0
  68. package/dist/exception/handler/microservice.handler.js.map +1 -0
  69. package/dist/exception/handler/not-found.handler.d.ts +6 -0
  70. package/dist/exception/handler/not-found.handler.js +27 -0
  71. package/dist/exception/handler/not-found.handler.js.map +1 -0
  72. package/dist/exception/handler/throttler.handler.d.ts +6 -0
  73. package/dist/exception/handler/throttler.handler.js +33 -0
  74. package/dist/exception/handler/throttler.handler.js.map +1 -0
  75. package/dist/exception/handler/unknown.handler.d.ts +6 -0
  76. package/dist/exception/handler/unknown.handler.js +35 -0
  77. package/dist/exception/handler/unknown.handler.js.map +1 -0
  78. package/dist/exception/handler/username-not-found.handler.d.ts +6 -0
  79. package/dist/exception/handler/username-not-found.handler.js +28 -0
  80. package/dist/exception/handler/username-not-found.handler.js.map +1 -0
  81. package/dist/exception/index.d.ts +7 -4
  82. package/dist/exception/index.js +12 -3
  83. package/dist/exception/index.js.map +1 -1
  84. package/dist/exception/micro-service.exception.js +1 -1
  85. package/dist/exception/micro-service.exception.js.map +1 -1
  86. package/dist/exception/user-not-found.exception.js +1 -1
  87. package/dist/http/index.d.ts +3 -3
  88. package/dist/index.d.ts +2 -1
  89. package/dist/index.js +2 -1
  90. package/dist/index.js.map +1 -1
  91. package/dist/interceptor/serializable.interceptor.d.ts +4 -2
  92. package/dist/interceptor/serializable.interceptor.js +75 -18
  93. package/dist/interceptor/serializable.interceptor.js.map +1 -1
  94. package/dist/ip/index.d.ts +3 -2
  95. package/dist/ip/index.js +4 -1
  96. package/dist/ip/index.js.map +1 -1
  97. package/dist/ip/request-ip.d.ts +4 -2
  98. package/dist/ip/request-ip.js +92 -30
  99. package/dist/ip/request-ip.js.map +1 -1
  100. package/dist/ip/trusted-proxy-presets.d.ts +3 -0
  101. package/dist/ip/trusted-proxy-presets.js +114 -0
  102. package/dist/ip/trusted-proxy-presets.js.map +1 -0
  103. package/dist/module/configurable-module.builder.js +3 -1
  104. package/dist/module/configurable-module.builder.js.map +1 -1
  105. package/dist/module/index.d.ts +5 -5
  106. package/dist/module/index.js.map +1 -1
  107. package/dist/pagination/index.d.ts +1 -0
  108. package/dist/pagination/index.js +18 -0
  109. package/dist/pagination/index.js.map +1 -0
  110. package/dist/pagination/page.d.ts +39 -0
  111. package/dist/pagination/page.js +87 -0
  112. package/dist/pagination/page.js.map +1 -0
  113. package/dist/serialize/localize.string.js.map +1 -1
  114. package/dist/utils/package-utils.js +3 -0
  115. package/dist/utils/package-utils.js.map +1 -1
  116. package/dist/validation/index.d.ts +1 -0
  117. package/dist/validation/index.js +1 -0
  118. package/dist/validation/index.js.map +1 -1
  119. package/dist/validation/validators.d.ts +3 -0
  120. package/dist/validation/validators.js +29 -0
  121. package/dist/validation/validators.js.map +1 -0
  122. package/docs/20260214-coverage-plan.md +52 -0
  123. package/docs/20260214-fix-plan-common-bugs.md +90 -0
  124. package/docs/20260214-fix-plan-common-cleanup.md +119 -0
  125. package/docs/20260214-fix-plan-exception-redesign.md +274 -0
  126. package/docs/20260214-fix-plan-nestjs-prisma.md +454 -0
  127. package/docs/20260214-fix-plan-pagination-move-and-rename.md +148 -0
  128. package/docs/20260214-review-common.md +426 -0
  129. package/docs/20260215-fix-plan-review-enhancements.md +110 -0
  130. package/docs/20260215-fix-plan-trust-proxy-presets.md +227 -0
  131. package/docs/20260215-review-nestjs-common.md +1015 -0
  132. package/docs/trust-proxy-security.md +475 -0
  133. package/package.json +1 -1
  134. package/dist/exception/app-exception.filter.d.ts +0 -9
  135. package/dist/exception/app-exception.filter.js +0 -52
  136. package/dist/exception/app-exception.filter.js.map +0 -1
  137. package/dist/i18n/i18n.constants.d.ts +0 -0
  138. package/dist/i18n/i18n.constants.js +0 -1
  139. package/dist/i18n/i18n.constants.js.map +0 -1
  140. package/dist/middleware/index.d.ts +0 -4
  141. package/dist/middleware/index.js +0 -14
  142. package/dist/middleware/index.js.map +0 -1
  143. package/dist/middleware/log.provider.d.ts +0 -38
  144. package/dist/middleware/log.provider.js +0 -147
  145. package/dist/middleware/log.provider.js.map +0 -1
  146. package/dist/middleware/logger-option.factory.d.ts +0 -17
  147. package/dist/middleware/logger-option.factory.js +0 -73
  148. package/dist/middleware/logger-option.factory.js.map +0 -1
  149. package/dist/middleware/logger.middleware.d.ts +0 -8
  150. package/dist/middleware/logger.middleware.js +0 -97
  151. package/dist/middleware/logger.middleware.js.map +0 -1
@@ -0,0 +1,227 @@
1
+ # Fix Plan: Trust Proxy Presets + Critical Security Fixes
2
+
3
+ **Date:** 2026-02-15
4
+ **Branch:** feat/monorepo-enterprise-upgrade
5
+ **Package:** `packages/nestjs-common`
6
+
7
+ ## Context
8
+
9
+ Three critical issues from the code review plus a new trusted proxy provider preset system.
10
+
11
+ ## Phase 1: BUG-1 — Fix Object Mutation in SerializableInterceptor
12
+
13
+ ### Fix 1: Deep clone before transforming
14
+ **File:** `lib/interceptor/serializable.interceptor.ts`
15
+ **Impact:** Data corruption when cached/shared objects are serialized multiple times
16
+ **Change:**
17
+ - In `serializeObject()`, deep clone `data` before passing to `transformObject()`
18
+ - Use `structuredClone(data)` (Node 17+, available in our target)
19
+ - This prevents mutation of the original object
20
+
21
+ ```typescript
22
+ serializeObject(obj: PlainLiteralObject, options: ClassTransformOptions, lang: string) {
23
+ const data = obj instanceof Serializable ? instanceToInstance(obj, options) : obj;
24
+ const cloned = structuredClone(data);
25
+ return this.transformToPlain(this.transformObject(cloned, lang), options);
26
+ }
27
+ ```
28
+
29
+ ## Phase 2: SEC-1 + SEC-2 + Provider Presets — Trust Proxy Overhaul
30
+
31
+ ### Fix 2A: SEC-1 — Default trust proxy to disabled
32
+ **File:** `lib/ip/request-ip.ts`
33
+ **Impact:** IP spoofing when trust proxy not explicitly configured
34
+ **Change:**
35
+ - Change default: `let trustProxyConfig: TrustProxyOptions = { enabled: false };`
36
+ - Breaking change for 3.0.0 — apps behind proxies must now explicitly configure
37
+
38
+ ### Fix 2B: SEC-2 — X-Forwarded-For right-to-left parsing
39
+ **File:** `lib/ip/request-ip.ts`
40
+ **Impact:** Attacker-controlled IP returned from X-Forwarded-For
41
+ **Change:**
42
+ - `getClientIpFromXForwardedFor()` — iterate right-to-left, skip trusted proxy IPs
43
+ - Add `trustedProxies` parameter to the function
44
+ - Update `getClientIpByHeader()` to pass trusted proxies through
45
+
46
+ ### Fix 2C: Trusted Proxy Provider Presets
47
+ **File:** `lib/ip/trusted-proxy-presets.ts` (NEW)
48
+ **Change:** Create a map of well-known provider CIDR ranges:
49
+
50
+ ```typescript
51
+ export type TrustedProxyProvider =
52
+ | 'cloudflare'
53
+ | 'aws-alb'
54
+ | 'aws-cloudfront'
55
+ | 'gcp'
56
+ | 'digitalocean'
57
+ | 'alibaba'
58
+ | 'loopback'
59
+ | 'private'
60
+ | 'link-local';
61
+
62
+ export const TRUSTED_PROXY_PRESETS: Record<TrustedProxyProvider, string[]> = {
63
+ loopback: ['127.0.0.0/8', '::1/128'],
64
+ private: [
65
+ '10.0.0.0/8',
66
+ '172.16.0.0/12',
67
+ '192.168.0.0/16',
68
+ 'fc00::/7',
69
+ ],
70
+ 'link-local': ['169.254.0.0/16', 'fe80::/10'],
71
+ cloudflare: [
72
+ // IPv4 — https://www.cloudflare.com/ips-v4 (last updated 2025-01)
73
+ '173.245.48.0/20',
74
+ '103.21.244.0/22',
75
+ '103.22.200.0/22',
76
+ '103.31.4.0/22',
77
+ '141.101.64.0/18',
78
+ '108.162.192.0/18',
79
+ '190.93.240.0/20',
80
+ '188.114.96.0/20',
81
+ '197.234.240.0/22',
82
+ '198.41.128.0/17',
83
+ '162.158.0.0/15',
84
+ '104.16.0.0/13',
85
+ '104.24.0.0/14',
86
+ '172.64.0.0/13',
87
+ '131.0.72.0/22',
88
+ // IPv6 — https://www.cloudflare.com/ips-v6
89
+ '2400:cb00::/32',
90
+ '2606:4700::/32',
91
+ '2803:f800::/32',
92
+ '2405:b500::/32',
93
+ '2405:8100::/32',
94
+ '2a06:98c0::/29',
95
+ '2c0f:f248::/32',
96
+ ],
97
+ 'aws-alb': [
98
+ // AWS ALB sits in your VPC — use private ranges
99
+ '10.0.0.0/8',
100
+ '172.16.0.0/12',
101
+ '192.168.0.0/16',
102
+ ],
103
+ 'aws-cloudfront': [
104
+ // Subset of AWS CloudFront edge IPs — https://ip-ranges.amazonaws.com/ip-ranges.json (CLOUDFRONT service, last updated 2025-01)
105
+ '120.52.22.96/27',
106
+ '205.251.249.0/24',
107
+ '180.163.57.128/26',
108
+ '204.246.168.0/22',
109
+ '111.13.171.128/26',
110
+ '18.160.0.0/15',
111
+ '205.251.252.0/23',
112
+ '54.192.0.0/16',
113
+ '204.246.173.0/24',
114
+ '54.230.0.0/17',
115
+ '54.230.128.0/18',
116
+ '54.230.200.0/21',
117
+ '54.230.208.0/20',
118
+ '54.230.224.0/19',
119
+ '70.132.0.0/18',
120
+ '116.129.226.128/26',
121
+ '130.176.0.0/17',
122
+ '130.176.128.0/18',
123
+ '130.176.192.0/19',
124
+ '130.176.224.0/20',
125
+ '13.32.0.0/15',
126
+ '13.35.0.0/16',
127
+ '13.224.0.0/14',
128
+ '34.195.252.0/24',
129
+ '34.226.14.0/24',
130
+ '13.249.0.0/16',
131
+ '34.232.163.208/29',
132
+ '52.84.0.0/15',
133
+ '64.252.64.0/18',
134
+ '64.252.128.0/18',
135
+ '204.246.164.0/22',
136
+ '204.246.174.0/23',
137
+ '204.246.176.0/20',
138
+ '71.152.0.0/17',
139
+ '3.160.0.0/14',
140
+ '3.164.0.0/18',
141
+ ],
142
+ gcp: [
143
+ // GCP Load Balancers use Google-owned ranges for health checks + proxy
144
+ '35.191.0.0/16',
145
+ '130.211.0.0/22',
146
+ // Internal VPC
147
+ '10.0.0.0/8',
148
+ '172.16.0.0/12',
149
+ '192.168.0.0/16',
150
+ ],
151
+ digitalocean: [
152
+ // DO App Platform / LB — VPC-internal
153
+ '10.0.0.0/8',
154
+ '172.16.0.0/12',
155
+ '192.168.0.0/16',
156
+ ],
157
+ alibaba: [
158
+ // Alibaba Cloud SLB/ALB — VPC-internal + known SLB health check ranges
159
+ '10.0.0.0/8',
160
+ '172.16.0.0/12',
161
+ '192.168.0.0/16',
162
+ '100.64.0.0/10', // Alibaba Cloud internal (SLB health checks, shared services)
163
+ ],
164
+ };
165
+
166
+ /**
167
+ * Resolve provider names to CIDR arrays.
168
+ * Merges multiple providers and deduplicates.
169
+ */
170
+ export function resolveProviderCidrs(providers: TrustedProxyProvider | TrustedProxyProvider[]): string[] {
171
+ const list = Array.isArray(providers) ? providers : [providers];
172
+ const cidrs = new Set<string>();
173
+ for (const provider of list) {
174
+ const preset = TRUSTED_PROXY_PRESETS[provider];
175
+ if (!preset) {
176
+ throw new Error(`Unknown trusted proxy provider: "${provider}". Valid: ${Object.keys(TRUSTED_PROXY_PRESETS).join(', ')}`);
177
+ }
178
+ for (const cidr of preset) {
179
+ cidrs.add(cidr);
180
+ }
181
+ }
182
+ return [...cidrs];
183
+ }
184
+ ```
185
+
186
+ ### Fix 2D: Update TrustProxyOptions interface
187
+ **File:** `lib/ip/request-ip.ts`
188
+ **Change:**
189
+ ```typescript
190
+ export interface TrustProxyOptions {
191
+ enabled?: boolean;
192
+ trustedProxies?: string[];
193
+ /** Well-known proxy provider presets. Merged with trustedProxies. */
194
+ providers?: TrustedProxyProvider | TrustedProxyProvider[];
195
+ }
196
+ ```
197
+
198
+ Update `shouldTrustProxyHeaders()` to resolve providers into CIDRs and merge with `trustedProxies`.
199
+
200
+ ### Fix 2E: Remove module-level mutable state
201
+ **File:** `lib/ip/request-ip.ts`
202
+ **Change:**
203
+ - Move `headerPriority` clone into `mw()` function scope (don't mutate module-level array)
204
+ - Keep `trustProxyConfig` module-level (it's intentionally global config) but document it
205
+
206
+ ### Fix 2F: Update barrel exports
207
+ **File:** `lib/ip/index.ts`
208
+ **Change:** Export `TrustedProxyProvider`, `TRUSTED_PROXY_PRESETS`, `resolveProviderCidrs`
209
+
210
+ **File:** `lib/index.ts`
211
+ **Change:** Ensure ip exports are re-exported
212
+
213
+ ## Test Strategy
214
+ - Mode: two-session-tdd
215
+ - TDD targets:
216
+ - SerializableInterceptor: object mutation test (same object serialized twice with different langs)
217
+ - Trust proxy presets: resolveProviderCidrs, unknown provider error, multiple providers, dedup
218
+ - X-Forwarded-For right-to-left: spoofed IP ignored, rightmost non-proxy IP returned
219
+ - Trust proxy disabled by default: no headers trusted without explicit config
220
+ - Provider + custom merge: providers CIDRs merged with trustedProxies
221
+ - shouldTrustProxyHeaders with providers configured
222
+ - Session 1 scope: test files only in `test/` directory
223
+ - Session 2 scope: implementation files only in `lib/` directory
224
+
225
+ ## Commits
226
+ - Phase 1: `fix(nestjs-common): prevent object mutation in SerializableInterceptor`
227
+ - Phase 2: `feat(nestjs-common): trust proxy provider presets + security fixes (SEC-1, SEC-2)`