Skip to content

Commit ef0e910

Browse files
Potential fix for code scanning alert no. 513: 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 4f5350d commit ef0e910

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

tests/e2e/versioning.behavior.spec.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@ function createServer(rootDir) {
1515
res.end('<html><body><div id="content">Draft V2</div></body></html>');
1616
return;
1717
}
18-
const filePath = path.join(rootDir, decodeURIComponent(req.url.split('?')[0]));
19-
fs.readFile(filePath, (err, data) => {
18+
// Normalize and validate the file path
19+
const requestedPath = decodeURIComponent(req.url.split('?')[0]);
20+
const safePath = path.resolve(rootDir, "." + requestedPath); // ensure requestedPath is not absolute
21+
if (!safePath.startsWith(rootDir)) {
22+
res.writeHead(403);
23+
res.end('Forbidden');
24+
return;
25+
}
26+
fs.readFile(safePath, (err, data) => {
2027
if (err) {
2128
res.writeHead(404);
2229
res.end('Not Found');
2330
return;
2431
}
25-
const ext = path.extname(filePath);
32+
const ext = path.extname(safePath);
2633
const types = { '.html': 'text/html', '.js': 'application/javascript', '.css': 'text/css' };
2734
res.writeHead(200, { 'Content-Type': types[ext] || 'application/octet-stream' });
2835
res.end(data);

0 commit comments

Comments
 (0)