yinzerflow 0.1.18 → 0.2.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.
- package/README.md +0 -296
- package/YinzerFlow.d.ts +565 -0
- package/YinzerFlow.js +24 -0
- package/YinzerFlow.js.map +42 -0
- package/docs/advanced-configuration-options.md +175 -0
- package/docs/body-parsing.md +294 -0
- package/docs/cors.md +187 -0
- package/docs/ip-security.md +232 -0
- package/docs/request.md +145 -0
- package/docs/response.md +251 -0
- package/docs/start-here.MD +116 -0
- package/example/index.ts +109 -53
- package/package.json +15 -17
- package/constants/index.d.ts +0 -86
- package/constants/index.js +0 -3
- package/constants/index.js.map +0 -13
- package/docs/README.md +0 -327
- package/docs/content-types.md +0 -390
- package/docs/error-handling.md +0 -266
- package/docs/file-parsers.md +0 -276
- package/docs/hooks.md +0 -289
- package/docs/routing.md +0 -204
- package/example/bun.lock +0 -866
- package/example/hooks/authentication.middleware.ts +0 -77
- package/example/package.json +0 -61
- package/example/routes/authentication.routes.ts +0 -243
- package/example/routes/content-types.ts +0 -116
- package/example/tsconfig.json +0 -32
- package/index.d.ts +0 -395
- package/index.js +0 -23
- package/index.js.map +0 -33
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# Advanced Configuration Options
|
|
2
|
+
|
|
3
|
+
YinzerFlow provides advanced configuration options for fine-tuning security, performance, and functionality. These options allow you to customize the framework's behavior for specific use cases while maintaining robust security defaults.
|
|
4
|
+
|
|
5
|
+
## Body Parser Configuration
|
|
6
|
+
|
|
7
|
+
Body parsing handles all incoming request data with built-in security protections against DoS attacks, prototype pollution, and memory exhaustion vulnerabilities. See [Body Parsing Documentation](./body-parsing.md) for detailed setup, configuration options, and security considerations.
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
const app = new YinzerFlow({
|
|
11
|
+
port: 3000,
|
|
12
|
+
bodyParser: {
|
|
13
|
+
json: {
|
|
14
|
+
maxSize: 262144, // 256KB
|
|
15
|
+
maxDepth: 10,
|
|
16
|
+
allowPrototypeProperties: false, // Security protection
|
|
17
|
+
maxKeys: 1000
|
|
18
|
+
},
|
|
19
|
+
fileUploads: {
|
|
20
|
+
maxFileSize: 10485760, // 10MB per file
|
|
21
|
+
maxFiles: 10,
|
|
22
|
+
allowedExtensions: ['.jpg', '.png', '.pdf']
|
|
23
|
+
},
|
|
24
|
+
urlEncoded: {
|
|
25
|
+
maxSize: 1048576, // 1MB
|
|
26
|
+
maxFields: 1000
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## CORS Configuration
|
|
33
|
+
|
|
34
|
+
Cross-Origin Resource Sharing (CORS) configuration for browser security. See [CORS Documentation](./cors.md) for detailed setup and security considerations.
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
const app = new YinzerFlow({
|
|
38
|
+
port: 3000,
|
|
39
|
+
cors: {
|
|
40
|
+
enabled: true,
|
|
41
|
+
origin: ['https://yourdomain.com'],
|
|
42
|
+
methods: ['GET', 'POST', 'PUT', 'DELETE'],
|
|
43
|
+
allowedHeaders: ['Content-Type', 'Authorization'],
|
|
44
|
+
credentials: true
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## IP Security Configuration
|
|
50
|
+
|
|
51
|
+
IP address validation and spoofing protection for accurate client identification. See [IP Security Documentation](./ip-security.md) for detailed setup, security considerations, and advanced use cases.
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
const app = new YinzerFlow({
|
|
55
|
+
port: 3000,
|
|
56
|
+
ipSecurity: {
|
|
57
|
+
trustedProxies: ['127.0.0.1', '::1', '192.168.1.10'],
|
|
58
|
+
allowPrivateIps: true,
|
|
59
|
+
headerPreference: ['x-forwarded-for', 'x-real-ip', 'cf-connecting-ip'],
|
|
60
|
+
maxChainLength: 10,
|
|
61
|
+
detectSpoofing: true
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Server Configuration
|
|
67
|
+
|
|
68
|
+
### Connection Options
|
|
69
|
+
|
|
70
|
+
Fine-tune server connection behavior and timeouts:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
const app = new YinzerFlow({
|
|
74
|
+
port: 3000,
|
|
75
|
+
host: '0.0.0.0', // Bind address
|
|
76
|
+
// IP extraction is now handled by ipSecurity configuration
|
|
77
|
+
connectionOptions: {
|
|
78
|
+
socketTimeout: 30000, // 30 seconds
|
|
79
|
+
gracefulShutdownTimeout: 30000,
|
|
80
|
+
keepAliveTimeout: 65000,
|
|
81
|
+
headersTimeout: 66000
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
| Option | Type | Default | Description |
|
|
87
|
+
|-----|---|---|---|
|
|
88
|
+
| `socketTimeout` | `number` | `30000` | Socket timeout in milliseconds |
|
|
89
|
+
| `gracefulShutdownTimeout` | `number` | `30000` | Graceful shutdown timeout |
|
|
90
|
+
| `keepAliveTimeout` | `number` | `65000` | Keep-alive timeout |
|
|
91
|
+
| `headersTimeout` | `number` | `66000` | Headers timeout (should be > keep-alive) |
|
|
92
|
+
|
|
93
|
+
### Logging Configuration
|
|
94
|
+
|
|
95
|
+
Control framework logging output:
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
const app = new YinzerFlow({
|
|
99
|
+
port: 3000,
|
|
100
|
+
logLevel: 'info' // 'off', 'error', 'warn', 'info', 'verbose'
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
## Common Configuration Patterns
|
|
107
|
+
|
|
108
|
+
### High-Security API
|
|
109
|
+
```typescript
|
|
110
|
+
const secureApi = new YinzerFlow({
|
|
111
|
+
port: 443,
|
|
112
|
+
bodyParser: {
|
|
113
|
+
json: { maxSize: 32768, maxDepth: 3, maxKeys: 50 }, // Very strict
|
|
114
|
+
fileUploads: { maxFileSize: 0, maxFiles: 0 }, // No uploads
|
|
115
|
+
urlEncoded: { maxSize: 8192, maxFields: 20 } // Minimal forms
|
|
116
|
+
},
|
|
117
|
+
cors: { enabled: false } // No CORS for security
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### File Processing Service
|
|
122
|
+
```typescript
|
|
123
|
+
const fileService = new YinzerFlow({
|
|
124
|
+
port: 3000,
|
|
125
|
+
bodyParser: {
|
|
126
|
+
json: { maxSize: 8192 }, // Minimal JSON for metadata
|
|
127
|
+
fileUploads: {
|
|
128
|
+
maxFileSize: 2147483648, // 2GB files
|
|
129
|
+
maxTotalSize: 10737418240, // 10GB total
|
|
130
|
+
maxFiles: 50,
|
|
131
|
+
allowedExtensions: ['.zip', '.tar', '.gz', '.7z', '.rar']
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Development/Testing Environment
|
|
138
|
+
```typescript
|
|
139
|
+
const devApp = new YinzerFlow({
|
|
140
|
+
port: 3000,
|
|
141
|
+
logLevel: 'verbose', // Verbose logging
|
|
142
|
+
bodyParser: {
|
|
143
|
+
json: { maxSize: 10485760 }, // 10MB for testing
|
|
144
|
+
fileUploads: {
|
|
145
|
+
maxFileSize: 104857600, // 100MB files
|
|
146
|
+
allowedExtensions: [] // Allow all for development
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
cors: {
|
|
150
|
+
enabled: true,
|
|
151
|
+
origin: '*' // Open CORS for dev (⚠️ Never use in production)
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Configuration Best Practices
|
|
157
|
+
|
|
158
|
+
### Security First
|
|
159
|
+
- **Never enable `allowPrototypeProperties`** unless absolutely necessary
|
|
160
|
+
- **Use allowlists over blocklists** for file extensions when possible
|
|
161
|
+
- **Set conservative limits initially** and increase as needed
|
|
162
|
+
- **Enable CORS carefully** with specific origins in production
|
|
163
|
+
|
|
164
|
+
### Performance Optimization
|
|
165
|
+
- **Match limits to your use case** - don't use defaults blindly
|
|
166
|
+
- **Consider memory usage** when setting maxSize limits
|
|
167
|
+
- **Balance security vs. functionality** based on your threat model
|
|
168
|
+
|
|
169
|
+
### Monitoring and Maintenance
|
|
170
|
+
- **Watch for security warnings** in your logs
|
|
171
|
+
- **Monitor actual usage patterns** to optimize limits
|
|
172
|
+
- **Review configuration regularly** as your application evolves
|
|
173
|
+
- **Test edge cases** with your specific limits
|
|
174
|
+
|
|
175
|
+
These advanced configuration options provide fine-grained control over YinzerFlow's behavior while maintaining security best practices and preventing common vulnerabilities.
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
# Body Parsing
|
|
2
|
+
|
|
3
|
+
YinzerFlow provides comprehensive body parsing with built-in security protections against DoS attacks, prototype pollution, and memory exhaustion vulnerabilities. The body parser automatically handles JSON, file uploads, and URL-encoded form data with configurable security limits.
|
|
4
|
+
|
|
5
|
+
## Configuration
|
|
6
|
+
|
|
7
|
+
Body parsing is automatically enabled with secure defaults that protect against common attack vectors:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
import { YinzerFlow } from 'yinzerflow';
|
|
11
|
+
|
|
12
|
+
const app = new YinzerFlow({
|
|
13
|
+
port: 3000,
|
|
14
|
+
bodyParser: {
|
|
15
|
+
json: {
|
|
16
|
+
maxSize: 262144, // 256KB (reasonable for JSON APIs)
|
|
17
|
+
maxDepth: 10,
|
|
18
|
+
allowPrototypeProperties: false, // Blocks prototype pollution
|
|
19
|
+
maxKeys: 1000,
|
|
20
|
+
maxStringLength: 1048576, // 1MB per string
|
|
21
|
+
maxArrayLength: 10000
|
|
22
|
+
},
|
|
23
|
+
fileUploads: {
|
|
24
|
+
maxFileSize: 10485760, // 10MB per file
|
|
25
|
+
maxTotalSize: 52428800, // 50MB total
|
|
26
|
+
maxFiles: 10,
|
|
27
|
+
allowedExtensions: [], // Empty = allow all
|
|
28
|
+
blockedExtensions: ['.exe', '.bat', '.cmd', '.scr', '.pif', '.com'],
|
|
29
|
+
maxFilenameLength: 255
|
|
30
|
+
},
|
|
31
|
+
urlEncoded: {
|
|
32
|
+
maxSize: 1048576, // 1MB
|
|
33
|
+
maxFields: 1000,
|
|
34
|
+
maxFieldNameLength: 100,
|
|
35
|
+
maxFieldLength: 1048576 // 1MB per field
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Configuration Options
|
|
42
|
+
|
|
43
|
+
#### JSON Parser Configuration
|
|
44
|
+
|
|
45
|
+
Controls how JSON request bodies are parsed and validated:
|
|
46
|
+
|
|
47
|
+
| Option | Type | Default | Description |
|
|
48
|
+
|-----|---|---|---|
|
|
49
|
+
| `maxSize` | `number` | `262144` (256KB) | Maximum JSON body size in bytes |
|
|
50
|
+
| `maxDepth` | `number` | `10` | Maximum nesting depth to prevent stack overflow |
|
|
51
|
+
| `allowPrototypeProperties` | `boolean` | `false` | Allow dangerous prototype properties (⚠️ Security Risk) |
|
|
52
|
+
| `maxKeys` | `number` | `1000` | Maximum object keys to prevent memory exhaustion |
|
|
53
|
+
| `maxStringLength` | `number` | `1048576` (1MB) | Maximum string length per property |
|
|
54
|
+
| `maxArrayLength` | `number` | `10000` | Maximum array elements per property |
|
|
55
|
+
|
|
56
|
+
#### File Upload Configuration
|
|
57
|
+
|
|
58
|
+
Controls file upload processing and security:
|
|
59
|
+
|
|
60
|
+
| Option | Type | Default | Description |
|
|
61
|
+
|-----|---|---|---|
|
|
62
|
+
| `maxFileSize` | `number` | `10485760` (10MB) | Maximum size per individual file |
|
|
63
|
+
| `maxTotalSize` | `number` | `52428800` (50MB) | Maximum total upload size per request |
|
|
64
|
+
| `maxFiles` | `number` | `10` | Maximum number of files per request |
|
|
65
|
+
| `allowedExtensions` | `string[]` | `[]` | Allowed file extensions (empty = allow all) |
|
|
66
|
+
| `blockedExtensions` | `string[]` | `['.exe', '.bat', ...]` | Blocked file extensions for security |
|
|
67
|
+
| `maxFilenameLength` | `number` | `255` | Maximum filename length |
|
|
68
|
+
|
|
69
|
+
#### URL-Encoded Form Configuration
|
|
70
|
+
|
|
71
|
+
Controls form data parsing and validation:
|
|
72
|
+
|
|
73
|
+
| Option | Type | Default | Description |
|
|
74
|
+
|-----|---|---|---|
|
|
75
|
+
| `maxSize` | `number` | `1048576` (1MB) | Maximum form data size |
|
|
76
|
+
| `maxFields` | `number` | `1000` | Maximum form fields to prevent DoS |
|
|
77
|
+
| `maxFieldNameLength` | `number` | `100` | Maximum field name length |
|
|
78
|
+
| `maxFieldLength` | `number` | `1048576` (1MB) | Maximum field value length |
|
|
79
|
+
|
|
80
|
+
## Examples
|
|
81
|
+
|
|
82
|
+
#### Strict API Configuration
|
|
83
|
+
```typescript
|
|
84
|
+
const strictApiApp = new YinzerFlow({
|
|
85
|
+
port: 3000,
|
|
86
|
+
bodyParser: {
|
|
87
|
+
json: {
|
|
88
|
+
maxSize: 131072, // 128KB - smaller for strict APIs
|
|
89
|
+
maxDepth: 5, // Shallow nesting only
|
|
90
|
+
allowPrototypeProperties: false, // Always keep false!
|
|
91
|
+
maxKeys: 100, // Fewer keys allowed
|
|
92
|
+
maxStringLength: 10240, // 10KB strings max
|
|
93
|
+
maxArrayLength: 100 // Small arrays only
|
|
94
|
+
},
|
|
95
|
+
fileUploads: {
|
|
96
|
+
maxFileSize: 1048576, // 1MB files only
|
|
97
|
+
maxFiles: 3, // Very few files
|
|
98
|
+
allowedExtensions: ['.jpg', '.png', '.pdf'], // Specific types only
|
|
99
|
+
maxFilenameLength: 50 // Short filenames
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### Media Upload Configuration
|
|
106
|
+
```typescript
|
|
107
|
+
const mediaApp = new YinzerFlow({
|
|
108
|
+
port: 3000,
|
|
109
|
+
bodyParser: {
|
|
110
|
+
json: {
|
|
111
|
+
maxSize: 512000, // 500KB for metadata
|
|
112
|
+
maxDepth: 3, // Simple metadata only
|
|
113
|
+
allowPrototypeProperties: false
|
|
114
|
+
},
|
|
115
|
+
fileUploads: {
|
|
116
|
+
maxFileSize: 104857600, // 100MB per file for media
|
|
117
|
+
maxTotalSize: 524288000, // 500MB total
|
|
118
|
+
maxFiles: 20,
|
|
119
|
+
allowedExtensions: ['.jpg', '.jpeg', '.png', '.gif', '.mp4', '.webm', '.pdf'],
|
|
120
|
+
blockedExtensions: [], // Using allowlist instead
|
|
121
|
+
maxFilenameLength: 200
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
#### High-Security Configuration
|
|
128
|
+
```typescript
|
|
129
|
+
const secureApp = new YinzerFlow({
|
|
130
|
+
port: 443,
|
|
131
|
+
bodyParser: {
|
|
132
|
+
json: {
|
|
133
|
+
maxSize: 32768, // 32KB only
|
|
134
|
+
maxDepth: 3,
|
|
135
|
+
maxKeys: 50
|
|
136
|
+
},
|
|
137
|
+
fileUploads: {
|
|
138
|
+
maxFileSize: 0, // No file uploads
|
|
139
|
+
maxFiles: 0
|
|
140
|
+
},
|
|
141
|
+
urlEncoded: {
|
|
142
|
+
maxSize: 8192, // 8KB forms only
|
|
143
|
+
maxFields: 20
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Common Use Cases
|
|
150
|
+
|
|
151
|
+
- **API Data Processing**: Parse JSON payloads with automatic security validation and DoS protection
|
|
152
|
+
- **File Upload Handling**: Secure file upload processing with size, type, and count restrictions
|
|
153
|
+
- **Form Data Processing**: Handle HTML form submissions with field validation and memory protection
|
|
154
|
+
- **Content Type Detection**: Automatic parsing based on Content-Type headers with fallback handling
|
|
155
|
+
- **Security Compliance**: Built-in protection against common web vulnerabilities and attack vectors
|
|
156
|
+
- **Memory Protection**: Prevent DoS attacks through configurable size and complexity limits
|
|
157
|
+
|
|
158
|
+
## API Reference
|
|
159
|
+
|
|
160
|
+
### Request Body Properties
|
|
161
|
+
|
|
162
|
+
When body parsing is successful, the parsed data is available on `request.body`:
|
|
163
|
+
|
|
164
|
+
#### JSON Requests
|
|
165
|
+
```typescript
|
|
166
|
+
// Content-Type: application/json
|
|
167
|
+
request.body: unknown // Parsed JSON object or array
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
#### File Upload Requests
|
|
171
|
+
```typescript
|
|
172
|
+
// Content-Type: multipart/form-data
|
|
173
|
+
request.body: {
|
|
174
|
+
fields: Record<string, string | string[]>; // Form fields
|
|
175
|
+
files: Array<{
|
|
176
|
+
fieldname: string;
|
|
177
|
+
filename: string;
|
|
178
|
+
mimetype: string;
|
|
179
|
+
size: number;
|
|
180
|
+
buffer: Buffer;
|
|
181
|
+
}>;
|
|
182
|
+
}
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
#### URL-Encoded Form Requests
|
|
186
|
+
```typescript
|
|
187
|
+
// Content-Type: application/x-www-form-urlencoded
|
|
188
|
+
request.body: Record<string, string | string[]> // Parsed form data
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Configuration Methods
|
|
192
|
+
|
|
193
|
+
Body parser configuration is set during YinzerFlow initialization:
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
const app = new YinzerFlow({
|
|
197
|
+
bodyParser: {
|
|
198
|
+
json: JsonParserOptions,
|
|
199
|
+
fileUploads: FileUploadOptions,
|
|
200
|
+
urlEncoded: UrlEncodedOptions
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## Configuration Validation and Warnings
|
|
206
|
+
|
|
207
|
+
YinzerFlow validates all body parser configuration options and provides security warnings for potentially risky settings:
|
|
208
|
+
|
|
209
|
+
### Automatic Validation
|
|
210
|
+
- **Minimum limits**: Prevents broken configurations (e.g., maxSize: 0)
|
|
211
|
+
- **Type checking**: Ensures proper data types for all options
|
|
212
|
+
- **Logical validation**: Checks for contradictory settings
|
|
213
|
+
|
|
214
|
+
### Security Warnings
|
|
215
|
+
YinzerFlow will log warnings (but not block) potentially risky configurations:
|
|
216
|
+
|
|
217
|
+
```typescript
|
|
218
|
+
// This will trigger security warnings
|
|
219
|
+
const riskyApp = new YinzerFlow({
|
|
220
|
+
bodyParser: {
|
|
221
|
+
json: {
|
|
222
|
+
maxSize: 52428800, // 50MB JSON - warning about memory risk
|
|
223
|
+
allowPrototypeProperties: true, // Warning about prototype pollution
|
|
224
|
+
maxDepth: 100 // Warning about stack overflow risk
|
|
225
|
+
},
|
|
226
|
+
fileUploads: {
|
|
227
|
+
maxFileSize: 1073741824, // 1GB files - warning about resources
|
|
228
|
+
allowedExtensions: ['.exe'] // Warning about dangerous file types
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
## Error Handling
|
|
235
|
+
|
|
236
|
+
YinzerFlow automatically handles body parsing errors and provides clear error messages:
|
|
237
|
+
|
|
238
|
+
**JSON parsing errors:**
|
|
239
|
+
- `Invalid JSON in request body: Unexpected token at position X`
|
|
240
|
+
- `JSON body too large: maximum 256KB allowed`
|
|
241
|
+
- `JSON nesting too deep: maximum 10 levels allowed`
|
|
242
|
+
- `JSON object has too many keys: maximum 1000 allowed`
|
|
243
|
+
|
|
244
|
+
**File upload errors:**
|
|
245
|
+
- `File too large: maximum 10MB per file allowed`
|
|
246
|
+
- `Too many files: maximum 10 files per request`
|
|
247
|
+
- `File type not allowed: .exe files are blocked`
|
|
248
|
+
- `Total upload size exceeded: maximum 50MB total allowed`
|
|
249
|
+
|
|
250
|
+
**URL-encoded form errors:**
|
|
251
|
+
- `Form data too large: maximum 1MB allowed`
|
|
252
|
+
- `Too many form fields: maximum 1000 fields allowed`
|
|
253
|
+
- `Form field name too long: maximum 100 characters`
|
|
254
|
+
- `Form field value too large: maximum 1MB per field`
|
|
255
|
+
|
|
256
|
+
These errors automatically result in appropriate HTTP status codes (400 Bad Request) and prevent malformed or malicious requests from reaching your application handlers.
|
|
257
|
+
|
|
258
|
+
## Security Considerations
|
|
259
|
+
|
|
260
|
+
YinzerFlow implements comprehensive security measures to prevent body parsing vulnerabilities:
|
|
261
|
+
|
|
262
|
+
### 🛡️ JSON DoS Attack Prevention
|
|
263
|
+
- **Problem**: Large or deeply nested JSON can cause memory exhaustion and stack overflow attacks
|
|
264
|
+
- **YinzerFlow Solution**: Configurable size limits, nesting depth limits, key count restrictions, and string/array length limits prevent resource exhaustion
|
|
265
|
+
|
|
266
|
+
### 🛡️ Prototype Pollution Protection
|
|
267
|
+
- **Problem**: Malicious JSON can pollute JavaScript prototypes using `__proto__`, `constructor`, and `prototype` properties
|
|
268
|
+
- **YinzerFlow Solution**: Blocks dangerous properties by default with `allowPrototypeProperties: false` and validates all object keys
|
|
269
|
+
|
|
270
|
+
### 🛡️ File Upload Security
|
|
271
|
+
- **Problem**: Malicious file uploads can execute code, consume server resources, or bypass security controls
|
|
272
|
+
- **YinzerFlow Solution**: File type filtering, size limits, filename validation, and extension-based security controls
|
|
273
|
+
|
|
274
|
+
### 🛡️ Memory Exhaustion Protection
|
|
275
|
+
- **Problem**: Large form data, arrays, or objects can exhaust server memory and cause crashes
|
|
276
|
+
- **YinzerFlow Solution**: Configurable limits on strings, arrays, fields, object keys, and total request sizes
|
|
277
|
+
|
|
278
|
+
### 🛡️ Request Size Validation
|
|
279
|
+
- **Problem**: Extremely large requests can cause DoS through resource exhaustion
|
|
280
|
+
- **YinzerFlow Solution**: Content-type specific size limits with early validation before full parsing
|
|
281
|
+
|
|
282
|
+
### 🛡️ Malformed Data Handling
|
|
283
|
+
- **Problem**: Invalid or malformed data can crash parsers, cause security issues, or bypass validation
|
|
284
|
+
- **YinzerFlow Solution**: Graceful error handling with detailed security context and safe fallback parsing
|
|
285
|
+
|
|
286
|
+
### 🛡️ Filename Injection Prevention
|
|
287
|
+
- **Problem**: Malicious filenames can contain path traversal attacks or dangerous characters
|
|
288
|
+
- **YinzerFlow Solution**: Filename length validation, character sanitization, and path traversal prevention
|
|
289
|
+
|
|
290
|
+
### 🛡️ MIME Type Validation
|
|
291
|
+
- **Problem**: Spoofed or incorrect MIME types can bypass security filters
|
|
292
|
+
- **YinzerFlow Solution**: Content-Type header validation and file extension cross-checking for uploaded files
|
|
293
|
+
|
|
294
|
+
These security measures ensure YinzerFlow's body parsing implementation follows security best practices and prevents common attack vectors while maintaining spec compliance.
|
package/docs/cors.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# CORS (Cross-Origin Resource Sharing)
|
|
2
|
+
|
|
3
|
+
YinzerFlow provides built-in CORS support to handle cross-origin requests securely and efficiently.
|
|
4
|
+
|
|
5
|
+
## Configuration
|
|
6
|
+
|
|
7
|
+
Configure CORS in your YinzerFlow setup:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
import { YinzerFlow } from 'yinzerflow';
|
|
11
|
+
|
|
12
|
+
const app = new YinzerFlow({
|
|
13
|
+
cors: {
|
|
14
|
+
enabled: true,
|
|
15
|
+
origin: 'https://myapp.com',
|
|
16
|
+
credentials: true,
|
|
17
|
+
methods: ['GET', 'POST', 'PUT', 'DELETE'],
|
|
18
|
+
allowedHeaders: ['Content-Type', 'Authorization'],
|
|
19
|
+
exposedHeaders: ['X-Total-Count'],
|
|
20
|
+
maxAge: 86400,
|
|
21
|
+
optionsSuccessStatus: 204,
|
|
22
|
+
preflightContinue: false,
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Configuration Options
|
|
28
|
+
|
|
29
|
+
| Option | Type | Default | Description |
|
|
30
|
+
|--------|------|---------|-------------|
|
|
31
|
+
| `enabled` | `boolean` | `false` | Enable/disable CORS |
|
|
32
|
+
| `origin` | `string \| string[] \| RegExp \| function \| '*'` | `'*'` | Allowed origins |
|
|
33
|
+
| `credentials` | `boolean` | `false` | Allow credentials (cookies, auth headers) |
|
|
34
|
+
| `methods` | `string[]` | `['GET', 'POST', 'PUT', 'DELETE']` | Allowed HTTP methods |
|
|
35
|
+
| `allowedHeaders` | `string \| string[]` | `['Content-Type', 'Authorization']` | Allowed request headers |
|
|
36
|
+
| `exposedHeaders` | `string[]` | `[]` | Headers exposed to client |
|
|
37
|
+
| `maxAge` | `number` | `86400` | Preflight cache duration (seconds) |
|
|
38
|
+
| `optionsSuccessStatus` | `number` | `204` | Status code for successful OPTIONS |
|
|
39
|
+
| `preflightContinue` | `boolean` | `false` | Pass control to next handler after preflight |
|
|
40
|
+
|
|
41
|
+
## Common Use Cases
|
|
42
|
+
|
|
43
|
+
- **Secure Web Applications**: Configure CORS for authenticated browser apps with specific domains and credentials
|
|
44
|
+
- **Public APIs**: Enable broad access for public data APIs while maintaining security controls
|
|
45
|
+
- **Development Environments**: Set up flexible CORS for local development with multiple frontend ports
|
|
46
|
+
- **Microservices**: Configure cross-service communication with controlled origin validation
|
|
47
|
+
- **Mobile App Backends**: Handle native mobile app requests with appropriate CORS settings
|
|
48
|
+
- **Third-Party Integrations**: Allow controlled access from partner domains with validation functions
|
|
49
|
+
|
|
50
|
+
## Origin Configuration Examples
|
|
51
|
+
|
|
52
|
+
### Wildcard (Public APIs)
|
|
53
|
+
```typescript
|
|
54
|
+
cors: {
|
|
55
|
+
enabled: true,
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Single Domain
|
|
60
|
+
```typescript
|
|
61
|
+
cors: {
|
|
62
|
+
enabled: true,
|
|
63
|
+
origin: 'https://myapp.com',
|
|
64
|
+
credentials: true,
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Multiple Domains
|
|
69
|
+
```typescript
|
|
70
|
+
cors: {
|
|
71
|
+
enabled: true,
|
|
72
|
+
origin: [
|
|
73
|
+
'https://myapp.com',
|
|
74
|
+
'https://admin.myapp.com',
|
|
75
|
+
'https://mobile.myapp.com'
|
|
76
|
+
],
|
|
77
|
+
credentials: true,
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### RegExp Pattern
|
|
82
|
+
```typescript
|
|
83
|
+
cors: {
|
|
84
|
+
enabled: true,
|
|
85
|
+
origin: /^https:\/\/.*\.myapp\.com$/,
|
|
86
|
+
credentials: true,
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Dynamic Function
|
|
91
|
+
```typescript
|
|
92
|
+
cors: {
|
|
93
|
+
enabled: true,
|
|
94
|
+
origin: (origin, request): boolean => {
|
|
95
|
+
// Custom validation logic
|
|
96
|
+
const allowedDomains = ['myapp.com', 'partner.com'];
|
|
97
|
+
if (!origin) return false; // Reject: no origin header
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
const url = new URL(origin);
|
|
101
|
+
return allowedDomains.includes(url.hostname); // true = allow, false = reject
|
|
102
|
+
} catch {
|
|
103
|
+
return false; // Reject: malformed URL
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
credentials: true,
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
**Function Return Values:**
|
|
111
|
+
- `true` - Allow the origin (request proceeds with CORS headers)
|
|
112
|
+
- `false` - Reject the origin (403 Forbidden response)
|
|
113
|
+
|
|
114
|
+
## Request Flow
|
|
115
|
+
|
|
116
|
+
### Simple Requests
|
|
117
|
+
For simple requests (GET, POST with basic content types), CORS headers are added directly:
|
|
118
|
+
|
|
119
|
+
```
|
|
120
|
+
Client Request:
|
|
121
|
+
Origin: https://myapp.com
|
|
122
|
+
|
|
123
|
+
Server Response:
|
|
124
|
+
Access-Control-Allow-Origin: https://myapp.com
|
|
125
|
+
Access-Control-Allow-Credentials: true
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Preflight Requests
|
|
129
|
+
For complex requests, browsers send an OPTIONS preflight:
|
|
130
|
+
|
|
131
|
+
```
|
|
132
|
+
Client Preflight:
|
|
133
|
+
OPTIONS /api/data HTTP/1.1
|
|
134
|
+
Origin: https://myapp.com
|
|
135
|
+
Access-Control-Request-Method: PUT
|
|
136
|
+
Access-Control-Request-Headers: Content-Type, Authorization
|
|
137
|
+
|
|
138
|
+
Server Preflight Response:
|
|
139
|
+
HTTP/1.1 204 No Content
|
|
140
|
+
Access-Control-Allow-Origin: https://myapp.com
|
|
141
|
+
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
|
|
142
|
+
Access-Control-Allow-Headers: Content-Type, Authorization
|
|
143
|
+
Access-Control-Allow-Credentials: true
|
|
144
|
+
Access-Control-Max-Age: 86400
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Error Handling
|
|
148
|
+
|
|
149
|
+
YinzerFlow automatically handles CORS errors
|
|
150
|
+
|
|
151
|
+
## Security Considerations
|
|
152
|
+
|
|
153
|
+
YinzerFlow implements several security measures to prevent common CORS vulnerabilities:
|
|
154
|
+
|
|
155
|
+
### 🛡️ Origin Validation Enforcement
|
|
156
|
+
- **Problem**: Many frameworks validate origins but don't enforce the validation result
|
|
157
|
+
- **YinzerFlow Solution**: Origin validation results are actually used - unauthorized requests get 403 Forbidden
|
|
158
|
+
|
|
159
|
+
### 🛡️ No Origin Echo-back for Wildcards
|
|
160
|
+
- **Problem**: Some implementations echo back the request origin when using wildcards, defeating CORS protection
|
|
161
|
+
- **YinzerFlow Solution**: Wildcard origins always return literal `'*'`, never echo back request origins
|
|
162
|
+
|
|
163
|
+
### 🛡️ Spec Compliance Enforcement
|
|
164
|
+
- **Problem**: CORS spec forbids `origin: '*'` with `credentials: true`, but many frameworks allow this dangerous combination
|
|
165
|
+
- **YinzerFlow Solution**: This combination throws a security error at startup, preventing deployment of vulnerable configurations
|
|
166
|
+
|
|
167
|
+
### 🛡️ Proper Preflight Rejection
|
|
168
|
+
- **Problem**: Some frameworks set CORS headers even for rejected requests, or let request handlers override CORS rejections
|
|
169
|
+
- **YinzerFlow Solution**: Unauthorized preflight requests get 403 with no CORS headers, and the rejection cannot be overridden
|
|
170
|
+
|
|
171
|
+
### 🛡️ Case-Insensitive Origin Matching
|
|
172
|
+
- **Problem**: Inconsistent case handling can lead to bypass attempts
|
|
173
|
+
- **YinzerFlow Solution**: All origin validation is case-insensitive but preserves original case in responses
|
|
174
|
+
|
|
175
|
+
### 🛡️ Malformed Origin Protection
|
|
176
|
+
- **Problem**: Malformed origins (like `javascript:`, `data:`, or invalid URLs) can cause security issues
|
|
177
|
+
- **YinzerFlow Solution**: All malformed origins are safely rejected with 403 status
|
|
178
|
+
|
|
179
|
+
### 🛡️ Function Validation Safety
|
|
180
|
+
- **Problem**: Custom origin validation functions might throw errors or behave unpredictably
|
|
181
|
+
- **YinzerFlow Solution**: Function results are safely coerced to boolean, preventing exceptions from bypassing security
|
|
182
|
+
|
|
183
|
+
### 🛡️ No Information Leakage
|
|
184
|
+
- **Problem**: Error responses might leak information about internal origins or configurations
|
|
185
|
+
- **YinzerFlow Solution**: Rejection responses only include the rejected origin, no internal configuration details
|
|
186
|
+
|
|
187
|
+
These security measures ensure YinzerFlow's CORS implementation follows security best practices and prevents common attack vectors while maintaining spec compliance. If you come up with any other security implementation's that hadn't been addressed, Please open a PR and follow our contribution guides.
|