import path from "node:path"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { _getTrustedDirs, _resetResolveSystemBin, resolveSystemBin } from "./resolve-system-bin.js"; import {
_resetWindowsInstallRootsForTests,
getWindowsInstallRoots,
getWindowsProgramFilesRoots,
} from "./windows-install-roots.js";
let executables: Set<string>;
function addExecutables(...paths: string[]): void { for (const candidate of paths) {
executables.add(candidate);
}
}
function expectDirsContainAll(dirs: readonly string[], expected: readonly string[]): void { for (const dir of expected) {
expect(dirs).toContain(dir);
}
}
function expectDirsExcludeAll(dirs: readonly string[], excluded: readonly string[]): void { for (const dir of excluded) {
expect(dirs).not.toContain(dir);
}
}
describe("resolveSystemBin", () => {
it("returns null when binary is not in any trusted directory", () => {
expect(resolveSystemBin("nonexistent")).toBeNull();
});
if (process.platform !== "win32") {
it("resolves a binary found in /usr/bin", () => {
executables.add("/usr/bin/ffmpeg");
expect(resolveSystemBin("ffmpeg")).toBe("/usr/bin/ffmpeg");
});
it.each([
{
name: "does NOT resolve a binary found in /usr/local/bin with strict trust",
executable: "/usr/local/bin/openssl",
command: "openssl",
checkStrict: true,
},
{
name: "does NOT resolve a binary found in /opt/homebrew/bin with strict trust",
executable: "/opt/homebrew/bin/ffmpeg",
command: "ffmpeg",
checkStrict: true,
},
{
name: "does NOT resolve a binary from a user-writable directory like ~/.local/bin",
executable: "/home/testuser/.local/bin/ffmpeg",
command: "ffmpeg",
checkStrict: false,
},
])("$name", ({ executable, command, checkStrict }) => {
addExecutables(executable);
expect(resolveSystemBin(command)).toBeNull(); if (checkStrict) {
expect(resolveSystemBin(command, { trust: "strict" })).toBeNull();
}
});
it("prefers /usr/bin over /usr/local/bin (first match wins)", () => {
executables.add("/usr/bin/openssl");
executables.add("/usr/local/bin/openssl");
expect(resolveSystemBin("openssl")).toBe("/usr/bin/openssl");
});
it("caches results across calls", () => {
executables.add("/usr/bin/ffmpeg");
expect(resolveSystemBin("ffmpeg")).toBe("/usr/bin/ffmpeg");
it("extraDirs results do not poison the cache for callers without extraDirs", () => { const untrustedDir = "/home/user/.local/bin";
executables.add(`${untrustedDir}/ffmpeg`);
if (process.platform === "darwin") {
it.each(["/opt/homebrew/bin/ffmpeg", "/usr/local/bin/ffmpeg"])( "resolves a binary in %s with standard trust on macOS",
(executable) => {
addExecutables(executable);
expect(resolveSystemBin("ffmpeg", { trust: "standard" })).toBe(executable);
},
);
it("prefers /usr/bin over /opt/homebrew/bin with standard trust", () => {
executables.add("/usr/bin/ffmpeg");
executables.add("/opt/homebrew/bin/ffmpeg");
expect(resolveSystemBin("ffmpeg", { trust: "standard" })).toBe("/usr/bin/ffmpeg");
});
it("standard trust results do not poison the strict cache", () => {
executables.add("/opt/homebrew/bin/ffmpeg");
expect(resolveSystemBin("ffmpeg", { trust: "standard" })).toBe("/opt/homebrew/bin/ffmpeg");
expect(resolveSystemBin("ffmpeg")).toBeNull();
});
if (process.platform === "linux") {
it("resolves a binary in /usr/local/bin with standard trust on Linux", () => {
addExecutables("/usr/local/bin/ffmpeg");
expect(resolveSystemBin("ffmpeg", { trust: "standard" })).toBe("/usr/local/bin/ffmpeg");
});
it("prefers /usr/bin over /usr/local/bin with standard trust on Linux", () => {
executables.add("/usr/bin/ffmpeg");
executables.add("/usr/local/bin/ffmpeg");
expect(resolveSystemBin("ffmpeg", { trust: "standard" })).toBe("/usr/bin/ffmpeg");
});
}
});
describe("trusted directory list", () => {
it("never includes user-writable home directories", () => { const dirs = _getTrustedDirs(); for (const dir of dirs) {
expect(dir, `${dir} should not be user-writable`).not.toMatch(/\.(local|bun|yarn)/);
expect(dir, `${dir} should not be a pnpm dir`).not.toContain("pnpm");
}
});
if (process.platform !== "win32") {
it("includes base Unix system directories only", () => { const dirs = _getTrustedDirs();
expectDirsContainAll(dirs, ["/usr/bin", "/bin", "/usr/sbin", "/sbin"]);
expectDirsExcludeAll(dirs, ["/usr/local/bin"]);
});
if (process.platform === "darwin") {
it("does not include /opt/homebrew/bin in strict trust on macOS", () => {
expectDirsExcludeAll(_getTrustedDirs("strict"), ["/opt/homebrew/bin", "/usr/local/bin"]);
});
it("includes /opt/homebrew/bin and /usr/local/bin in standard trust on macOS", () => { const dirs = _getTrustedDirs("standard");
expectDirsContainAll(dirs, ["/opt/homebrew/bin", "/usr/local/bin"]);
});
it("places Homebrew dirs after system dirs in standard trust", () => { const dirs = [..._getTrustedDirs("standard")]; const usrBinIdx = dirs.indexOf("/usr/bin"); const brewIdx = dirs.indexOf("/opt/homebrew/bin"); const localIdx = dirs.indexOf("/usr/local/bin");
expect(usrBinIdx).toBeGreaterThanOrEqual(0);
expect(brewIdx).toBeGreaterThan(usrBinIdx);
expect(localIdx).toBeGreaterThan(usrBinIdx);
});
it("standard trust is a superset of strict trust on macOS", () => { const strict = _getTrustedDirs("strict"); const standard = _getTrustedDirs("standard"); for (const dir of strict) {
expect(standard, `standard trust should include strict dir ${dir}`).toContain(dir);
}
});
}
if (process.platform === "linux") {
it("includes Linux system-managed directories", () => { const dirs = _getTrustedDirs();
expectDirsContainAll(dirs, ["/run/current-system/sw/bin", "/snap/bin"]);
});
it("includes /usr/local/bin in standard trust on Linux", () => { const dirs = _getTrustedDirs("standard");
expect(dirs).toContain("/usr/local/bin");
});
it("places /usr/local/bin after /usr/bin in standard trust on Linux", () => { const dirs = [..._getTrustedDirs("standard")]; const usrBinIdx = dirs.indexOf("/usr/bin"); const usrLocalBinIdx = dirs.indexOf("/usr/local/bin");
expect(usrBinIdx).toBeGreaterThanOrEqual(0);
expect(usrLocalBinIdx).toBeGreaterThan(usrBinIdx);
});
}
if (process.platform !== "darwin" && process.platform !== "linux") {
it("standard trust equals strict trust on platforms without expansion", () => { const strict = _getTrustedDirs("strict"); const standard = _getTrustedDirs("standard");
expect(standard).toEqual(strict);
});
}
if (process.platform === "win32") {
it("includes Windows system directories", () => { const dirs = _getTrustedDirs();
expect(dirs).toContain(path.win32.join(getWindowsInstallRoots().systemRoot, "System32"));
});
it("includes Program Files OpenSSL and ffmpeg paths", () => { const dirs = _getTrustedDirs(); for (const programFilesRoot of getWindowsProgramFilesRoots()) {
expect(dirs).toContain(path.win32.join(programFilesRoot, "OpenSSL-Win64", "bin"));
expect(dirs).toContain(path.win32.join(programFilesRoot, "ffmpeg", "bin"));
}
});
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.