Skip to content

Commit d498314

Browse files
mprobstmhevery
authored andcommitted
fix(zone.js): a path traversal attack in test (#32392)
`simple-server.js` is vulnerable to a trivial path traversal attack, i.e. an attacker can supply a path like `../../etc/passwd` to read arbitrary files on the server. This change fixes the issue by properly resolving the path, and then only serving files under the current directory (as intended). This is not really a security issue, given the code is not part of Angular, but rather just testing infrastructure for Angular itself, and the CI servers are not expected to contain confidential information, but still worth fixing for code hygiene. PR Close #32392
1 parent 8dc3f36 commit d498314

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

packages/zone.js/simple-server.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,31 @@ let server;
1313

1414
const localFolder = __dirname;
1515

16+
function writeNotFound(res) {
17+
res.writeHead(404, {'Content-Type': 'text/html'});
18+
res.end('<h1>404, Not Found!</h1>');
19+
}
20+
1621
function requestHandler(req, res) {
1722
if (req.url === '/close') {
1823
res.end('server closing');
1924
setTimeout(() => { process.exit(0); }, 1000);
2025
} else {
21-
const file = localFolder + req.url;
26+
const file = path.resolve(localFolder, req.url);
27+
if (!file.startsWith(localFolder + '/')) {
28+
writeNotFound(res);
29+
return;
30+
}
2231

2332
fs.readFile(file, function(err, contents) {
2433
if (!err) {
2534
res.end(contents);
2635
} else {
27-
res.writeHead(404, {'Content-Type': 'text/html'});
28-
res.end('<h1>404, Not Found!</h1>');
36+
writeNotFound(res);
37+
return;
2938
};
3039
});
3140
};
3241
};
3342

34-
server = http.createServer(requestHandler).listen(8080);
43+
server = http.createServer(requestHandler).listen(8080);

0 commit comments

Comments
 (0)