@oxog/spark 1.1.1 → 1.3.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/CHANGELOG.md +656 -315
- package/LICENSE +20 -20
- package/README.md +288 -241
- package/package.json +90 -89
- package/src/core/application.js +1068 -956
- package/src/core/context.js +1098 -830
- package/src/core/middleware.js +303 -217
- package/src/index.js +183 -280
- package/src/middleware/body-parser.js +482 -526
- package/src/middleware/cache.js +177 -170
- package/src/middleware/compression.js +177 -182
- package/src/middleware/cors.js +176 -95
- package/src/middleware/health.js +219 -218
- package/src/middleware/logger.js +195 -194
- package/src/middleware/metrics.js +339 -327
- package/src/middleware/rate-limit.js +367 -319
- package/src/middleware/security.js +442 -332
- package/src/middleware/session.js +647 -529
- package/src/middleware/static.js +404 -299
- package/src/router/layer.js +197 -117
- package/src/router/route.js +149 -123
- package/src/router/router.js +705 -755
- package/src/utils/async-handler.js +145 -147
- package/src/utils/context-pool.js +83 -82
- package/src/utils/regex-validator.js +187 -163
- package/src/utils/safe-json.js +110 -0
- package/types/index.d.ts +538 -291
- package/dist/LICENSE +0 -21
- package/dist/README.md +0 -242
- package/dist/core/application.js +0 -959
- package/dist/core/context.js +0 -833
- package/dist/core/middleware.js +0 -220
- package/dist/core/request.js +0 -335
- package/dist/core/response.js +0 -377
- package/dist/index.js +0 -280
- package/dist/middleware/body-parser.js +0 -528
- package/dist/middleware/cache.js +0 -173
- package/dist/middleware/compression.js +0 -185
- package/dist/middleware/cors.js +0 -97
- package/dist/middleware/health.js +0 -221
- package/dist/middleware/index.js +0 -23
- package/dist/middleware/logger.js +0 -197
- package/dist/middleware/metrics.js +0 -330
- package/dist/middleware/rate-limit.js +0 -322
- package/dist/middleware/security.js +0 -335
- package/dist/middleware/session.js +0 -532
- package/dist/middleware/static.js +0 -301
- package/dist/package.json +0 -62
- package/dist/router/layer.js +0 -119
- package/dist/router/route.js +0 -125
- package/dist/router/router.js +0 -758
- package/dist/types/index.d.ts +0 -291
- package/dist/utils/async-handler.js +0 -150
- package/dist/utils/cache.js +0 -443
- package/dist/utils/context-pool.js +0 -85
- package/dist/utils/error-types.js +0 -134
- package/dist/utils/http.js +0 -415
- package/dist/utils/regex-validator.js +0 -166
- package/src/core/request.js +0 -335
- package/src/core/response.js +0 -377
- package/src/middleware/index.js +0 -23
- package/src/utils/cache.js +0 -443
- package/src/utils/error-types.js +0 -134
- package/src/utils/http.js +0 -415
|
@@ -1,166 +1,190 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const MAX_PATTERN_LENGTH = 1000;
|
|
4
|
-
const MAX_CAPTURE_GROUPS = 10;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
regex,
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
3
|
+
const MAX_PATTERN_LENGTH = 1000;
|
|
4
|
+
const MAX_CAPTURE_GROUPS = 10;
|
|
5
|
+
// SECURITY: Use safe patterns to detect dangerous regex (avoid ReDoS in validator itself)
|
|
6
|
+
// Previous patterns like /(\w+\+)+\w+/ could themselves cause ReDoS
|
|
7
|
+
const DANGEROUS_PATTERNS = [
|
|
8
|
+
/\(\w\+\+\)\+/, // Nested quantifiers with word chars: (\w+)+
|
|
9
|
+
/\(\w\+\*\)\+/, // Nested quantifiers: (\w*)+
|
|
10
|
+
/\(a\+\)\+b/, // Classic ReDoS pattern: (a+)+b
|
|
11
|
+
/\(\d\+\)\+/, // Numeric nested quantifiers: (\d+)+
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
class RegexValidator {
|
|
15
|
+
static isComplexPattern(pattern) {
|
|
16
|
+
if (typeof pattern !== 'string') return false;
|
|
17
|
+
|
|
18
|
+
// Check pattern length
|
|
19
|
+
if (pattern.length > MAX_PATTERN_LENGTH) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Count capture groups (excluding non-capturing groups like (?:...))
|
|
24
|
+
// SECURITY: Avoid lookbehind (?<!...) for older Node.js compatibility
|
|
25
|
+
// Match opening parens that are not escaped and not followed by ?
|
|
26
|
+
let captureGroups = 0;
|
|
27
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
28
|
+
if (pattern[i] === '(' && (i === 0 || pattern[i - 1] !== '\\')) {
|
|
29
|
+
if (i + 1 >= pattern.length || pattern[i + 1] !== '?') {
|
|
30
|
+
captureGroups++;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (captureGroups > MAX_CAPTURE_GROUPS) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Check for nested quantifiers
|
|
39
|
+
if (/(\+|\*|\{[^}]+\}){2,}/.test(pattern)) {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Check for dangerous patterns
|
|
44
|
+
for (const dangerous of DANGEROUS_PATTERNS) {
|
|
45
|
+
if (dangerous.test(pattern)) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static sanitizePattern(pattern) {
|
|
54
|
+
if (typeof pattern !== 'string') return pattern;
|
|
55
|
+
|
|
56
|
+
// Limit pattern length
|
|
57
|
+
if (pattern.length > MAX_PATTERN_LENGTH) {
|
|
58
|
+
pattern = pattern.substring(0, MAX_PATTERN_LENGTH);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// SECURITY: Removed dangerous quantifier replacement that changes semantics
|
|
62
|
+
// The pattern /(\+|\*){2,}/g would incorrectly change '++' to '+',
|
|
63
|
+
// which alters regex meaning (++ is literal, + is quantifier)
|
|
64
|
+
// Instead, reject patterns with excessive nested quantifiers during validation
|
|
65
|
+
|
|
66
|
+
// Limit quantifier ranges to prevent ReDoS
|
|
67
|
+
pattern = pattern.replace(/\{(\d+),?\}/g, (match, num) => {
|
|
68
|
+
const limit = parseInt(num, 10);
|
|
69
|
+
return limit > 100 ? '{0,100}' : match;
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
return pattern;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static testPerformance(pattern, testString = 'a'.repeat(100)) {
|
|
76
|
+
if (!(pattern instanceof RegExp)) {
|
|
77
|
+
try {
|
|
78
|
+
pattern = new RegExp(pattern);
|
|
79
|
+
} catch (e) {
|
|
80
|
+
return { safe: false, error: e.message };
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const startTime = process.hrtime.bigint();
|
|
85
|
+
const timeout = 100; // 100ms timeout
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
// Simple synchronous test with time check
|
|
89
|
+
const result = pattern.test(testString);
|
|
90
|
+
const endTime = process.hrtime.bigint();
|
|
91
|
+
const duration = Number(endTime - startTime) / 1e6; // Convert to milliseconds
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
safe: duration < timeout,
|
|
95
|
+
duration,
|
|
96
|
+
pattern: pattern.source,
|
|
97
|
+
result
|
|
98
|
+
};
|
|
99
|
+
} catch (error) {
|
|
100
|
+
return { safe: false, error: error.message };
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Regex cache with complexity checking
|
|
106
|
+
class SafeRegexCache {
|
|
107
|
+
constructor(maxSize = 1000) {
|
|
108
|
+
this.cache = new Map();
|
|
109
|
+
this.maxSize = maxSize;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
get(pattern, flags = '') {
|
|
113
|
+
const key = `${pattern}:${flags}`;
|
|
114
|
+
|
|
115
|
+
if (this.cache.has(key)) {
|
|
116
|
+
const entry = this.cache.get(key);
|
|
117
|
+
entry.lastAccess = Date.now();
|
|
118
|
+
return entry.regex;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// SECURITY: Validate regex flags before creating RegExp
|
|
122
|
+
// Valid flags are: g (global), i (ignoreCase), m (multiline), s (dotAll), u (unicode), y (sticky)
|
|
123
|
+
const validFlags = new Set(['g', 'i', 'm', 's', 'u', 'y']);
|
|
124
|
+
for (const flag of flags) {
|
|
125
|
+
if (!validFlags.has(flag)) {
|
|
126
|
+
throw new Error(`Invalid regex flag: "${flag}". Valid flags are: g, i, m, s, u, y`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Validate pattern before creating regex
|
|
131
|
+
if (RegexValidator.isComplexPattern(pattern)) {
|
|
132
|
+
pattern = RegexValidator.sanitizePattern(pattern);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
const regex = new RegExp(pattern, flags);
|
|
137
|
+
|
|
138
|
+
// Test performance before caching
|
|
139
|
+
const perfTest = RegexValidator.testPerformance(regex);
|
|
140
|
+
if (!perfTest.safe) {
|
|
141
|
+
throw new Error(`Unsafe regex pattern: ${perfTest.error}`);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Add to cache
|
|
145
|
+
if (this.cache.size >= this.maxSize) {
|
|
146
|
+
this.evictOldest();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
this.cache.set(key, {
|
|
150
|
+
regex,
|
|
151
|
+
lastAccess: Date.now(),
|
|
152
|
+
pattern,
|
|
153
|
+
flags
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
return regex;
|
|
157
|
+
} catch (error) {
|
|
158
|
+
throw new Error(`Invalid regex pattern: ${error.message}`, { cause: error });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
evictOldest() {
|
|
163
|
+
let oldest = null;
|
|
164
|
+
let oldestTime = Infinity;
|
|
165
|
+
|
|
166
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
167
|
+
if (entry.lastAccess < oldestTime) {
|
|
168
|
+
oldest = key;
|
|
169
|
+
oldestTime = entry.lastAccess;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (oldest) {
|
|
174
|
+
this.cache.delete(oldest);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
clear() {
|
|
179
|
+
this.cache.clear();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
get size() {
|
|
183
|
+
return this.cache.size;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
module.exports = {
|
|
188
|
+
RegexValidator,
|
|
189
|
+
SafeRegexCache
|
|
166
190
|
};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe JSON parsing with depth and size validation
|
|
3
|
+
* Prevents DoS attacks via deeply nested objects or extremely large payloads
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Parse JSON with security validations
|
|
8
|
+
* @param {string} text - JSON string to parse
|
|
9
|
+
* @param {Object} options - Parsing options
|
|
10
|
+
* @param {number} options.maxDepth - Maximum nesting depth (default: 20)
|
|
11
|
+
* @param {number} options.maxSize - Maximum JSON string size in bytes (default: 1MB)
|
|
12
|
+
* @param {Function} [options.reviver] - JSON.parse reviver
|
|
13
|
+
* @returns {*} Parsed JSON object
|
|
14
|
+
* @throws {Error} If validation fails or parsing errors
|
|
15
|
+
*/
|
|
16
|
+
function safeJSONParse(text, options = {}) {
|
|
17
|
+
const maxDepth = options.maxDepth || 20;
|
|
18
|
+
const maxSize = options.maxSize || 1024 * 1024; // 1MB default
|
|
19
|
+
|
|
20
|
+
// SECURITY: Validate size before parsing to prevent memory exhaustion
|
|
21
|
+
if (typeof text !== 'string') {
|
|
22
|
+
throw new Error('JSON input must be a string');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (text.length > maxSize) {
|
|
26
|
+
throw new Error(`JSON payload too large: ${text.length} bytes exceeds maximum ${maxSize} bytes`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// SECURITY: Validate depth to prevent stack overflow from deeply nested objects
|
|
30
|
+
// Count maximum nesting level by tracking bracket/brace depth
|
|
31
|
+
let depth = 0;
|
|
32
|
+
let maxDepthFound = 0;
|
|
33
|
+
let inString = false;
|
|
34
|
+
let escaped = false;
|
|
35
|
+
|
|
36
|
+
for (let i = 0; i < text.length; i++) {
|
|
37
|
+
const char = text[i];
|
|
38
|
+
|
|
39
|
+
if (escaped) {
|
|
40
|
+
escaped = false;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (char === '\\') {
|
|
45
|
+
escaped = true;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (char === '"') {
|
|
50
|
+
inString = !inString;
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (inString) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (char === '{' || char === '[') {
|
|
59
|
+
depth++;
|
|
60
|
+
if (depth > maxDepthFound) {
|
|
61
|
+
maxDepthFound = depth;
|
|
62
|
+
}
|
|
63
|
+
if (depth > maxDepth) {
|
|
64
|
+
throw new Error(`JSON nesting depth ${depth} exceeds maximum allowed depth ${maxDepth}`);
|
|
65
|
+
}
|
|
66
|
+
} else if (char === '}' || char === ']') {
|
|
67
|
+
depth--;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Parse the JSON after validation
|
|
72
|
+
try {
|
|
73
|
+
return options.reviver ? JSON.parse(text, options.reviver) : JSON.parse(text);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
throw new Error(`Invalid JSON: ${error.message}`, { cause: error });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Stringify JSON with size validation
|
|
81
|
+
* @param {*} value - Value to stringify
|
|
82
|
+
* @param {Object} options - Stringify options
|
|
83
|
+
* @param {number} options.maxSize - Maximum output size (default: 1MB)
|
|
84
|
+
* @returns {string} JSON string
|
|
85
|
+
* @throws {Error} If output exceeds size limit
|
|
86
|
+
*/
|
|
87
|
+
function safeJSONStringify(value, options = {}) {
|
|
88
|
+
const maxSize = options.maxSize || 1024 * 1024; // 1MB default
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
const result = JSON.stringify(value);
|
|
92
|
+
|
|
93
|
+
if (result.length > maxSize) {
|
|
94
|
+
throw new Error(`JSON output too large: ${result.length} bytes exceeds maximum ${maxSize} bytes`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return result;
|
|
98
|
+
} catch (error) {
|
|
99
|
+
// Handle circular references
|
|
100
|
+
if (error.message && error.message.includes('circular')) {
|
|
101
|
+
throw new Error('Cannot stringify circular JSON structure', { cause: error });
|
|
102
|
+
}
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
module.exports = {
|
|
108
|
+
safeJSONParse,
|
|
109
|
+
safeJSONStringify
|
|
110
|
+
};
|