Skip to content

Commit 4f5350d

Browse files
Potential fix for code scanning alert no. 512: Uncontrolled data used in path expression
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent ca9f998 commit 4f5350d

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

tests/e2e/versioning.spec.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,21 @@ function createServer(rootDir) {
1919
return;
2020
}
2121
// static files
22-
const filePath = path.join(rootDir, decodeURIComponent(req.url.split('?')[0]));
22+
let rawPath = decodeURIComponent(req.url.split('?')[0]);
23+
let resolvedPath = path.resolve(rootDir, '.' + rawPath); // prevent rootDir + "/etc/passwd"
24+
let filePath;
25+
try {
26+
filePath = fs.realpathSync(resolvedPath);
27+
} catch (e) {
28+
res.writeHead(404);
29+
res.end('Not Found');
30+
return;
31+
}
32+
if (!filePath.startsWith(rootDir)) {
33+
res.writeHead(403);
34+
res.end('Forbidden');
35+
return;
36+
}
2337
fs.readFile(filePath, (err, data) => {
2438
if (err) {
2539
res.writeHead(404);

0 commit comments

Comments
 (0)