@oxog/spark 1.1.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 +294 -0
- package/LICENSE +21 -0
- package/README.md +242 -0
- package/dist/LICENSE +21 -0
- package/dist/README.md +242 -0
- package/dist/core/application.js +957 -0
- package/dist/core/context.js +831 -0
- package/dist/core/middleware.js +218 -0
- package/dist/core/request.js +333 -0
- package/dist/core/response.js +375 -0
- package/dist/index.js +278 -0
- package/dist/middleware/body-parser.js +526 -0
- package/dist/middleware/cache.js +171 -0
- package/dist/middleware/compression.js +183 -0
- package/dist/middleware/cors.js +95 -0
- package/dist/middleware/health.js +219 -0
- package/dist/middleware/index.js +21 -0
- package/dist/middleware/logger.js +195 -0
- package/dist/middleware/metrics.js +328 -0
- package/dist/middleware/rate-limit.js +320 -0
- package/dist/middleware/security.js +333 -0
- package/dist/middleware/session.js +530 -0
- package/dist/middleware/static.js +299 -0
- package/dist/package.json +62 -0
- package/dist/router/layer.js +117 -0
- package/dist/router/route.js +123 -0
- package/dist/router/router.js +756 -0
- package/dist/types/index.d.ts +291 -0
- package/dist/utils/async-handler.js +145 -0
- package/dist/utils/cache.js +441 -0
- package/dist/utils/context-pool.js +83 -0
- package/dist/utils/error-types.js +132 -0
- package/dist/utils/http.js +413 -0
- package/dist/utils/regex-validator.js +177 -0
- package/package.json +89 -0
- package/src/core/application.js +957 -0
- package/src/core/context.js +831 -0
- package/src/core/middleware.js +218 -0
- package/src/core/request.js +333 -0
- package/src/core/response.js +375 -0
- package/src/index.js +278 -0
- package/src/middleware/body-parser.js +526 -0
- package/src/middleware/cache.js +171 -0
- package/src/middleware/compression.js +183 -0
- package/src/middleware/cors.js +95 -0
- package/src/middleware/health.js +219 -0
- package/src/middleware/index.js +21 -0
- package/src/middleware/logger.js +195 -0
- package/src/middleware/metrics.js +328 -0
- package/src/middleware/rate-limit.js +320 -0
- package/src/middleware/security.js +333 -0
- package/src/middleware/session.js +530 -0
- package/src/middleware/static.js +299 -0
- package/src/router/layer.js +117 -0
- package/src/router/route.js +123 -0
- package/src/router/router.js +756 -0
- package/src/utils/async-handler.js +145 -0
- package/src/utils/cache.js +441 -0
- package/src/utils/context-pool.js +83 -0
- package/src/utils/error-types.js +132 -0
- package/src/utils/http.js +413 -0
- package/src/utils/regex-validator.js +177 -0
- package/types/index.d.ts +291 -0
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
const { STATUS_CODES } = require('http');
|
|
2
|
+
|
|
3
|
+
const HTTP_STATUS = {
|
|
4
|
+
CONTINUE: 100,
|
|
5
|
+
SWITCHING_PROTOCOLS: 101,
|
|
6
|
+
PROCESSING: 102,
|
|
7
|
+
EARLY_HINTS: 103,
|
|
8
|
+
|
|
9
|
+
OK: 200,
|
|
10
|
+
CREATED: 201,
|
|
11
|
+
ACCEPTED: 202,
|
|
12
|
+
NON_AUTHORITATIVE_INFORMATION: 203,
|
|
13
|
+
NO_CONTENT: 204,
|
|
14
|
+
RESET_CONTENT: 205,
|
|
15
|
+
PARTIAL_CONTENT: 206,
|
|
16
|
+
MULTI_STATUS: 207,
|
|
17
|
+
ALREADY_REPORTED: 208,
|
|
18
|
+
IM_USED: 226,
|
|
19
|
+
|
|
20
|
+
MULTIPLE_CHOICES: 300,
|
|
21
|
+
MOVED_PERMANENTLY: 301,
|
|
22
|
+
FOUND: 302,
|
|
23
|
+
SEE_OTHER: 303,
|
|
24
|
+
NOT_MODIFIED: 304,
|
|
25
|
+
USE_PROXY: 305,
|
|
26
|
+
TEMPORARY_REDIRECT: 307,
|
|
27
|
+
PERMANENT_REDIRECT: 308,
|
|
28
|
+
|
|
29
|
+
BAD_REQUEST: 400,
|
|
30
|
+
UNAUTHORIZED: 401,
|
|
31
|
+
PAYMENT_REQUIRED: 402,
|
|
32
|
+
FORBIDDEN: 403,
|
|
33
|
+
NOT_FOUND: 404,
|
|
34
|
+
METHOD_NOT_ALLOWED: 405,
|
|
35
|
+
NOT_ACCEPTABLE: 406,
|
|
36
|
+
PROXY_AUTHENTICATION_REQUIRED: 407,
|
|
37
|
+
REQUEST_TIMEOUT: 408,
|
|
38
|
+
CONFLICT: 409,
|
|
39
|
+
GONE: 410,
|
|
40
|
+
LENGTH_REQUIRED: 411,
|
|
41
|
+
PRECONDITION_FAILED: 412,
|
|
42
|
+
PAYLOAD_TOO_LARGE: 413,
|
|
43
|
+
URI_TOO_LONG: 414,
|
|
44
|
+
UNSUPPORTED_MEDIA_TYPE: 415,
|
|
45
|
+
RANGE_NOT_SATISFIABLE: 416,
|
|
46
|
+
EXPECTATION_FAILED: 417,
|
|
47
|
+
IM_A_TEAPOT: 418,
|
|
48
|
+
MISDIRECTED_REQUEST: 421,
|
|
49
|
+
UNPROCESSABLE_ENTITY: 422,
|
|
50
|
+
LOCKED: 423,
|
|
51
|
+
FAILED_DEPENDENCY: 424,
|
|
52
|
+
TOO_EARLY: 425,
|
|
53
|
+
UPGRADE_REQUIRED: 426,
|
|
54
|
+
PRECONDITION_REQUIRED: 428,
|
|
55
|
+
TOO_MANY_REQUESTS: 429,
|
|
56
|
+
REQUEST_HEADER_FIELDS_TOO_LARGE: 431,
|
|
57
|
+
UNAVAILABLE_FOR_LEGAL_REASONS: 451,
|
|
58
|
+
|
|
59
|
+
INTERNAL_SERVER_ERROR: 500,
|
|
60
|
+
NOT_IMPLEMENTED: 501,
|
|
61
|
+
BAD_GATEWAY: 502,
|
|
62
|
+
SERVICE_UNAVAILABLE: 503,
|
|
63
|
+
GATEWAY_TIMEOUT: 504,
|
|
64
|
+
HTTP_VERSION_NOT_SUPPORTED: 505,
|
|
65
|
+
VARIANT_ALSO_NEGOTIATES: 506,
|
|
66
|
+
INSUFFICIENT_STORAGE: 507,
|
|
67
|
+
LOOP_DETECTED: 508,
|
|
68
|
+
NOT_EXTENDED: 510,
|
|
69
|
+
NETWORK_AUTHENTICATION_REQUIRED: 511
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const MIME_TYPES = {
|
|
73
|
+
'html': 'text/html',
|
|
74
|
+
'htm': 'text/html',
|
|
75
|
+
'txt': 'text/plain',
|
|
76
|
+
'css': 'text/css',
|
|
77
|
+
'js': 'application/javascript',
|
|
78
|
+
'json': 'application/json',
|
|
79
|
+
'xml': 'application/xml',
|
|
80
|
+
'pdf': 'application/pdf',
|
|
81
|
+
'zip': 'application/zip',
|
|
82
|
+
'tar': 'application/x-tar',
|
|
83
|
+
'gz': 'application/gzip',
|
|
84
|
+
'png': 'image/png',
|
|
85
|
+
'jpg': 'image/jpeg',
|
|
86
|
+
'jpeg': 'image/jpeg',
|
|
87
|
+
'gif': 'image/gif',
|
|
88
|
+
'svg': 'image/svg+xml',
|
|
89
|
+
'ico': 'image/x-icon',
|
|
90
|
+
'webp': 'image/webp',
|
|
91
|
+
'bmp': 'image/bmp',
|
|
92
|
+
'tiff': 'image/tiff',
|
|
93
|
+
'mp4': 'video/mp4',
|
|
94
|
+
'webm': 'video/webm',
|
|
95
|
+
'ogg': 'video/ogg',
|
|
96
|
+
'avi': 'video/x-msvideo',
|
|
97
|
+
'mov': 'video/quicktime',
|
|
98
|
+
'wmv': 'video/x-ms-wmv',
|
|
99
|
+
'flv': 'video/x-flv',
|
|
100
|
+
'mp3': 'audio/mpeg',
|
|
101
|
+
'wav': 'audio/wav',
|
|
102
|
+
'ogg': 'audio/ogg',
|
|
103
|
+
'aac': 'audio/aac',
|
|
104
|
+
'flac': 'audio/flac',
|
|
105
|
+
'woff': 'font/woff',
|
|
106
|
+
'woff2': 'font/woff2',
|
|
107
|
+
'ttf': 'font/ttf',
|
|
108
|
+
'otf': 'font/otf',
|
|
109
|
+
'eot': 'application/vnd.ms-fontobject'
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const HTTP_METHODS = [
|
|
113
|
+
'GET',
|
|
114
|
+
'POST',
|
|
115
|
+
'PUT',
|
|
116
|
+
'DELETE',
|
|
117
|
+
'PATCH',
|
|
118
|
+
'HEAD',
|
|
119
|
+
'OPTIONS',
|
|
120
|
+
'TRACE',
|
|
121
|
+
'CONNECT'
|
|
122
|
+
];
|
|
123
|
+
|
|
124
|
+
function isHttpError(status) {
|
|
125
|
+
return status >= 400;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function isClientError(status) {
|
|
129
|
+
return status >= 400 && status < 500;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function isServerError(status) {
|
|
133
|
+
return status >= 500;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function isRedirect(status) {
|
|
137
|
+
return status >= 300 && status < 400;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function isSuccess(status) {
|
|
141
|
+
return status >= 200 && status < 300;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function isInformational(status) {
|
|
145
|
+
return status >= 100 && status < 200;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function getStatusText(status) {
|
|
149
|
+
return STATUS_CODES[status] || 'Unknown Status';
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function getMimeType(extension) {
|
|
153
|
+
if (extension.startsWith('.')) {
|
|
154
|
+
extension = extension.slice(1);
|
|
155
|
+
}
|
|
156
|
+
return MIME_TYPES[extension.toLowerCase()] || 'application/octet-stream';
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function parseContentType(contentType) {
|
|
160
|
+
if (!contentType) {
|
|
161
|
+
return { type: '', charset: '', boundary: '' };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const [type, ...params] = contentType.split(';');
|
|
165
|
+
const result = { type: type.trim(), charset: '', boundary: '' };
|
|
166
|
+
|
|
167
|
+
for (const param of params) {
|
|
168
|
+
const [key, value] = param.split('=');
|
|
169
|
+
if (key && value) {
|
|
170
|
+
result[key.trim()] = value.trim().replace(/"/g, '');
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function parseAccept(accept) {
|
|
178
|
+
if (!accept) {
|
|
179
|
+
return [];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return accept
|
|
183
|
+
.split(',')
|
|
184
|
+
.map(type => {
|
|
185
|
+
const [mediaType, ...params] = type.split(';');
|
|
186
|
+
const result = { type: mediaType.trim(), q: 1.0 };
|
|
187
|
+
|
|
188
|
+
for (const param of params) {
|
|
189
|
+
const [key, value] = param.split('=');
|
|
190
|
+
if (key && value) {
|
|
191
|
+
if (key.trim() === 'q') {
|
|
192
|
+
result.q = parseFloat(value.trim());
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return result;
|
|
198
|
+
})
|
|
199
|
+
.sort((a, b) => b.q - a.q);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function parseRange(range, size) {
|
|
203
|
+
if (!range || !range.startsWith('bytes=')) {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const ranges = [];
|
|
208
|
+
const parts = range.slice(6).split(',');
|
|
209
|
+
|
|
210
|
+
for (const part of parts) {
|
|
211
|
+
const [start, end] = part.trim().split('-');
|
|
212
|
+
const startNum = parseInt(start);
|
|
213
|
+
const endNum = parseInt(end);
|
|
214
|
+
|
|
215
|
+
if (isNaN(startNum) && isNaN(endNum)) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (isNaN(startNum)) {
|
|
220
|
+
ranges.push({ start: Math.max(0, size - endNum), end: size - 1 });
|
|
221
|
+
} else if (isNaN(endNum)) {
|
|
222
|
+
ranges.push({ start: startNum, end: size - 1 });
|
|
223
|
+
} else {
|
|
224
|
+
ranges.push({ start: startNum, end: Math.min(endNum, size - 1) });
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return ranges.length > 0 ? ranges : null;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function parseAuthorizationHeader(auth) {
|
|
232
|
+
if (!auth) {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const [scheme, credentials] = auth.split(' ');
|
|
237
|
+
|
|
238
|
+
if (scheme.toLowerCase() === 'basic') {
|
|
239
|
+
const [username, password] = Buffer.from(credentials, 'base64').toString().split(':');
|
|
240
|
+
return { scheme: 'basic', username, password };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if (scheme.toLowerCase() === 'bearer') {
|
|
244
|
+
return { scheme: 'bearer', token: credentials };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return { scheme: scheme.toLowerCase(), credentials };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function parseUserAgent(userAgent) {
|
|
251
|
+
if (!userAgent) {
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const browsers = [
|
|
256
|
+
{ name: 'Chrome', pattern: /Chrome\/([^\s]+)/ },
|
|
257
|
+
{ name: 'Firefox', pattern: /Firefox\/([^\s]+)/ },
|
|
258
|
+
{ name: 'Safari', pattern: /Version\/([^\s]+).*Safari/ },
|
|
259
|
+
{ name: 'Edge', pattern: /Edge\/([^\s]+)/ },
|
|
260
|
+
{ name: 'IE', pattern: /MSIE ([^\s]+)/ },
|
|
261
|
+
{ name: 'Opera', pattern: /OPR\/([^\s]+)/ }
|
|
262
|
+
];
|
|
263
|
+
|
|
264
|
+
for (const browser of browsers) {
|
|
265
|
+
const match = userAgent.match(browser.pattern);
|
|
266
|
+
if (match) {
|
|
267
|
+
return { name: browser.name, version: match[1] };
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return { name: 'Unknown', version: 'Unknown' };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function createError(status, message, properties = {}) {
|
|
275
|
+
const error = new Error(message || getStatusText(status));
|
|
276
|
+
error.status = status;
|
|
277
|
+
error.statusCode = status;
|
|
278
|
+
error.expose = isClientError(status);
|
|
279
|
+
|
|
280
|
+
Object.assign(error, properties);
|
|
281
|
+
|
|
282
|
+
return error;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function isValidMethod(method) {
|
|
286
|
+
return HTTP_METHODS.includes(method.toUpperCase());
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function parseQuery(queryString) {
|
|
290
|
+
if (!queryString) {
|
|
291
|
+
return {};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const params = {};
|
|
295
|
+
const pairs = queryString.split('&');
|
|
296
|
+
|
|
297
|
+
for (const pair of pairs) {
|
|
298
|
+
const [key, value] = pair.split('=');
|
|
299
|
+
if (key) {
|
|
300
|
+
const decodedKey = decodeURIComponent(key);
|
|
301
|
+
const decodedValue = value ? decodeURIComponent(value) : '';
|
|
302
|
+
|
|
303
|
+
if (params[decodedKey]) {
|
|
304
|
+
if (Array.isArray(params[decodedKey])) {
|
|
305
|
+
params[decodedKey].push(decodedValue);
|
|
306
|
+
} else {
|
|
307
|
+
params[decodedKey] = [params[decodedKey], decodedValue];
|
|
308
|
+
}
|
|
309
|
+
} else {
|
|
310
|
+
params[decodedKey] = decodedValue;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return params;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function stringifyQuery(params) {
|
|
319
|
+
if (!params || typeof params !== 'object') {
|
|
320
|
+
return '';
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const pairs = [];
|
|
324
|
+
|
|
325
|
+
for (const [key, value] of Object.entries(params)) {
|
|
326
|
+
if (Array.isArray(value)) {
|
|
327
|
+
for (const item of value) {
|
|
328
|
+
pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(item)}`);
|
|
329
|
+
}
|
|
330
|
+
} else if (value !== null && value !== undefined) {
|
|
331
|
+
pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
return pairs.join('&');
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function parseUrl(url) {
|
|
339
|
+
try {
|
|
340
|
+
const parsed = new URL(url);
|
|
341
|
+
return {
|
|
342
|
+
protocol: parsed.protocol,
|
|
343
|
+
hostname: parsed.hostname,
|
|
344
|
+
port: parsed.port,
|
|
345
|
+
pathname: parsed.pathname,
|
|
346
|
+
search: parsed.search,
|
|
347
|
+
hash: parsed.hash,
|
|
348
|
+
query: parseQuery(parsed.search.slice(1))
|
|
349
|
+
};
|
|
350
|
+
} catch (error) {
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
function formatUrl(parts) {
|
|
356
|
+
let url = '';
|
|
357
|
+
|
|
358
|
+
if (parts.protocol) {
|
|
359
|
+
url += parts.protocol;
|
|
360
|
+
if (!parts.protocol.endsWith(':')) {
|
|
361
|
+
url += ':';
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (parts.hostname) {
|
|
366
|
+
url += '//' + parts.hostname;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (parts.port) {
|
|
370
|
+
url += ':' + parts.port;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (parts.pathname) {
|
|
374
|
+
url += parts.pathname;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (parts.query && Object.keys(parts.query).length > 0) {
|
|
378
|
+
url += '?' + stringifyQuery(parts.query);
|
|
379
|
+
} else if (parts.search) {
|
|
380
|
+
url += parts.search;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (parts.hash) {
|
|
384
|
+
url += parts.hash;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return url;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
module.exports = {
|
|
391
|
+
HTTP_STATUS,
|
|
392
|
+
MIME_TYPES,
|
|
393
|
+
HTTP_METHODS,
|
|
394
|
+
isHttpError,
|
|
395
|
+
isClientError,
|
|
396
|
+
isServerError,
|
|
397
|
+
isRedirect,
|
|
398
|
+
isSuccess,
|
|
399
|
+
isInformational,
|
|
400
|
+
getStatusText,
|
|
401
|
+
getMimeType,
|
|
402
|
+
parseContentType,
|
|
403
|
+
parseAccept,
|
|
404
|
+
parseRange,
|
|
405
|
+
parseAuthorizationHeader,
|
|
406
|
+
parseUserAgent,
|
|
407
|
+
createError,
|
|
408
|
+
isValidMethod,
|
|
409
|
+
parseQuery,
|
|
410
|
+
stringifyQuery,
|
|
411
|
+
parseUrl,
|
|
412
|
+
formatUrl
|
|
413
|
+
};
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
const MAX_PATTERN_LENGTH = 1000;
|
|
2
|
+
const MAX_CAPTURE_GROUPS = 10;
|
|
3
|
+
const DANGEROUS_PATTERNS = [
|
|
4
|
+
/(\w+\+)+\w+/, // Potential catastrophic backtracking
|
|
5
|
+
/(\w+\*)+\w+/, // Potential catastrophic backtracking
|
|
6
|
+
/(a+)+b/, // Classic ReDoS pattern
|
|
7
|
+
/(\d+)+\w/, // Numeric ReDoS pattern
|
|
8
|
+
];
|
|
9
|
+
|
|
10
|
+
class RegexValidator {
|
|
11
|
+
static isComplexPattern(pattern) {
|
|
12
|
+
if (typeof pattern !== 'string') return false;
|
|
13
|
+
|
|
14
|
+
// Check pattern length
|
|
15
|
+
if (pattern.length > MAX_PATTERN_LENGTH) {
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Count capture groups
|
|
20
|
+
const captureGroups = pattern.match(/\([^)]*\)/g) || [];
|
|
21
|
+
if (captureGroups.length > MAX_CAPTURE_GROUPS) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Check for nested quantifiers
|
|
26
|
+
if (/(\+|\*|\{[^}]+\}){2,}/.test(pattern)) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Check for dangerous patterns
|
|
31
|
+
for (const dangerous of DANGEROUS_PATTERNS) {
|
|
32
|
+
if (dangerous.test(pattern)) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static sanitizePattern(pattern) {
|
|
41
|
+
if (typeof pattern !== 'string') return pattern;
|
|
42
|
+
|
|
43
|
+
// Limit pattern length
|
|
44
|
+
if (pattern.length > MAX_PATTERN_LENGTH) {
|
|
45
|
+
pattern = pattern.substring(0, MAX_PATTERN_LENGTH);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Replace potentially dangerous quantifiers
|
|
49
|
+
pattern = pattern.replace(/(\+|\*){2,}/g, '$1');
|
|
50
|
+
pattern = pattern.replace(/\{(\d+),?\}/g, (match, num) => {
|
|
51
|
+
const limit = parseInt(num);
|
|
52
|
+
return limit > 100 ? '{0,100}' : match;
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
return pattern;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static testPerformance(pattern, testString = 'a'.repeat(100)) {
|
|
59
|
+
if (!(pattern instanceof RegExp)) {
|
|
60
|
+
try {
|
|
61
|
+
pattern = new RegExp(pattern);
|
|
62
|
+
} catch (e) {
|
|
63
|
+
return { safe: false, error: e.message };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const startTime = process.hrtime.bigint();
|
|
68
|
+
const timeout = 100; // 100ms timeout
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
// Set up a timeout
|
|
72
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
73
|
+
setTimeout(() => reject(new Error('Regex timeout')), timeout);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Test the regex
|
|
77
|
+
const testPromise = new Promise((resolve) => {
|
|
78
|
+
const result = pattern.test(testString);
|
|
79
|
+
resolve(result);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Race between test and timeout
|
|
83
|
+
Promise.race([testPromise, timeoutPromise]).catch(() => {
|
|
84
|
+
return { safe: false, error: 'Regex execution timeout' };
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const endTime = process.hrtime.bigint();
|
|
88
|
+
const duration = Number(endTime - startTime) / 1e6; // Convert to milliseconds
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
safe: duration < timeout,
|
|
92
|
+
duration,
|
|
93
|
+
pattern: pattern.source
|
|
94
|
+
};
|
|
95
|
+
} catch (error) {
|
|
96
|
+
return { safe: false, error: error.message };
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Regex cache with complexity checking
|
|
102
|
+
class SafeRegexCache {
|
|
103
|
+
constructor(maxSize = 1000) {
|
|
104
|
+
this.cache = new Map();
|
|
105
|
+
this.maxSize = maxSize;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
get(pattern, flags = '') {
|
|
109
|
+
const key = `${pattern}:${flags}`;
|
|
110
|
+
|
|
111
|
+
if (this.cache.has(key)) {
|
|
112
|
+
const entry = this.cache.get(key);
|
|
113
|
+
entry.lastAccess = Date.now();
|
|
114
|
+
return entry.regex;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Validate pattern before creating regex
|
|
118
|
+
if (RegexValidator.isComplexPattern(pattern)) {
|
|
119
|
+
pattern = RegexValidator.sanitizePattern(pattern);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
const regex = new RegExp(pattern, flags);
|
|
124
|
+
|
|
125
|
+
// Test performance before caching
|
|
126
|
+
const perfTest = RegexValidator.testPerformance(regex);
|
|
127
|
+
if (!perfTest.safe) {
|
|
128
|
+
throw new Error(`Unsafe regex pattern: ${perfTest.error}`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Add to cache
|
|
132
|
+
if (this.cache.size >= this.maxSize) {
|
|
133
|
+
this.evictOldest();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
this.cache.set(key, {
|
|
137
|
+
regex,
|
|
138
|
+
lastAccess: Date.now(),
|
|
139
|
+
pattern,
|
|
140
|
+
flags
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
return regex;
|
|
144
|
+
} catch (error) {
|
|
145
|
+
throw new Error(`Invalid regex pattern: ${error.message}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
evictOldest() {
|
|
150
|
+
let oldest = null;
|
|
151
|
+
let oldestTime = Infinity;
|
|
152
|
+
|
|
153
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
154
|
+
if (entry.lastAccess < oldestTime) {
|
|
155
|
+
oldest = key;
|
|
156
|
+
oldestTime = entry.lastAccess;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (oldest) {
|
|
161
|
+
this.cache.delete(oldest);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
clear() {
|
|
166
|
+
this.cache.clear();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
get size() {
|
|
170
|
+
return this.cache.size;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
module.exports = {
|
|
175
|
+
RegexValidator,
|
|
176
|
+
SafeRegexCache
|
|
177
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oxog/spark",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Ultra-fast, zero-dependency Node.js web framework with security hardening, memory leak protection, and enhanced error handling",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"types": "types/index.d.ts",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=14.0.0"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "node tests/run-all-tests.js",
|
|
12
|
+
"test:integration": "node test-integration.js",
|
|
13
|
+
"test:unit": "node tests/unit/run.js",
|
|
14
|
+
"test:performance": "node tests/performance/run.js",
|
|
15
|
+
"test:security": "node tests/security/run.js",
|
|
16
|
+
"test:coverage": "node scripts/test-coverage.js",
|
|
17
|
+
"build": "node scripts/build.js",
|
|
18
|
+
"build:minify": "node scripts/minify.js",
|
|
19
|
+
"benchmark": "node benchmarks/run.js",
|
|
20
|
+
"benchmark:express": "node benchmarks/vs-express.js",
|
|
21
|
+
"benchmark:fastify": "node benchmarks/vs-fastify.js",
|
|
22
|
+
"benchmark:koa": "node benchmarks/vs-koa.js",
|
|
23
|
+
"dev": "node examples/basic-api/server.js",
|
|
24
|
+
"lint": "node scripts/lint.js",
|
|
25
|
+
"format:check": "node scripts/format-check.js",
|
|
26
|
+
"docs": "node scripts/generate-docs.js",
|
|
27
|
+
"docs:validate": "node scripts/validate-docs.js",
|
|
28
|
+
"docs:build": "node scripts/build-docs.js",
|
|
29
|
+
"docs:test": "node tests/docs-examples/run-all-tests.js",
|
|
30
|
+
"examples:test": "node scripts/test-examples.js",
|
|
31
|
+
"validate:all": "node validation/run-all-validations.js",
|
|
32
|
+
"validate:core": "node validation/core-functionality.js",
|
|
33
|
+
"validate:features": "node validation/features/checklist.js",
|
|
34
|
+
"validate:build": "node scripts/validate-build.js",
|
|
35
|
+
"validate:package": "node scripts/validate-package.js",
|
|
36
|
+
"status": "node scripts/production-summary.js",
|
|
37
|
+
"prepublishOnly": "node scripts/final-validation.js",
|
|
38
|
+
"start": "node examples/basic-api/server.js"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"spark",
|
|
42
|
+
"api",
|
|
43
|
+
"framework",
|
|
44
|
+
"rest",
|
|
45
|
+
"http",
|
|
46
|
+
"server",
|
|
47
|
+
"web",
|
|
48
|
+
"app",
|
|
49
|
+
"middleware",
|
|
50
|
+
"router",
|
|
51
|
+
"zero-dependency",
|
|
52
|
+
"fast",
|
|
53
|
+
"secure",
|
|
54
|
+
"lightweight",
|
|
55
|
+
"performance",
|
|
56
|
+
"typescript",
|
|
57
|
+
"security",
|
|
58
|
+
"memory-safe",
|
|
59
|
+
"error-handling",
|
|
60
|
+
"redos-protection",
|
|
61
|
+
"path-traversal-protection",
|
|
62
|
+
"header-injection-protection"
|
|
63
|
+
],
|
|
64
|
+
"author": "Ersin Koç",
|
|
65
|
+
"license": "MIT",
|
|
66
|
+
"repository": {
|
|
67
|
+
"type": "git",
|
|
68
|
+
"url": "https://github.com/ersinkoc/spark.git"
|
|
69
|
+
},
|
|
70
|
+
"bugs": {
|
|
71
|
+
"url": "https://github.com/ersinkoc/spark/issues"
|
|
72
|
+
},
|
|
73
|
+
"homepage": "https://github.com/ersinkoc/spark#readme",
|
|
74
|
+
"files": [
|
|
75
|
+
"src/",
|
|
76
|
+
"types/",
|
|
77
|
+
"dist/",
|
|
78
|
+
"README.md",
|
|
79
|
+
"LICENSE",
|
|
80
|
+
"CHANGELOG.md"
|
|
81
|
+
],
|
|
82
|
+
"exports": {
|
|
83
|
+
".": {
|
|
84
|
+
"import": "./src/index.js",
|
|
85
|
+
"require": "./src/index.js"
|
|
86
|
+
},
|
|
87
|
+
"./package.json": "./package.json"
|
|
88
|
+
}
|
|
89
|
+
}
|