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.
- package/README.md +31 -26
- package/docs/configuration/configuration.md +815 -0
- package/docs/core/core-concepts.md +801 -0
- package/docs/core/error-handling.md +391 -153
- package/docs/core/logging.md +426 -68
- package/docs/modules/body-parsing.md +561 -0
- package/docs/modules/cors.md +369 -0
- package/docs/modules/index.md +125 -0
- package/docs/modules/ip-security.md +280 -0
- package/docs/modules/rate-limiting.md +795 -0
- package/index.d.ts +278 -76
- package/index.js +18 -18
- package/index.js.map +17 -8
- package/package.json +5 -3
- package/docs/configuration/advanced-configuration-options.md +0 -302
- package/docs/configuration/configuration-patterns.md +0 -500
- package/docs/core/context.md +0 -230
- package/docs/core/examples.md +0 -444
- package/docs/core/request.md +0 -161
- package/docs/core/response.md +0 -212
- package/docs/core/routes.md +0 -720
- package/docs/quick-reference.md +0 -346
- package/docs/security/body-parsing.md +0 -296
- package/docs/security/cors.md +0 -189
- package/docs/security/ip-security.md +0 -234
- package/docs/security/security-overview.md +0 -282
- package/docs/start-here.md +0 -184
package/docs/core/request.md
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
# Request Object
|
|
2
|
-
|
|
3
|
-
YinzerFlow provides a comprehensive request object containing parsed headers, body, query parameters, route parameters, and metadata with built-in security protections and validation.
|
|
4
|
-
|
|
5
|
-
## Configuration
|
|
6
|
-
|
|
7
|
-
Request parsing is automatically enabled and requires no configuration for basic usage:
|
|
8
|
-
|
|
9
|
-
```typescript
|
|
10
|
-
import { YinzerFlow } from 'yinzerflow';
|
|
11
|
-
|
|
12
|
-
const app = new YinzerFlow({ port: 3000 });
|
|
13
|
-
|
|
14
|
-
// Request object is automatically parsed and available in handlers
|
|
15
|
-
app.get('/api/data', ({ request }) => {
|
|
16
|
-
const userAgent = request.headers['user-agent'];
|
|
17
|
-
const queryParam = request.query.search;
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
message: 'Request parsed successfully',
|
|
21
|
-
userAgent,
|
|
22
|
-
queryParam
|
|
23
|
-
};
|
|
24
|
-
});
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
### Configuration Options
|
|
28
|
-
|
|
29
|
-
YinzerFlow's request parsing includes built-in security limits that are automatically applied:
|
|
30
|
-
|
|
31
|
-
| Security Limit | Default Value | Description |
|
|
32
|
-
|-----|---|---|
|
|
33
|
-
| `MAX_HEADERS` | `100` | Maximum number of headers per request |
|
|
34
|
-
| `MAX_HEADER_NAME_LENGTH` | `200` | Maximum length of header names |
|
|
35
|
-
| `MAX_HEADER_VALUE_LENGTH` | `8192` | Maximum length of header values |
|
|
36
|
-
|
|
37
|
-
These limits are built into the framework and cannot be disabled, ensuring consistent security across all YinzerFlow applications.
|
|
38
|
-
|
|
39
|
-
## Examples
|
|
40
|
-
|
|
41
|
-
### Basic Example
|
|
42
|
-
|
|
43
|
-
See [Request Access Pattern](./examples.md#request-access-pattern) for a complete example showing how to access request properties.
|
|
44
|
-
|
|
45
|
-
### Body Parsing Example
|
|
46
|
-
|
|
47
|
-
YinzerFlow automatically parses request bodies (JSON, file uploads, forms) with built-in security protections. Parsed data is available on `request.body` - see [Body Parsing Documentation](../security/body-parsing.md) for detailed configuration options, examples, and security considerations.
|
|
48
|
-
|
|
49
|
-
```typescript
|
|
50
|
-
app.post('/api/users', ({ request, response }) => {
|
|
51
|
-
// Body is automatically parsed based on Content-Type
|
|
52
|
-
const userData = request.body;
|
|
53
|
-
|
|
54
|
-
return {
|
|
55
|
-
message: 'User created successfully',
|
|
56
|
-
data: userData
|
|
57
|
-
};
|
|
58
|
-
});
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
### Raw Body Access Example
|
|
62
|
-
|
|
63
|
-
For advanced use cases where you need to manually parse the request body, YinzerFlow provides access to the raw body via `request.rawBody`. This is useful when:
|
|
64
|
-
|
|
65
|
-
- You need to implement custom parsing logic
|
|
66
|
-
- You want to validate the raw body before parsing
|
|
67
|
-
- You're working with unsupported content types
|
|
68
|
-
- You need to implement custom security validations
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
|
-
app.post('/api/custom-parser', ({ request }) => {
|
|
72
|
-
// Access the raw body as string or Buffer
|
|
73
|
-
const rawBody = request.rawBody;
|
|
74
|
-
|
|
75
|
-
// Implement custom parsing logic
|
|
76
|
-
if (typeof rawBody === 'string') {
|
|
77
|
-
// Custom string parsing
|
|
78
|
-
const customData = parseCustomFormat(rawBody);
|
|
79
|
-
return { parsed: customData };
|
|
80
|
-
} else if (Buffer.isBuffer(rawBody)) {
|
|
81
|
-
// Custom binary parsing
|
|
82
|
-
const binaryData = parseBinaryFormat(rawBody);
|
|
83
|
-
return { parsed: binaryData };
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return { error: 'Unsupported body format' };
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
// Example: Custom XML parser
|
|
90
|
-
app.post('/api/xml', ({ request }) => {
|
|
91
|
-
const rawBody = request.rawBody;
|
|
92
|
-
|
|
93
|
-
if (typeof rawBody === 'string') {
|
|
94
|
-
// Parse XML manually
|
|
95
|
-
const xmlData = parseXML(rawBody);
|
|
96
|
-
return { xml: xmlData };
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return { error: 'Expected string body for XML' };
|
|
100
|
-
});
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
**Note**: The `rawBody` property contains the unprocessed request body as either a `string` or `Buffer`, depending on the content type and framework processing. Always validate and sanitize raw body data before processing to prevent security vulnerabilities.
|
|
104
|
-
|
|
105
|
-
## Common Use Cases
|
|
106
|
-
|
|
107
|
-
- **Authentication**: Extract and validate Bearer tokens, API keys, and session headers
|
|
108
|
-
- **Content Negotiation**: Handle Accept, Accept-Encoding, and Content-Type headers for proper response formatting
|
|
109
|
-
- **File Uploads**: Process multipart form data with automatic parsing and validation
|
|
110
|
-
- **API Filtering**: Use query parameters for pagination, sorting, and filtering
|
|
111
|
-
- **Route Parameters**: Extract dynamic URL segments with automatic parsing
|
|
112
|
-
- **Request Logging**: Access client IP, user agent, and request metadata for logging
|
|
113
|
-
|
|
114
|
-
## Error Handling
|
|
115
|
-
|
|
116
|
-
YinzerFlow automatically handles request parsing errors and provides clear error messages:
|
|
117
|
-
|
|
118
|
-
**Header-related errors:**
|
|
119
|
-
- `Too many headers: maximum 100 allowed`
|
|
120
|
-
- `Invalid header name: Invalid@Header`
|
|
121
|
-
- `Header name too long: maximum 200 characters allowed`
|
|
122
|
-
- `Header value too long: maximum 8192 characters allowed`
|
|
123
|
-
- `Header value contains invalid control characters`
|
|
124
|
-
|
|
125
|
-
**Body parsing errors:** See [Body Parsing Documentation](../security/body-parsing.md) for detailed error handling
|
|
126
|
-
|
|
127
|
-
These errors automatically result in appropriate HTTP status codes (400 Bad Request) and prevent malformed requests from reaching your application handlers.
|
|
128
|
-
|
|
129
|
-
## Security Considerations
|
|
130
|
-
|
|
131
|
-
YinzerFlow implements several security measures to prevent common request-based vulnerabilities:
|
|
132
|
-
|
|
133
|
-
### 🛡️ RFC 7230 Header Compliance
|
|
134
|
-
- **Problem**: Invalid header names can bypass security filters and cause parsing inconsistencies
|
|
135
|
-
- **YinzerFlow Solution**: Strict validation against RFC 7230 specification - only valid header characters are allowed (letters, digits, hyphens, and specific symbols)
|
|
136
|
-
|
|
137
|
-
### 🛡️ DoS Protection Through Limits
|
|
138
|
-
- **Problem**: Unlimited headers, names, or values can cause memory exhaustion and server crashes
|
|
139
|
-
- **YinzerFlow Solution**: Built-in limits prevent abuse: maximum 100 headers, 200-character names, and 8KB values
|
|
140
|
-
|
|
141
|
-
### 🛡️ Control Character Sanitization
|
|
142
|
-
- **Problem**: Control characters in headers can cause parsing errors and bypass security filters
|
|
143
|
-
- **YinzerFlow Solution**: Automatic removal of dangerous control characters while preserving legitimate characters like horizontal tabs
|
|
144
|
-
|
|
145
|
-
### 🛡️ Body Parsing Protection
|
|
146
|
-
- **Problem**: Malformed request bodies can cause parsing errors, memory exhaustion, or security vulnerabilities
|
|
147
|
-
- **YinzerFlow Solution**: Comprehensive body parsing security with size limits, validation, and protection against common attacks - see [Body Parsing Documentation](./body-parsing.md)
|
|
148
|
-
|
|
149
|
-
### 🛡️ Parameter Pollution Prevention
|
|
150
|
-
- **Problem**: Duplicate query parameters can bypass validation or cause unexpected behavior
|
|
151
|
-
- **YinzerFlow Solution**: Consistent parameter handling - last value wins for duplicates, preventing pollution attacks
|
|
152
|
-
|
|
153
|
-
### 🛡️ Path Traversal Protection
|
|
154
|
-
- **Problem**: Directory traversal attacks through URL paths can access unauthorized files
|
|
155
|
-
- **YinzerFlow Solution**: Comprehensive path normalization and validation prevents traversal attempts while maintaining route functionality
|
|
156
|
-
|
|
157
|
-
### 🛡️ IP Address Validation
|
|
158
|
-
- **Problem**: Spoofed proxy headers can bypass IP-based security controls
|
|
159
|
-
- **YinzerFlow Solution**: Secure IP address extraction with proxy header validation prevents spoofing attacks
|
|
160
|
-
|
|
161
|
-
These security measures ensure YinzerFlow's request implementation follows security best practices and prevents common attack vectors while maintaining RFC compliance and performance.
|
package/docs/core/response.md
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
# Response Object
|
|
2
|
-
|
|
3
|
-
YinzerFlow provides a powerful response object for sending HTTP responses with automatic content type detection, header validation, and built-in security protections.
|
|
4
|
-
|
|
5
|
-
## Configuration
|
|
6
|
-
|
|
7
|
-
Response handling is automatically enabled and requires no configuration for basic usage:
|
|
8
|
-
|
|
9
|
-
```typescript
|
|
10
|
-
import { YinzerFlow } from 'yinzerflow';
|
|
11
|
-
|
|
12
|
-
const app = new YinzerFlow({ port: 3000 });
|
|
13
|
-
|
|
14
|
-
// Response object is automatically available in handlers
|
|
15
|
-
app.get('/api/data', ({ response }) => {
|
|
16
|
-
// Set status code
|
|
17
|
-
response.setStatusCode(200);
|
|
18
|
-
|
|
19
|
-
// Add headers
|
|
20
|
-
response.addHeaders({
|
|
21
|
-
'Content-Type': 'application/json',
|
|
22
|
-
'Cache-Control': 'no-cache'
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
return { message: 'Response sent successfully' };
|
|
26
|
-
});
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
YinzerFlow's response handling includes built-in security protections that are automatically applied to prevent common vulnerabilities, including automatic security headers on all responses.
|
|
30
|
-
|
|
31
|
-
## Basic Example
|
|
32
|
-
|
|
33
|
-
See [Response Control Pattern](./examples.md#response-control-pattern) for a complete example showing how to control the response.
|
|
34
|
-
|
|
35
|
-
## Response Body Handling
|
|
36
|
-
|
|
37
|
-
YinzerFlow automatically processes response bodies based on their type:
|
|
38
|
-
|
|
39
|
-
### JSON Objects and Arrays
|
|
40
|
-
|
|
41
|
-
Objects and arrays are automatically JSON stringified with `Content-Type: application/json`:
|
|
42
|
-
|
|
43
|
-
```typescript
|
|
44
|
-
// Automatically becomes JSON
|
|
45
|
-
return { message: 'Hello', data: [1, 2, 3] };
|
|
46
|
-
// Content-Type: application/json
|
|
47
|
-
// Body: {"message":"Hello","data":[1,2,3]}
|
|
48
|
-
|
|
49
|
-
return [{ id: 1 }, { id: 2 }];
|
|
50
|
-
// Content-Type: application/json
|
|
51
|
-
// Body: [{"id":1},{"id":2}]
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
### Strings
|
|
55
|
-
|
|
56
|
-
String responses get intelligent content type detection:
|
|
57
|
-
|
|
58
|
-
```typescript
|
|
59
|
-
// JSON string
|
|
60
|
-
return '{"message": "Hello"}';
|
|
61
|
-
// Content-Type: application/json
|
|
62
|
-
|
|
63
|
-
// Form data
|
|
64
|
-
return 'name=John&email=john@example.com';
|
|
65
|
-
// Content-Type: application/x-www-form-urlencoded
|
|
66
|
-
|
|
67
|
-
// Plain text
|
|
68
|
-
return 'Hello, world!';
|
|
69
|
-
// Content-Type: text/plain
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### Binary Data
|
|
73
|
-
|
|
74
|
-
Binary data is handled with appropriate encoding:
|
|
75
|
-
|
|
76
|
-
```typescript
|
|
77
|
-
// Buffer/Uint8Array/ArrayBuffer
|
|
78
|
-
const imageBuffer = Buffer.from(imageData);
|
|
79
|
-
return imageBuffer;
|
|
80
|
-
// Content-Type: application/octet-stream (for binary)
|
|
81
|
-
// Content-Type: text/plain (for text-like content)
|
|
82
|
-
|
|
83
|
-
// Base64 encoding for true binary
|
|
84
|
-
response.addHeaders({ 'Content-Transfer-Encoding': 'base64' });
|
|
85
|
-
return imageBuffer; // Automatically encoded as base64
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
### Primitives
|
|
89
|
-
|
|
90
|
-
Numbers, booleans, and other primitives are converted to strings:
|
|
91
|
-
|
|
92
|
-
```typescript
|
|
93
|
-
return 42; // "42", Content-Type: text/plain
|
|
94
|
-
return true; // "true", Content-Type: text/plain
|
|
95
|
-
return new Date(); // ISO string, Content-Type: text/plain
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
## API Reference
|
|
99
|
-
|
|
100
|
-
### setStatusCode(statusCode)
|
|
101
|
-
|
|
102
|
-
Sets the HTTP status code for the response.
|
|
103
|
-
|
|
104
|
-
```typescript
|
|
105
|
-
response.setStatusCode(200); // OK
|
|
106
|
-
response.setStatusCode(201); // Created
|
|
107
|
-
response.setStatusCode(400); // Bad Request
|
|
108
|
-
response.setStatusCode(404); // Not Found
|
|
109
|
-
response.setStatusCode(500); // Internal Server Error
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### addHeaders(headers)
|
|
113
|
-
|
|
114
|
-
Adds or updates response headers. Headers are validated for security before being set.
|
|
115
|
-
|
|
116
|
-
```typescript
|
|
117
|
-
// Single header
|
|
118
|
-
response.addHeaders({ 'X-Custom-Header': 'value' });
|
|
119
|
-
|
|
120
|
-
// Multiple headers
|
|
121
|
-
response.addHeaders({
|
|
122
|
-
'Cache-Control': 'max-age=3600',
|
|
123
|
-
'X-API-Version': '2.0',
|
|
124
|
-
'Access-Control-Allow-Origin': '*'
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
// Security headers
|
|
128
|
-
response.addHeaders({
|
|
129
|
-
'X-Content-Type-Options': 'nosniff',
|
|
130
|
-
'X-Frame-Options': 'DENY',
|
|
131
|
-
'X-XSS-Protection': '1; mode=block'
|
|
132
|
-
});
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
### removeHeaders(headerNames)
|
|
136
|
-
|
|
137
|
-
Removes headers from the response.
|
|
138
|
-
|
|
139
|
-
```typescript
|
|
140
|
-
// Remove single header
|
|
141
|
-
response.removeHeaders(['X-Powered-By']);
|
|
142
|
-
|
|
143
|
-
// Remove multiple headers
|
|
144
|
-
response.removeHeaders(['Server', 'X-Debug-Info', 'X-Internal-ID']);
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
## Common Use Cases
|
|
148
|
-
|
|
149
|
-
- **Set Status Codes**: Configure HTTP status codes (200, 404, 500, etc.) to communicate request results to clients
|
|
150
|
-
- **Add Custom Headers**: Include metadata, caching directives, and API versioning information in responses
|
|
151
|
-
- **Handle File Downloads**: Stream binary data with proper Content-Type and Content-Disposition headers for file downloads
|
|
152
|
-
- **Configure CORS**: Enable cross-origin requests by adding Access-Control headers for browser security
|
|
153
|
-
- **Implement Redirects**: Send location headers with 301/302 status codes to redirect clients to new resources
|
|
154
|
-
- **Process Error Responses**: Return appropriate error status codes and structured error messages for client debugging
|
|
155
|
-
|
|
156
|
-
## Error Handling
|
|
157
|
-
|
|
158
|
-
YinzerFlow automatically handles response processing errors and provides clear error messages:
|
|
159
|
-
|
|
160
|
-
**Header-related errors:**
|
|
161
|
-
- `Invalid header value: contains CRLF characters`
|
|
162
|
-
- `Suspicious header pattern detected: potential injection attempt`
|
|
163
|
-
- `Header validation failed: invalid characters in header name`
|
|
164
|
-
|
|
165
|
-
**Body processing errors:**
|
|
166
|
-
- `Failed to stringify response body: circular reference detected`
|
|
167
|
-
- `Binary data encoding error: invalid buffer format`
|
|
168
|
-
- `Response body too large: exceeds maximum size limit`
|
|
169
|
-
|
|
170
|
-
These errors are automatically logged and result in appropriate HTTP status codes (500 Internal Server Error) to prevent malformed responses from being sent to clients.
|
|
171
|
-
|
|
172
|
-
## Security Considerations
|
|
173
|
-
|
|
174
|
-
YinzerFlow implements several security measures to prevent common response-based vulnerabilities:
|
|
175
|
-
|
|
176
|
-
### 🛡️ CRLF Injection Prevention
|
|
177
|
-
- **Problem**: Injecting carriage return (`\r`) or line feed (`\n`) characters in header values can allow attackers to inject additional headers or even HTTP response splitting attacks
|
|
178
|
-
- **YinzerFlow Solution**: Comprehensive validation detects and blocks CRLF characters in header values before they are set, preventing header injection attacks
|
|
179
|
-
|
|
180
|
-
### 🛡️ Response Header Validation
|
|
181
|
-
- **Problem**: Invalid header names or values can cause parsing errors in clients or bypass security controls
|
|
182
|
-
- **YinzerFlow Solution**: All response headers are validated against HTTP specifications and security patterns before being sent
|
|
183
|
-
|
|
184
|
-
### 🛡️ Suspicious Pattern Detection
|
|
185
|
-
- **Problem**: Attackers may try to inject malicious headers like `Set-Cookie` or create HTTP response splitting attacks
|
|
186
|
-
- **YinzerFlow Solution**: Advanced pattern detection identifies and blocks suspicious header values that could indicate injection attempts
|
|
187
|
-
|
|
188
|
-
### 🛡️ Safe Header Filtering
|
|
189
|
-
- **Problem**: Undefined or null header values can cause unexpected behavior or security bypasses
|
|
190
|
-
- **YinzerFlow Solution**: Automatic filtering removes undefined values and validates all headers before they reach the response
|
|
191
|
-
|
|
192
|
-
### 🛡️ Content-Type Security
|
|
193
|
-
- **Problem**: Missing or incorrect Content-Type headers can lead to MIME sniffing attacks or content confusion
|
|
194
|
-
- **YinzerFlow Solution**: Intelligent content type detection ensures appropriate Content-Type headers are set based on response body content
|
|
195
|
-
|
|
196
|
-
### 🛡️ JSON Stringification Safety
|
|
197
|
-
- **Problem**: Circular references or non-serializable objects can crash the server or leak sensitive information
|
|
198
|
-
- **YinzerFlow Solution**: Safe JSON stringification with error handling prevents crashes and provides fallback string conversion
|
|
199
|
-
|
|
200
|
-
### 🛡️ Binary Data Handling
|
|
201
|
-
- **Problem**: Improper binary data encoding can corrupt files or expose server internals
|
|
202
|
-
- **YinzerFlow Solution**: Proper binary data detection and encoding ensures files are transmitted correctly and securely
|
|
203
|
-
|
|
204
|
-
### 🛡️ Status Code Validation
|
|
205
|
-
- **Problem**: Invalid HTTP status codes can confuse clients or indicate server errors
|
|
206
|
-
- **YinzerFlow Solution**: Automatic mapping of status codes to standard HTTP status messages ensures consistent and valid responses
|
|
207
|
-
|
|
208
|
-
### 🛡️ Automatic Security Headers
|
|
209
|
-
- **Problem**: Missing security headers leave applications vulnerable to clickjacking, MIME sniffing, and XSS attacks
|
|
210
|
-
- **YinzerFlow Solution**: Automatically adds essential security headers to every response unless explicitly overridden by the application
|
|
211
|
-
|
|
212
|
-
These security measures ensure YinzerFlow's response implementation follows security best practices and prevents common attack vectors while maintaining HTTP compliance and performance.
|