* fix(win32): probe .cmd/.bat runtime shims via cmd.exe to avoid spawn EINVAL Node >= 18.20 refuses to spawn .cmd/.bat files without a shell (CVE-2024-27980 mitigation) and throws spawn EINVAL. On Windows, the npm and npx PATH shims are .CMD files, so doctor's runtime toolchain probes always failed even on a healthy install. Route batch-file candidates through cmd.exe /d /s /c with the command line as a single quoted argument and windowsVerbatimArguments. * fix(win32): thread platform through version probe and harden batch quoting - windowsBatchCommand takes platform/systemRoot params instead of reading process.platform directly, so it is unit-testable off Windows - double embedded quotes in the cmd.exe /c string to block breakout - replace spawn-based win32 tests with pure helper units plus a win32-only temp .bat probe --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
81 lines
2.5 KiB
TypeScript
81 lines
2.5 KiB
TypeScript
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
executeVersionForTesting as executeVersion,
|
|
windowsBatchCommandForTesting as windowsBatchCommand,
|
|
} from "./RuntimeExecutableDiagnostics.js";
|
|
|
|
const roots: string[] = [];
|
|
|
|
afterEach(async () => {
|
|
await Promise.all(
|
|
roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
|
|
);
|
|
});
|
|
|
|
describe("windowsBatchCommand", () => {
|
|
it("returns null off Windows even for batch files", () => {
|
|
expect(windowsBatchCommand("C:\\tools\\npm.cmd", "linux")).toBeNull();
|
|
expect(windowsBatchCommand("C:\\tools\\npm.cmd", "darwin")).toBeNull();
|
|
});
|
|
|
|
it("returns null for non-batch candidates on win32", () => {
|
|
expect(windowsBatchCommand("C:\\tools\\node.exe", "win32")).toBeNull();
|
|
expect(windowsBatchCommand("/usr/local/bin/npm", "win32")).toBeNull();
|
|
});
|
|
|
|
it.each(["npm.cmd", "npm.CMD", "tool.bat", "tool.BAT"])(
|
|
"routes %s through cmd.exe on win32",
|
|
(file) => {
|
|
expect(windowsBatchCommand(`C:\\tools\\${file}`, "win32")).toEqual({
|
|
executable: "C:\\Windows\\System32\\cmd.exe",
|
|
arguments: ["/d", "/s", "/c", `"C:\\tools\\${file}" --version`],
|
|
});
|
|
},
|
|
);
|
|
|
|
it("preserves paths with spaces in a single /c argument", () => {
|
|
const command = windowsBatchCommand(
|
|
"C:\\Program Files\\nodejs\\npm.cmd",
|
|
"win32",
|
|
);
|
|
expect(command?.arguments[3]).toBe(
|
|
'"C:\\Program Files\\nodejs\\npm.cmd" --version',
|
|
);
|
|
});
|
|
|
|
it("doubles embedded quotes so they cannot break out of the /c string", () => {
|
|
const command = windowsBatchCommand('C:\\tools\\evil".cmd', "win32");
|
|
expect(command?.arguments[3]).toBe('"C:\\tools\\evil"".cmd" --version');
|
|
});
|
|
|
|
it("honours an explicit SystemRoot", () => {
|
|
const command = windowsBatchCommand(
|
|
"C:\\tools\\npm.cmd",
|
|
"win32",
|
|
"D:\\Win",
|
|
);
|
|
expect(command?.executable).toBe("D:\\Win\\System32\\cmd.exe");
|
|
});
|
|
});
|
|
|
|
describe.runIf(process.platform === "win32")("executeVersion on win32", () => {
|
|
it("probes a real .bat shim without spawn EINVAL", async () => {
|
|
const dir = await mkdtemp(join(tmpdir(), "rea-win32-"));
|
|
roots.push(dir);
|
|
const shim = join(dir, "probe.bat");
|
|
await writeFile(shim, "@echo 9.9.9\r\n");
|
|
const result = await executeVersion(
|
|
shim,
|
|
10_000,
|
|
process.env.PATH ?? "",
|
|
"win32",
|
|
);
|
|
expect(result).toMatchObject({ ok: true, version: "9.9.9" });
|
|
});
|
|
});
|