yinzerflow 0.4.4 → 0.5.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.
@@ -0,0 +1,280 @@
1
+ # 📖 IP Security
2
+
3
+ YinzerFlow provides comprehensive IP address validation and security protection against IP spoofing attacks, supporting multiple header formats with trusted proxy validation.
4
+
5
+ For detailed configuration examples and patterns, see [Configuration Guide](../configuration/configuration.md).
6
+
7
+ # ⚙️ Usage
8
+
9
+ ## 🎛️ Settings
10
+
11
+ ### trustedProxies — @default <span style="color: #2ecc71">`['127.0.0.1', '::1']`</span>
12
+
13
+ List of trusted proxy IP addresses that can send forwarded headers.
14
+
15
+ ```typescript
16
+ const app = new YinzerFlow({
17
+ ipSecurity: {
18
+ trustedProxies: ['127.0.0.1', '::1', '192.168.1.10'] // Trusted proxy IPs
19
+ }
20
+ });
21
+ ```
22
+
23
+ <aside>
24
+
25
+ Options: `string[]`
26
+
27
+ - `['127.0.0.1', '::1']`: Localhost only (default)
28
+ - `['192.168.1.10']`: Specific load balancer IP
29
+ - `['*']`: Trust any proxy (useful for Kubernetes/unknown infrastructure)
30
+ - `['173.245.48.0/20', '103.21.244.0/22']`: Cloudflare IP ranges
31
+ </aside>
32
+
33
+ ### allowPrivateIps — @default <span style="color: #2ecc71">`true`</span>
34
+
35
+ Allow private IP addresses (RFC 1918, RFC 4193, RFC 3927).
36
+
37
+ ```typescript
38
+ const app = new YinzerFlow({
39
+ ipSecurity: {
40
+ allowPrivateIps: true // Allow private IPs (default)
41
+ }
42
+ });
43
+ ```
44
+
45
+ <aside>
46
+
47
+ Options: `boolean`
48
+
49
+ - `true`: Allow private IPs (default, good for internal networks)
50
+ - `false`: Only allow public IPs (more secure for public APIs)
51
+ </aside>
52
+
53
+ ### headerPreference — @default <span style="color: #2ecc71">`['x-forwarded-for', 'x-real-ip', 'cf-connecting-ip', 'x-client-ip', 'true-client-ip']`</span>
54
+
55
+ Header preference order for IP extraction.
56
+
57
+ ```typescript
58
+ const app = new YinzerFlow({
59
+ ipSecurity: {
60
+ headerPreference: [
61
+ 'x-forwarded-for', // Standard proxy header
62
+ 'x-real-ip', // Nginx header
63
+ 'cf-connecting-ip', // Cloudflare header
64
+ 'x-client-ip', // Alternative header
65
+ 'true-client-ip' // Another alternative
66
+ ]
67
+ }
68
+ });
69
+ ```
70
+
71
+ <aside>
72
+
73
+ Options: `string[]`
74
+
75
+ - `['x-forwarded-for']`: Only X-Forwarded-For (most common)
76
+ - `['cf-connecting-ip', 'x-forwarded-for']`: Cloudflare priority
77
+ - `['x-real-ip']`: Only X-Real-IP (Nginx)
78
+ - `['x-forwarded-for', 'x-real-ip', 'cf-connecting-ip']`: Comprehensive (default)
79
+ </aside>
80
+
81
+ ### maxChainLength — @default <span style="color: #2ecc71">`10`</span>
82
+
83
+ Maximum IP chain length to prevent amplification attacks.
84
+
85
+ ```typescript
86
+ const app = new YinzerFlow({
87
+ ipSecurity: {
88
+ maxChainLength: 10 // Reasonable chain limit
89
+ }
90
+ });
91
+ ```
92
+
93
+ <aside>
94
+
95
+ Options: `number`
96
+
97
+ - `2`: Short chains only (high security)
98
+ - `10`: Standard limit (default)
99
+ - `20`: Longer chains (complex infrastructure)
100
+ </aside>
101
+
102
+ ### detectSpoofing — @default <span style="color: #2ecc71">`true`</span>
103
+
104
+ Enable spoofing pattern detection.
105
+
106
+ ```typescript
107
+ const app = new YinzerFlow({
108
+ ipSecurity: {
109
+ detectSpoofing: true // Enable spoofing detection
110
+ }
111
+ });
112
+ ```
113
+
114
+ <aside>
115
+
116
+ Options: `boolean`
117
+
118
+ - `true`: Detect spoofing patterns (default, recommended)
119
+ - `false`: Disable detection (development only)
120
+ </aside>
121
+
122
+ # ✨ Best Practices
123
+
124
+ - **Configure trusted proxies** for your load balancer/CDN infrastructure
125
+ - **Use specific IPs** instead of wildcards when possible
126
+ - **Enable spoofing detection** in production environments
127
+ - **Set appropriate chain length limits** based on your network topology
128
+ - **Test IP extraction** with your specific infrastructure
129
+ - **Monitor IP validation logs** for suspicious patterns
130
+
131
+ # 💻 Examples
132
+
133
+ ### Production API
134
+
135
+ **Use Case:** High-security API behind Nginx load balancer
136
+
137
+ **Description:** Production-ready IP security configuration with specific trusted proxies, strict validation, and spoofing detection for maximum security.
138
+
139
+ ```typescript
140
+ import { YinzerFlow } from 'yinzerflow';
141
+
142
+ const app = new YinzerFlow({
143
+ port: 3000,
144
+ ipSecurity: {
145
+ trustedProxies: ['192.168.1.10'], // Your Nginx server
146
+ allowPrivateIps: false, // Only real client IPs
147
+ headerPreference: ['x-forwarded-for'], // Nginx uses X-Forwarded-For
148
+ detectSpoofing: true,
149
+ maxChainLength: 2 // Short chains only
150
+ }
151
+ });
152
+
153
+ app.get('/api/user-info', ({ request }) => {
154
+ const clientIp = request.ipAddress; // Validated and secure
155
+
156
+ return {
157
+ message: `Hello from ${clientIp}`,
158
+ userAgent: request.headers['user-agent']
159
+ };
160
+ });
161
+
162
+ await app.listen();
163
+ ```
164
+
165
+ ### Dev API
166
+
167
+ **Use Case:** Development server with relaxed IP validation
168
+
169
+ **Description:** Development configuration with localhost trusted proxies and relaxed settings for easier debugging and testing.
170
+
171
+ ```typescript
172
+ import { YinzerFlow } from 'yinzerflow';
173
+
174
+ const app = new YinzerFlow({
175
+ port: 3000,
176
+ ipSecurity: {
177
+ trustedProxies: ['127.0.0.1', '::1'], // Localhost only
178
+ allowPrivateIps: true, // Allow private IPs in dev
179
+ headerPreference: [ // Comprehensive header support
180
+ 'x-forwarded-for',
181
+ 'x-real-ip',
182
+ 'cf-connecting-ip'
183
+ ],
184
+ detectSpoofing: false, // Disable for development
185
+ maxChainLength: 10 // Standard limit
186
+ }
187
+ });
188
+
189
+ app.get('/api/test', ({ request }) => {
190
+ const clientIp = request.ipAddress;
191
+ return { message: 'Development mode', clientIp };
192
+ });
193
+
194
+ await app.listen();
195
+ ```
196
+
197
+ ## 🚀 Performance Notes
198
+
199
+ - **IP validation**: O(1) constant time for trusted proxy validation
200
+ - **Header processing**: Minimal overhead for standard headers
201
+ - **Spoofing detection**: Lightweight pattern matching
202
+ - **Memory usage**: Negligible impact on request processing
203
+
204
+ ## 🔒 Security Notes
205
+
206
+ YinzerFlow implements several security measures to prevent IP spoofing attacks:
207
+
208
+ ### 🛡️ Trusted Proxy Validation
209
+ - **Problem**: Attackers can spoof `X-Forwarded-For` headers to hide their real IP address or impersonate other clients
210
+ - **YinzerFlow Solution**: Only accepts forwarded headers from explicitly configured trusted proxy IPs, preventing spoofing from untrusted sources
211
+
212
+ ### 🛡️ Multiple Header Support
213
+ - **Problem**: Different proxies and CDNs use different headers (X-Real-IP, CF-Connecting-IP, etc.), making it hard to get consistent client IPs
214
+ - **YinzerFlow Solution**: Configurable header preference order with validation for each header type based on its expected format and source
215
+
216
+ ### 🛡️ IP Format Validation
217
+ - **Problem**: Malformed IP addresses can cause application errors or bypass security controls
218
+ - **YinzerFlow Solution**: Comprehensive IPv4 and IPv6 validation with named capture groups for precise format checking
219
+
220
+ ### 🛡️ Spoofing Pattern Detection
221
+ - **Problem**: Sophisticated attacks use patterns like duplicate IPs, overly long chains, or mixed valid/invalid IPs to confuse parsing
222
+ - **YinzerFlow Solution**: Advanced pattern detection identifies suspicious IP chains and automatically rejects them
223
+
224
+ ### 🛡️ Private IP Filtering
225
+ - **Problem**: Internal network IPs might leak information about network topology or be used in certain attacks
226
+ - **YinzerFlow Solution**: Configurable private IP filtering with RFC 1918, RFC 4193, and RFC 3927 range detection
227
+
228
+ ## 🔧 Troubleshooting
229
+
230
+ ### IP Address Always Empty
231
+ - **Problem**: `request.ipAddress` is always empty
232
+ - **Fix**: Configure trusted proxies for your infrastructure
233
+
234
+ ```typescript
235
+ // ❌ Wrong - no trusted proxies
236
+ ipSecurity: { trustedProxies: [] }
237
+
238
+ // ✅ Correct - configure trusted proxies
239
+ ipSecurity: { trustedProxies: ['127.0.0.1', '192.168.1.10'] }
240
+ ```
241
+
242
+ ### Wrong Client IP Behind Load Balancer
243
+ - **Problem**: Load balancer IP not in trusted proxies
244
+ - **Fix**: Add load balancer IP to trusted proxies
245
+
246
+ ```typescript
247
+ // ❌ Wrong - load balancer IP not trusted
248
+ ipSecurity: { trustedProxies: ['127.0.0.1'] }
249
+ // Load balancer at 192.168.1.10 sends: X-Forwarded-For: 203.0.113.1, 192.168.1.10
250
+
251
+ // ✅ Correct - add load balancer IP
252
+ ipSecurity: { trustedProxies: ['127.0.0.1', '192.168.1.10'] }
253
+ ```
254
+
255
+ ### Private IPs Blocked When Needed
256
+ - **Problem**: `allowPrivateIps: false` blocks internal network IPs
257
+ - **Fix**: Enable private IPs for internal networks
258
+
259
+ ```typescript
260
+ // ❌ Wrong - blocks private IPs
261
+ ipSecurity: { allowPrivateIps: false }
262
+
263
+ // ✅ Correct - allow private IPs for internal networks
264
+ ipSecurity: { allowPrivateIps: true }
265
+ ```
266
+
267
+ ### Cloudflare IP Not Working
268
+ - **Problem**: Cloudflare IP ranges not in trusted proxies
269
+ - **Fix**: Add Cloudflare IP ranges or use wildcard
270
+
271
+ ```typescript
272
+ // ❌ Wrong - Cloudflare IPs not trusted
273
+ ipSecurity: { trustedProxies: ['127.0.0.1'] }
274
+
275
+ // ✅ Correct - add Cloudflare IPs or use wildcard
276
+ ipSecurity: {
277
+ trustedProxies: ['*'], // Trust any proxy
278
+ headerPreference: ['cf-connecting-ip', 'x-forwarded-for']
279
+ }
280
+ ```