Skip to content

Commit e7b31c7

Browse files
committed
tests: prevent possibility of path traversal through tests
This is not an actual security issue for how we use it for now, but sonarcloud was shouting about it and I thought it was indeed something that merited more attention. I tried to fix that hole.
1 parent 1085820 commit e7b31c7

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

tests/contents/server.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,10 @@ function generateUrlListHtml(urls, baseUrl) {
610610
}
611611

612612
async function prepareStaticFile(baseDir, url) {
613-
const filePath = path.join(baseDir, url);
614-
if (!filePath.startsWith(baseDir)) {
613+
const filePath = path.resolve(baseDir, url);
614+
const normalizedBase = path.resolve(baseDir);
615+
const relative = path.relative(normalizedBase, filePath);
616+
if (relative.startsWith("..") || path.isAbsolute(relative)) {
615617
return null;
616618
}
617619
const exists = await new Promise((resolve) => {

0 commit comments

Comments
 (0)