Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ const fs = require("fs");
*/
function findFiles(parent, directory, name) {
const names = [].concat(name);
const chunks = path.resolve(parent, directory).split(path.sep);
const resolvedParent = path.resolve(parent);
const resolvedDir = path.resolve(resolvedParent, directory);

// If the resolved starting directory escapes the parent (e.g. a
// path.relative() result like "../../outside"), do not search outside the
// workspace β€” return null immediately.
if (resolvedDir !== resolvedParent &&
!resolvedDir.startsWith(resolvedParent + path.sep)) {
return null;
}

const chunks = resolvedDir.split(path.sep);

while (chunks.length) {
let currentDir = chunks.join(path.sep);
Expand All @@ -23,7 +34,7 @@ function findFiles(parent, directory, name) {
return filePath;
}
}
if (parent === currentDir) {
if (resolvedParent === currentDir) {
break;
}
chunks.pop();
Expand Down
14 changes: 14 additions & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,18 @@ describe("findFiles", () => {
const result = findFiles(path.join(tmpRoot, "single"), ".", "phpcs.xml");
assert.equal(result, expected);
});

test("returns null when directory resolves outside parent (no workspace escape)", () => {
// Simulates a file opened from outside the workspace, where
// path.relative(workspaceRoot, filePath) produces "../../outside".
// findFiles must NOT search outside the parent boundary.
mkFile("outside-config.xml"); // exists above the workspace root
mkDir("workspace");
const result = findFiles(
path.join(tmpRoot, "workspace"),
path.join("..", ".."), // escapes tmpRoot/workspace
"outside-config.xml"
);
assert.equal(result, null);
});
});