kempo-server 1.7.7 → 1.7.9
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/package.json +1 -1
- package/src/router.js +48 -39
- package/tests/router.node-test.js +46 -0
package/package.json
CHANGED
package/src/router.js
CHANGED
|
@@ -16,9 +16,9 @@ import {
|
|
|
16
16
|
} from './builtinMiddleware.js';
|
|
17
17
|
|
|
18
18
|
export default async (flags, log) => {
|
|
19
|
-
log('Initializing router',
|
|
19
|
+
log('Initializing router', 3);
|
|
20
20
|
const rootPath = path.isAbsolute(flags.root) ? flags.root : path.join(process.cwd(), flags.root);
|
|
21
|
-
log(`Root path: ${rootPath}`,
|
|
21
|
+
log(`Root path: ${rootPath}`, 3);
|
|
22
22
|
|
|
23
23
|
let config = defaultConfig;
|
|
24
24
|
try {
|
|
@@ -28,25 +28,25 @@ export default async (flags, log) => {
|
|
|
28
28
|
? configFileName
|
|
29
29
|
: path.join(rootPath, configFileName);
|
|
30
30
|
|
|
31
|
-
log(`Config file name: ${configFileName}`,
|
|
32
|
-
log(`Config path: ${configPath}`,
|
|
31
|
+
log(`Config file name: ${configFileName}`, 3);
|
|
32
|
+
log(`Config path: ${configPath}`, 3);
|
|
33
33
|
|
|
34
34
|
// Validate that config file is within the server root directory
|
|
35
35
|
// Allow absolute paths (user explicitly specified location)
|
|
36
36
|
if (!path.isAbsolute(configFileName)) {
|
|
37
37
|
const relativeConfigPath = path.relative(rootPath, configPath);
|
|
38
|
-
log(`Relative config path: ${relativeConfigPath}`,
|
|
39
|
-
log(`Starts with '..': ${relativeConfigPath.startsWith('..')}`,
|
|
38
|
+
log(`Relative config path: ${relativeConfigPath}`, 4);
|
|
39
|
+
log(`Starts with '..': ${relativeConfigPath.startsWith('..')}`, 4);
|
|
40
40
|
if (relativeConfigPath.startsWith('..') || path.isAbsolute(relativeConfigPath)) {
|
|
41
|
-
log(`Validation failed - throwing error`,
|
|
41
|
+
log(`Validation failed - throwing error`, 4);
|
|
42
42
|
throw new Error(`Config file must be within the server root directory. Config path: ${configPath}, Root path: ${rootPath}`);
|
|
43
43
|
}
|
|
44
|
-
log(`Validation passed`,
|
|
44
|
+
log(`Validation passed`, 4);
|
|
45
45
|
} else {
|
|
46
|
-
log(`Config file name is absolute, skipping validation`,
|
|
46
|
+
log(`Config file name is absolute, skipping validation`, 4);
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
log(`Loading config from: ${configPath}`,
|
|
49
|
+
log(`Loading config from: ${configPath}`, 3);
|
|
50
50
|
const configContent = await readFile(configPath, 'utf8');
|
|
51
51
|
const userConfig = JSON.parse(configContent);
|
|
52
52
|
config = {
|
|
@@ -70,14 +70,14 @@ export default async (flags, log) => {
|
|
|
70
70
|
...userConfig.cache
|
|
71
71
|
}
|
|
72
72
|
};
|
|
73
|
-
log('User config loaded and merged with defaults',
|
|
73
|
+
log('User config loaded and merged with defaults', 3);
|
|
74
74
|
} catch (e){
|
|
75
75
|
// Only fall back to default config for file reading/parsing errors
|
|
76
76
|
// Let validation errors propagate up
|
|
77
77
|
if (e.message.includes('Config file must be within the server root directory')) {
|
|
78
78
|
throw e;
|
|
79
79
|
}
|
|
80
|
-
log('Using default config (no config file found)',
|
|
80
|
+
log('Using default config (no config file found)', 3);
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
/*
|
|
@@ -88,10 +88,10 @@ export default async (flags, log) => {
|
|
|
88
88
|
dis.add("\\.config$");
|
|
89
89
|
dis.add("\\.git/");
|
|
90
90
|
config.disallowedRegex = [...dis];
|
|
91
|
-
log(`Config loaded with ${config.disallowedRegex.length} disallowed patterns`,
|
|
91
|
+
log(`Config loaded with ${config.disallowedRegex.length} disallowed patterns`, 3);
|
|
92
92
|
|
|
93
93
|
let files = await getFiles(rootPath, config, log);
|
|
94
|
-
log(`Initial scan found ${files.length} files`,
|
|
94
|
+
log(`Initial scan found ${files.length} files`, 2);
|
|
95
95
|
|
|
96
96
|
// Initialize middleware runner
|
|
97
97
|
const middlewareRunner = new MiddlewareRunner();
|
|
@@ -99,32 +99,32 @@ export default async (flags, log) => {
|
|
|
99
99
|
// Load built-in middleware based on config
|
|
100
100
|
if (config.middleware?.cors?.enabled) {
|
|
101
101
|
middlewareRunner.use(corsMiddleware(config.middleware.cors));
|
|
102
|
-
log('CORS middleware enabled',
|
|
102
|
+
log('CORS middleware enabled', 3);
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
if (config.middleware?.compression?.enabled) {
|
|
106
106
|
middlewareRunner.use(compressionMiddleware(config.middleware.compression));
|
|
107
|
-
log('Compression middleware enabled',
|
|
107
|
+
log('Compression middleware enabled', 3);
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
if (config.middleware?.rateLimit?.enabled) {
|
|
111
111
|
middlewareRunner.use(rateLimitMiddleware(config.middleware.rateLimit));
|
|
112
|
-
log('Rate limit middleware enabled',
|
|
112
|
+
log('Rate limit middleware enabled', 3);
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
if (config.middleware?.security?.enabled) {
|
|
116
116
|
middlewareRunner.use(securityMiddleware(config.middleware.security));
|
|
117
|
-
log('Security middleware enabled',
|
|
117
|
+
log('Security middleware enabled', 3);
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
if (config.middleware?.logging?.enabled) {
|
|
121
121
|
middlewareRunner.use(loggingMiddleware(config.middleware.logging, log));
|
|
122
|
-
log('Logging middleware enabled',
|
|
122
|
+
log('Logging middleware enabled', 3);
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
// Load custom middleware files
|
|
126
126
|
if (config.middleware?.custom && config.middleware.custom.length > 0) {
|
|
127
|
-
log(`Loading ${config.middleware.custom.length} custom middleware files`,
|
|
127
|
+
log(`Loading ${config.middleware.custom.length} custom middleware files`, 3);
|
|
128
128
|
|
|
129
129
|
for (const middlewarePath of config.middleware.custom) {
|
|
130
130
|
try {
|
|
@@ -134,7 +134,7 @@ export default async (flags, log) => {
|
|
|
134
134
|
|
|
135
135
|
if (typeof customMiddleware === 'function') {
|
|
136
136
|
middlewareRunner.use(customMiddleware(config.middleware));
|
|
137
|
-
log(`Custom middleware loaded: ${middlewarePath}`,
|
|
137
|
+
log(`Custom middleware loaded: ${middlewarePath}`, 3);
|
|
138
138
|
} else {
|
|
139
139
|
log(`Custom middleware error: ${middlewarePath} does not export a default function`, 1);
|
|
140
140
|
}
|
|
@@ -159,7 +159,7 @@ export default async (flags, log) => {
|
|
|
159
159
|
const wildcardRoutes = new Map();
|
|
160
160
|
|
|
161
161
|
if (config.customRoutes && Object.keys(config.customRoutes).length > 0) {
|
|
162
|
-
log(`Processing ${Object.keys(config.customRoutes).length} custom routes`,
|
|
162
|
+
log(`Processing ${Object.keys(config.customRoutes).length} custom routes`, 3);
|
|
163
163
|
for (const [urlPath, filePath] of Object.entries(config.customRoutes)) {
|
|
164
164
|
// Check if this is a wildcard route
|
|
165
165
|
if (urlPath.includes('*')) {
|
|
@@ -167,12 +167,12 @@ export default async (flags, log) => {
|
|
|
167
167
|
const resolvedPath = path.isAbsolute(filePath) ? filePath : path.resolve(rootPath, filePath);
|
|
168
168
|
// Store wildcard routes separately for pattern matching
|
|
169
169
|
wildcardRoutes.set(urlPath, resolvedPath);
|
|
170
|
-
log(`Wildcard route mapped: ${urlPath} -> ${resolvedPath}`,
|
|
170
|
+
log(`Wildcard route mapped: ${urlPath} -> ${resolvedPath}`, 3);
|
|
171
171
|
} else {
|
|
172
172
|
// Resolve the file path relative to rootPath if relative, otherwise use absolute path
|
|
173
173
|
const resolvedPath = path.isAbsolute(filePath) ? filePath : path.resolve(rootPath, filePath);
|
|
174
174
|
customRoutes.set(urlPath, resolvedPath);
|
|
175
|
-
log(`Custom route mapped: ${urlPath} -> ${resolvedPath}`,
|
|
175
|
+
log(`Custom route mapped: ${urlPath} -> ${resolvedPath}`, 3);
|
|
176
176
|
}
|
|
177
177
|
}
|
|
178
178
|
}
|
|
@@ -259,27 +259,36 @@ export default async (flags, log) => {
|
|
|
259
259
|
log(`Path ${requestPath} added to dynamic blacklist after ${newAttempts} failed attempts`, 1);
|
|
260
260
|
}
|
|
261
261
|
|
|
262
|
-
log(`Rescan attempt ${newAttempts}/${config.maxRescanAttempts} for: ${requestPath}`,
|
|
262
|
+
log(`Rescan attempt ${newAttempts}/${config.maxRescanAttempts} for: ${requestPath}`, 3);
|
|
263
263
|
return newAttempts;
|
|
264
264
|
};
|
|
265
265
|
|
|
266
266
|
const requestHandler = async (req, res) => {
|
|
267
267
|
await middlewareRunner.run(req, res, async () => {
|
|
268
268
|
const requestPath = req.url.split('?')[0];
|
|
269
|
-
log(`${req.method} ${requestPath}`,
|
|
269
|
+
log(`${req.method} ${requestPath}`, 4);
|
|
270
270
|
|
|
271
271
|
|
|
272
272
|
// Check custom routes first (allow outside rootPath)
|
|
273
|
-
log(`customRoutes keys: ${Array.from(customRoutes.keys()).join(', ')}`,
|
|
273
|
+
log(`customRoutes keys: ${Array.from(customRoutes.keys()).join(', ')}`, 4);
|
|
274
274
|
// Normalize requestPath and keys for matching
|
|
275
275
|
const normalizePath = p => {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
276
|
+
try {
|
|
277
|
+
let np = decodeURIComponent(p);
|
|
278
|
+
if (!np.startsWith('/')) np = '/' + np;
|
|
279
|
+
if (np.length > 1 && np.endsWith('/')) np = np.slice(0, -1);
|
|
280
|
+
return np;
|
|
281
|
+
} catch (e) {
|
|
282
|
+
log(`Warning: Failed to decode URI component "${p}": ${e.message}`, 1);
|
|
283
|
+
// Return the original path if decoding fails
|
|
284
|
+
let np = p;
|
|
285
|
+
if (!np.startsWith('/')) np = '/' + np;
|
|
286
|
+
if (np.length > 1 && np.endsWith('/')) np = np.slice(0, -1);
|
|
287
|
+
return np;
|
|
288
|
+
}
|
|
280
289
|
};
|
|
281
290
|
const normalizedRequestPath = normalizePath(requestPath);
|
|
282
|
-
log(`Normalized requestPath: ${normalizedRequestPath}`,
|
|
291
|
+
log(`Normalized requestPath: ${normalizedRequestPath}`, 4);
|
|
283
292
|
let matchedKey = null;
|
|
284
293
|
for (const key of customRoutes.keys()) {
|
|
285
294
|
if (normalizePath(key) === normalizedRequestPath) {
|
|
@@ -289,14 +298,14 @@ export default async (flags, log) => {
|
|
|
289
298
|
}
|
|
290
299
|
if (matchedKey) {
|
|
291
300
|
const customFilePath = customRoutes.get(matchedKey);
|
|
292
|
-
log(`Serving custom route: ${normalizedRequestPath} -> ${customFilePath}`,
|
|
301
|
+
log(`Serving custom route: ${normalizedRequestPath} -> ${customFilePath}`, 3);
|
|
293
302
|
try {
|
|
294
303
|
const { stat } = await import('fs/promises');
|
|
295
304
|
try {
|
|
296
305
|
await stat(customFilePath);
|
|
297
|
-
log(`Custom route file exists: ${customFilePath}`,
|
|
306
|
+
log(`Custom route file exists: ${customFilePath}`, 4);
|
|
298
307
|
} catch (e) {
|
|
299
|
-
log(`Custom route file does NOT exist: ${customFilePath}`,
|
|
308
|
+
log(`Custom route file does NOT exist: ${customFilePath}`, 1);
|
|
300
309
|
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
301
310
|
res.end('Custom route file not found');
|
|
302
311
|
return;
|
|
@@ -309,7 +318,7 @@ export default async (flags, log) => {
|
|
|
309
318
|
res.end(fileContent);
|
|
310
319
|
return; // Successfully served custom route
|
|
311
320
|
} catch (error) {
|
|
312
|
-
log(`Error serving custom route ${normalizedRequestPath}: ${error.message}`,
|
|
321
|
+
log(`Error serving custom route ${normalizedRequestPath}: ${error.message}`, 1);
|
|
313
322
|
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
|
314
323
|
res.end('Internal Server Error');
|
|
315
324
|
return;
|
|
@@ -320,17 +329,17 @@ export default async (flags, log) => {
|
|
|
320
329
|
const wildcardMatch = findWildcardRoute(requestPath);
|
|
321
330
|
if (wildcardMatch) {
|
|
322
331
|
const resolvedFilePath = resolveWildcardPath(wildcardMatch.filePath, wildcardMatch.matches);
|
|
323
|
-
log(`Serving wildcard route: ${requestPath} -> ${resolvedFilePath}`,
|
|
332
|
+
log(`Serving wildcard route: ${requestPath} -> ${resolvedFilePath}`, 3);
|
|
324
333
|
try {
|
|
325
334
|
const fileContent = await readFile(resolvedFilePath);
|
|
326
335
|
const fileExtension = path.extname(resolvedFilePath).toLowerCase().slice(1);
|
|
327
336
|
const mimeType = config.allowedMimes[fileExtension] || 'application/octet-stream';
|
|
328
|
-
log(`Serving wildcard file as ${mimeType} (${fileContent.length} bytes)`,
|
|
337
|
+
log(`Serving wildcard file as ${mimeType} (${fileContent.length} bytes)`, 4);
|
|
329
338
|
res.writeHead(200, { 'Content-Type': mimeType });
|
|
330
339
|
res.end(fileContent);
|
|
331
340
|
return; // Successfully served wildcard route
|
|
332
341
|
} catch (error) {
|
|
333
|
-
log(`Error serving wildcard route ${requestPath}: ${error.message}`,
|
|
342
|
+
log(`Error serving wildcard route ${requestPath}: ${error.message}`, 1);
|
|
334
343
|
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
|
335
344
|
res.end('Internal Server Error');
|
|
336
345
|
return;
|
|
@@ -103,5 +103,51 @@ export default {
|
|
|
103
103
|
process.chdir(prev);
|
|
104
104
|
});
|
|
105
105
|
pass('custom+wildcard');
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
'handles malformed URLs gracefully': async ({pass, fail, log}) => {
|
|
109
|
+
await withTestDir(async (dir) => {
|
|
110
|
+
const prev = process.cwd();
|
|
111
|
+
process.chdir(dir);
|
|
112
|
+
const flags = {root: '.', logging: 0, scan: false};
|
|
113
|
+
const logFn = () => {};
|
|
114
|
+
const handler = await router(flags, logFn);
|
|
115
|
+
const server = http.createServer(handler);
|
|
116
|
+
const port = randomPort();
|
|
117
|
+
await new Promise(r => server.listen(port, r));
|
|
118
|
+
await new Promise(r => setTimeout(r, 50));
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
// Test various malformed URLs that could cause decodeURIComponent to throw
|
|
122
|
+
const malformedUrls = [
|
|
123
|
+
'/test%', // Incomplete percent encoding
|
|
124
|
+
'/test%2', // Incomplete percent encoding
|
|
125
|
+
'/test%G1', // Invalid hex characters
|
|
126
|
+
'/test%ZZ', // Invalid hex characters
|
|
127
|
+
'/test%1G', // Invalid hex characters
|
|
128
|
+
'/%E0%A4%A', // Incomplete UTF-8 sequence
|
|
129
|
+
'/%C0%80' // Overlong UTF-8 encoding
|
|
130
|
+
];
|
|
131
|
+
|
|
132
|
+
for (const url of malformedUrls) {
|
|
133
|
+
log(`Testing malformed URL: ${url}`);
|
|
134
|
+
const response = await httpGet(`http://localhost:${port}${url}`);
|
|
135
|
+
// Server should handle the request gracefully (return 404 or serve content)
|
|
136
|
+
// and not crash with URIError
|
|
137
|
+
if (response.res.statusCode !== 404 && response.res.statusCode !== 200) {
|
|
138
|
+
throw new Error(`Unexpected status code ${response.res.statusCode} for URL ${url}`);
|
|
139
|
+
}
|
|
140
|
+
log(`URL ${url} handled gracefully with status ${response.res.statusCode}`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
server.close();
|
|
144
|
+
process.chdir(prev);
|
|
145
|
+
pass('malformed URLs handled gracefully');
|
|
146
|
+
} catch (e) {
|
|
147
|
+
server.close();
|
|
148
|
+
process.chdir(prev);
|
|
149
|
+
fail(`Error handling malformed URL: ${e.message}`);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
106
152
|
}
|
|
107
153
|
};
|