Skip to content
Closed
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
32 changes: 16 additions & 16 deletions src/update/npm-invocation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,17 @@ function escapeCmdCommand(command) {
return command.replace(CMD_META, "^$1");
}

/**
* Whether a PATH entry *is* the current directory. The hijack this guards against is
* cmd.exe resolving a bare `npm` out of the directory opencodex was launched from, so
* only that exact directory has to be skipped — every candidate we hand to spawn is an
* absolute path, which is what actually defeats the implicit cwd-first search.
*
* Deliberately not a subtree test: npm's default Windows global prefix is
* `%AppData%\npm` (`C:\Users\x\AppData\Roaming\npm`), so excluding everything under the
* cwd would fail closed for anyone whose shell sits in their home directory — a normal
* setup, not the untrusted-project case this hardening is for.
*/
function isCurrentDirectory(cwd, entry) {
const left = win32.resolve(entry);
const right = win32.resolve(cwd);
return left.toLowerCase() === right.toLowerCase();
function isInside(root, candidate) {
const relative = win32.relative(win32.resolve(root), win32.resolve(candidate));
return relative === "" || (
relative !== ".."
&& !relative.startsWith(`..${win32.sep}`)
&& !win32.isAbsolute(relative)
);
}

function isSamePath(left, right) {
return win32.resolve(left).toLowerCase() === win32.resolve(right).toLowerCase();
}

function cleanPathEntry(entry) {
Expand All @@ -43,6 +39,9 @@ export function resolveNpmCommand(
if (platform !== "win32") return "npm";
const exists = deps.exists ?? existsSync;
const cwd = deps.cwd ?? process.cwd();
const appDataNpm = env.APPDATA && win32.isAbsolute(env.APPDATA)
? win32.join(env.APPDATA, "npm")
: null;
const extensions = (env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD")
.split(";")
.filter(Boolean);
Expand All @@ -53,7 +52,8 @@ export function resolveNpmCommand(

for (const entry of pathEntries) {
if (!win32.isAbsolute(entry)) continue;
if (isCurrentDirectory(cwd, entry)) continue;
if (isSamePath(entry, cwd)) continue;
if (isInside(cwd, entry) && (!appDataNpm || !isSamePath(entry, appDataNpm))) continue;
Comment thread
luvs01 marked this conversation as resolved.
for (const extension of extensions) {
const candidate = win32.join(entry, `npm${extension.toLowerCase()}`);
if (exists(candidate)) return win32.resolve(candidate);
Expand Down
47 changes: 47 additions & 0 deletions tests/update/update-npm-invocation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe("Windows npm update invocation", () => {
const appDataNpm = `${home}\\AppData\\Roaming\\npm\\npm.cmd`;
const env = {
PATH: `${home}\\AppData\\Roaming\\npm`,
APPDATA: `${home}\\AppData\\Roaming`,
PATHEXT: ".CMD",
SystemRoot: "C:\\Windows",
};
Expand All @@ -55,6 +56,28 @@ describe("Windows npm update invocation", () => {
})).toBe(appDataNpm);
});

test("ignores npm candidates in current-directory subtrees", () => {
const projectNpm = `${cwd}\\node_modules\\.bin\\npm.cmd`;
const env = {
PATH: `${cwd}\\node_modules\\.bin;C:\\Program Files\\nodejs`,
PATHEXT: ".CMD",
SystemRoot: "C:\\Windows",
};
const existing = new Set([projectNpm, trustedNpm]);

expect(resolveNpmCommand("win32", env, {
cwd,
exists: path => existing.has(path),
})).toBe(trustedNpm);

const invocation = npmInvocation(["install", "-g", "pkg@latest"], "win32", env, {
cwd,
exists: path => existing.has(path),
});
expect(invocation?.args.at(-1)).toContain("nodejs\\npm.cmd");
expect(invocation?.args.at(-1)).not.toContain("node_modules\\.bin\\npm.cmd");
});

test("still skips the current directory when it is a PATH entry under the home tree", () => {
// The narrower rule must not lose the actual defense: a PATH entry equal to the
// launch directory stays excluded even though it sits inside the user's home.
Expand Down Expand Up @@ -92,4 +115,28 @@ describe("Windows npm update invocation", () => {
exists: path => path === `${cwd}\\npm.cmd`,
})).toBeNull();
});

test("rejects the launch directory even when it is the trusted global npm prefix", () => {
// Regression: with cwd === %APPDATA%\npm the PATH entry equal to the launch
// directory satisfied both the subtree check and the trusted-prefix exception,
// re-admitting the exact current-directory candidate this hardening forbids.
const appData = "C:\\Users\\dev\\AppData\\Roaming";
const appDataNpmDir = `${appData}\\npm`;
const env = {
PATH: appDataNpmDir,
APPDATA: appData,
PATHEXT: ".CMD",
SystemRoot: "C:\\Windows",
};
const only = `${appDataNpmDir}\\npm.cmd`;

expect(resolveNpmCommand("win32", env, {
cwd: appDataNpmDir,
exists: path => path === only,
})).toBeNull();
expect(npmInvocation(["install", "-g", "pkg@latest"], "win32", env, {
cwd: appDataNpmDir,
exists: path => path === only,
})).toBeNull();
});
});
Loading