@oxog/spark 1.1.1 → 1.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/CHANGELOG.md +632 -315
- package/LICENSE +20 -20
- package/README.md +288 -241
- package/package.json +83 -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 +703 -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 +536 -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
package/src/core/middleware.js
CHANGED
|
@@ -1,220 +1,306 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
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
|
-
ctx.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
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
|
-
const
|
|
107
|
-
if (
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
return async (ctx, next) => {
|
|
174
|
-
if (
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
};
|
|
191
|
-
},
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
const crypto = require('crypto');
|
|
195
|
-
|
|
196
|
-
return async (ctx, next) => {
|
|
197
|
-
|
|
198
|
-
ctx.
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
3
|
+
const crypto = require('crypto');
|
|
4
|
+
const bodyParser = require('../middleware/body-parser');
|
|
5
|
+
const cors = require('../middleware/cors');
|
|
6
|
+
const compression = require('../middleware/compression');
|
|
7
|
+
const staticFiles = require('../middleware/static');
|
|
8
|
+
const session = require('../middleware/session');
|
|
9
|
+
const rateLimit = require('../middleware/rate-limit');
|
|
10
|
+
const security = require('../middleware/security');
|
|
11
|
+
const logger = require('../middleware/logger');
|
|
12
|
+
|
|
13
|
+
function createMiddleware(app) {
|
|
14
|
+
return {
|
|
15
|
+
bodyParser: (options) => bodyParser(options),
|
|
16
|
+
cors: (options) => cors(options),
|
|
17
|
+
compression: (options) => compression(options),
|
|
18
|
+
static: (root, options) => staticFiles(root, options),
|
|
19
|
+
session: (options) => session(options),
|
|
20
|
+
rateLimit: (options) => rateLimit(options),
|
|
21
|
+
security: (options) => security(options),
|
|
22
|
+
logger: (options) => logger(options),
|
|
23
|
+
|
|
24
|
+
helmet: (options) => security(options),
|
|
25
|
+
|
|
26
|
+
json: (options) => bodyParser({ ...options, type: 'json' }),
|
|
27
|
+
urlencoded: (options) => bodyParser({ ...options, type: 'urlencoded' }),
|
|
28
|
+
raw: (options) => bodyParser({ ...options, type: 'raw' }),
|
|
29
|
+
text: (options) => bodyParser({ ...options, type: 'text' }),
|
|
30
|
+
|
|
31
|
+
gzip: (options) => compression({ ...options, algorithm: 'gzip' }),
|
|
32
|
+
deflate: (options) => compression({ ...options, algorithm: 'deflate' }),
|
|
33
|
+
brotli: (options) => compression({ ...options, algorithm: 'br' }),
|
|
34
|
+
|
|
35
|
+
basicAuth: (options) => {
|
|
36
|
+
return async (ctx, next) => {
|
|
37
|
+
const auth = ctx.get('authorization');
|
|
38
|
+
if (!auth || !auth.startsWith('Basic ')) {
|
|
39
|
+
ctx.set('WWW-Authenticate', 'Basic realm="Secure Area"');
|
|
40
|
+
ctx.status(401).json({ error: 'Unauthorized' });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const credentials = Buffer.from(auth.slice(6), 'base64').toString();
|
|
45
|
+
const [username, password] = credentials.split(':');
|
|
46
|
+
|
|
47
|
+
// SECURITY FIX (BUG-SEC-006): Prevent username enumeration via timing attacks
|
|
48
|
+
// Always perform the same operations regardless of username existence
|
|
49
|
+
let authenticated = false;
|
|
50
|
+
|
|
51
|
+
// Use a dummy password for timing-safe comparison when user doesn't exist
|
|
52
|
+
const dummyPassword = 'dummy_password_to_prevent_timing_attack_1234567890';
|
|
53
|
+
|
|
54
|
+
if (options.users) {
|
|
55
|
+
// Get the expected password (or dummy if user doesn't exist)
|
|
56
|
+
const expectedPassword = options.users[username] || dummyPassword;
|
|
57
|
+
const userExists = Object.prototype.hasOwnProperty.call(options.users, username);
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
// SECURITY: Always perform timing-safe comparison, even for non-existent users
|
|
61
|
+
// This prevents timing attacks that could enumerate valid usernames
|
|
62
|
+
const maxLen = Math.max(password.length, expectedPassword.length);
|
|
63
|
+
const passwordBuf = Buffer.from(password.padEnd(maxLen, '\0'));
|
|
64
|
+
const expectedBuf = Buffer.from(expectedPassword.padEnd(maxLen, '\0'));
|
|
65
|
+
|
|
66
|
+
const passwordMatches = crypto.timingSafeEqual(passwordBuf, expectedBuf) &&
|
|
67
|
+
password.length === expectedPassword.length;
|
|
68
|
+
|
|
69
|
+
// Only set authenticated if BOTH user exists AND password matches
|
|
70
|
+
authenticated = userExists && passwordMatches;
|
|
71
|
+
} catch (err) {
|
|
72
|
+
// Length mismatch or other error - not authenticated
|
|
73
|
+
authenticated = false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (authenticated) {
|
|
78
|
+
ctx.user = { username };
|
|
79
|
+
await next();
|
|
80
|
+
} else if (options.verify) {
|
|
81
|
+
// SECURITY: Even with custom verify, perform dummy operation if needed
|
|
82
|
+
// to maintain constant timing
|
|
83
|
+
const verifyResult = await options.verify(username, password);
|
|
84
|
+
if (verifyResult) {
|
|
85
|
+
ctx.user = { username };
|
|
86
|
+
await next();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// SECURITY: Add small random delay to further prevent timing attacks
|
|
92
|
+
// This makes it harder to measure timing differences
|
|
93
|
+
if (!authenticated) {
|
|
94
|
+
await new Promise(resolve => setTimeout(resolve, Math.random() * 10));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (!authenticated) {
|
|
98
|
+
ctx.set('WWW-Authenticate', 'Basic realm="Secure Area"');
|
|
99
|
+
ctx.status(401).json({ error: 'Unauthorized' });
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
timeout: (ms) => {
|
|
105
|
+
return async (ctx, next) => {
|
|
106
|
+
const timeout = setTimeout(() => {
|
|
107
|
+
if (!ctx.responded) {
|
|
108
|
+
ctx.status(408).json({ error: 'Request Timeout' });
|
|
109
|
+
}
|
|
110
|
+
}, ms);
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
await next();
|
|
114
|
+
} finally {
|
|
115
|
+
clearTimeout(timeout);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
errorHandler: (handler) => {
|
|
121
|
+
return async (ctx, next) => {
|
|
122
|
+
try {
|
|
123
|
+
await next();
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (handler) {
|
|
126
|
+
await handler(error, ctx);
|
|
127
|
+
} else {
|
|
128
|
+
ctx.status(500).json({
|
|
129
|
+
error: 'Internal Server Error',
|
|
130
|
+
message: process.env.NODE_ENV === 'development' ? error.message : undefined
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
|
|
137
|
+
conditional: (condition, middleware) => {
|
|
138
|
+
return async (ctx, next) => {
|
|
139
|
+
if (typeof condition === 'function' ? condition(ctx) : condition) {
|
|
140
|
+
await middleware(ctx, next);
|
|
141
|
+
} else {
|
|
142
|
+
await next();
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
compose: (...middlewares) => {
|
|
148
|
+
return async (ctx, next) => {
|
|
149
|
+
const dispatch = async (i) => {
|
|
150
|
+
if (i >= middlewares.length) {
|
|
151
|
+
return next();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const middleware = middlewares[i];
|
|
155
|
+
return middleware(ctx, () => dispatch(i + 1));
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
return dispatch(0);
|
|
159
|
+
};
|
|
160
|
+
},
|
|
161
|
+
|
|
162
|
+
unless: (condition, middleware) => {
|
|
163
|
+
return async (ctx, next) => {
|
|
164
|
+
if (typeof condition === 'function' ? !condition(ctx) : !condition) {
|
|
165
|
+
await middleware(ctx, next);
|
|
166
|
+
} else {
|
|
167
|
+
await next();
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
skipIf: (condition) => {
|
|
173
|
+
return async (ctx, next) => {
|
|
174
|
+
if (typeof condition === 'function' ? condition(ctx) : condition) {
|
|
175
|
+
await next();
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
},
|
|
179
|
+
|
|
180
|
+
cache: (options = {}) => {
|
|
181
|
+
const { maxAge = 3600, private: isPrivate = false } = options;
|
|
182
|
+
|
|
183
|
+
return async (ctx, next) => {
|
|
184
|
+
await next();
|
|
185
|
+
|
|
186
|
+
if (ctx.method === 'GET' && ctx.statusCode === 200) {
|
|
187
|
+
const cacheControl = isPrivate ? 'private' : 'public';
|
|
188
|
+
ctx.set('Cache-Control', `${cacheControl}, max-age=${maxAge}`);
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
etag: (options = {}) => {
|
|
194
|
+
const crypto = require('crypto');
|
|
195
|
+
|
|
196
|
+
return async (ctx, next) => {
|
|
197
|
+
// BUG FIX: Intercept response body to generate ETag
|
|
198
|
+
// The previous implementation used ctx.res._getData() which doesn't exist
|
|
199
|
+
// on real Node.js response objects (only in mock objects)
|
|
200
|
+
|
|
201
|
+
let responseBody = null;
|
|
202
|
+
const originalSend = ctx.send;
|
|
203
|
+
const originalJson = ctx.json;
|
|
204
|
+
|
|
205
|
+
// Intercept send() to capture body
|
|
206
|
+
ctx.send = function(data) {
|
|
207
|
+
responseBody = data;
|
|
208
|
+
return originalSend.call(this, data);
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
// Intercept json() to capture body
|
|
212
|
+
ctx.json = function(data) {
|
|
213
|
+
responseBody = JSON.stringify(data);
|
|
214
|
+
return originalJson.call(this, data);
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
await next();
|
|
218
|
+
|
|
219
|
+
// Generate ETag if we captured a body
|
|
220
|
+
if (ctx.method === 'GET' && ctx.statusCode === 200 && responseBody) {
|
|
221
|
+
const bodyStr = typeof responseBody === 'string' ? responseBody : String(responseBody);
|
|
222
|
+
// SECURITY: Use SHA-256 instead of MD5 for cryptographic strength
|
|
223
|
+
const etag = crypto.createHash('sha256').update(bodyStr).digest('hex').substring(0, 32);
|
|
224
|
+
|
|
225
|
+
ctx.set('ETag', `"${etag}"`);
|
|
226
|
+
|
|
227
|
+
// Check if client's ETag matches (conditional GET)
|
|
228
|
+
if (ctx.get('if-none-match') === `"${etag}"`) {
|
|
229
|
+
ctx.status(304).end();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
},
|
|
234
|
+
|
|
235
|
+
favicon: (path) => {
|
|
236
|
+
// BUG FIX: Use async file reading to avoid blocking event loop
|
|
237
|
+
const fs = require('fs').promises;
|
|
238
|
+
let favicon = null;
|
|
239
|
+
let loading = null;
|
|
240
|
+
|
|
241
|
+
// Start loading favicon asynchronously
|
|
242
|
+
loading = fs.readFile(path).then(data => {
|
|
243
|
+
favicon = data;
|
|
244
|
+
loading = null;
|
|
245
|
+
}).catch(err => {
|
|
246
|
+
console.error('Failed to load favicon:', err.message);
|
|
247
|
+
loading = null;
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
return async (ctx, next) => {
|
|
251
|
+
if (ctx.path === '/favicon.ico') {
|
|
252
|
+
// Wait for favicon to load if still loading
|
|
253
|
+
if (loading) {
|
|
254
|
+
await loading;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (favicon) {
|
|
258
|
+
ctx.set('Content-Type', 'image/x-icon');
|
|
259
|
+
ctx.set('Cache-Control', 'public, max-age=86400');
|
|
260
|
+
ctx.send(favicon);
|
|
261
|
+
} else {
|
|
262
|
+
ctx.status(404).send('Favicon not found');
|
|
263
|
+
}
|
|
264
|
+
} else {
|
|
265
|
+
await next();
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
},
|
|
269
|
+
|
|
270
|
+
responseTime: () => {
|
|
271
|
+
return async (ctx, next) => {
|
|
272
|
+
const start = Date.now();
|
|
273
|
+
await next();
|
|
274
|
+
const duration = Date.now() - start;
|
|
275
|
+
ctx.set('X-Response-Time', `${duration}ms`);
|
|
276
|
+
};
|
|
277
|
+
},
|
|
278
|
+
|
|
279
|
+
requestId: () => {
|
|
280
|
+
const crypto = require('crypto');
|
|
281
|
+
|
|
282
|
+
return async (ctx, next) => {
|
|
283
|
+
const id = crypto.randomBytes(16).toString('hex');
|
|
284
|
+
ctx.requestId = id;
|
|
285
|
+
ctx.set('X-Request-ID', id);
|
|
286
|
+
await next();
|
|
287
|
+
};
|
|
288
|
+
},
|
|
289
|
+
|
|
290
|
+
healthCheck: (path = '/health') => {
|
|
291
|
+
return async (ctx, next) => {
|
|
292
|
+
if (ctx.path === path) {
|
|
293
|
+
ctx.json({
|
|
294
|
+
status: 'healthy',
|
|
295
|
+
timestamp: new Date().toISOString(),
|
|
296
|
+
uptime: process.uptime()
|
|
297
|
+
});
|
|
298
|
+
} else {
|
|
299
|
+
await next();
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
220
306
|
module.exports = { createMiddleware };
|