diff --git a/packages/cli/src/utils/windowsExeSearch.test.ts b/packages/cli/src/utils/windowsExeSearch.test.ts index 5e38edf5e9..cc58432b18 100644 --- a/packages/cli/src/utils/windowsExeSearch.test.ts +++ b/packages/cli/src/utils/windowsExeSearch.test.ts @@ -1,4 +1,13 @@ -import { existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync } from "node:fs"; +import { spawnSync } from "node:child_process"; +import { + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + realpathSync, + rmSync, + writeFileSync, +} from "node:fs"; import { tmpdir } from "node:os"; import { delimiter, dirname, isAbsolute, join, win32 } from "node:path"; import { fileURLToPath } from "node:url"; @@ -142,6 +151,33 @@ describe("searchOnlyTrustedFolders", () => { expect(env.PATH?.startsWith(project)).toBe(false); }); + it.skipIf(process.platform === "win32")( + "a helper started by bare name never runs a same-named file from the project folder", + () => { + const { project, outside } = inProject(); + const marker = join(outside[1]!, "ran"); + mkdirSync(join(project, "bin")); + for (const dir of [project, join(project, "bin")]) { + writeFileSync(join(dir, "hf-planted-helper"), `#!/bin/sh\necho ran > "${marker}"\n`, { + mode: 0o755, + }); + } + const raw: NodeJS.ProcessEnv = { PATH: [".", "", "bin", outside[0]].join(delimiter) }; + const run = (env: NodeJS.ProcessEnv) => + spawnSync("hf-planted-helper", [], { cwd: project, env, stdio: "ignore" }); + + expect(run(raw).status).toBe(0); + expect(existsSync(marker)).toBe(true); + rmSync(marker); + + const env = { ...raw }; + searchOnlyTrustedFolders(env, process.platform); + + expect((run(env).error as NodeJS.ErrnoException | undefined)?.code).toBe("ENOENT"); + expect(existsSync(marker)).toBe(false); + }, + ); + it("adds no PATH when there was none", () => { inProject(); const env: NodeJS.ProcessEnv = {}; diff --git a/packages/parsers/src/trustedExecutable.test.ts b/packages/parsers/src/trustedExecutable.test.ts index 2eb106a006..7ddddc4165 100644 --- a/packages/parsers/src/trustedExecutable.test.ts +++ b/packages/parsers/src/trustedExecutable.test.ts @@ -82,6 +82,48 @@ describe("findTrustedExecutable", () => { }, ); + it.skipIf(process.platform === "win32")( + "run from a subfolder, skips the project's node_modules/.bin and every folder inside the project", + () => { + const project = tempDir("hf-trusted-project-"); + const tools = tempDir("hf-trusted-tools-"); + const bin = join(project, "node_modules", ".bin"); + const scenes = join(project, "scenes", "intro"); + mkdirSync(bin, { recursive: true }); + mkdirSync(scenes, { recursive: true }); + writeFileSync(join(project, "package.json"), "{}"); + for (const dir of [bin, project, tools]) + writeFileSync(join(dir, "ffmpeg"), "", { mode: 0o755 }); + vi.spyOn(process, "cwd").mockReturnValue(scenes); + vi.stubEnv("PATH", [bin, project, tools, "/usr/bin", "/bin"].join(delimiter)); + + expect(findTrustedExecutable("ffmpeg")).toBe(join(tools, "ffmpeg")); + expect(trustedPathDirs()).not.toContain(bin); + }, + ); + + it.skipIf(process.platform === "win32")( + "in a monorepo, skips the repository's hoisted node_modules/.bin as well as the package's", + () => { + const repo = tempDir("hf-trusted-repo-"); + const tools = tempDir("hf-trusted-tools-"); + const pkg = join(repo, "packages", "film"); + const hoisted = join(repo, "node_modules", ".bin"); + const local = join(pkg, "node_modules", ".bin"); + const cwd = join(pkg, "scenes"); + for (const dir of [hoisted, local, cwd]) mkdirSync(dir, { recursive: true }); + mkdirSync(join(repo, ".git")); + writeFileSync(join(pkg, "package.json"), "{}"); + for (const dir of [hoisted, local, tools]) + writeFileSync(join(dir, "ffmpeg"), "", { mode: 0o755 }); + vi.spyOn(process, "cwd").mockReturnValue(cwd); + vi.stubEnv("PATH", [local, hoisted, tools].join(delimiter)); + + expect(findTrustedExecutable("ffmpeg")).toBe(join(tools, "ffmpeg")); + expect(trustedPathDirs()).toEqual([tools]); + }, + ); + it("on Windows, takes the PATH copy and skips one in the working directory", () => { const project = tempDir("hf-trusted-project-"); const tools = tempDir("hf-trusted-tools-"); diff --git a/packages/parsers/src/trustedExecutable.ts b/packages/parsers/src/trustedExecutable.ts index 0cc0bbe553..99a3fd72db 100644 --- a/packages/parsers/src/trustedExecutable.ts +++ b/packages/parsers/src/trustedExecutable.ts @@ -4,8 +4,8 @@ import { delimiter, dirname, isAbsolute, join, relative, resolve, sep } from "no /** * The one rule for finding a helper program by name. A project folder can come from anyone, so a - * program is taken only from an absolute PATH entry outside the working directory, or a fixed - * install dir. Only an override the person sets in their own environment may point into it. + * program comes only from an absolute PATH entry outside the working directory's project, or a + * fixed install dir. Only an override the person sets in their own environment may point into it. */ function comparable(path: string): string { @@ -23,14 +23,27 @@ function contains(parent: string, child: string): boolean { return rel === "" || (rel !== ".." && !rel.startsWith(`..${sep}`) && !isAbsolute(rel)); } +const PROJECT_MARKERS = [".git", "package.json", "node_modules", "hyperframes.json"]; + +function projectRoot(cwd: string, home: string): string { + let root = cwd; + for (let dir = cwd; dirname(dir) !== dir && !contains(dir, home); dir = dirname(dir)) { + if (PROJECT_MARKERS.some((marker) => existsSync(join(dir, marker)))) root = dir; + } + return root; +} + /** Whether a program in `dir` may be run without the person naming it. */ export function isTrustedDirectory(dir: string): boolean { if (!dir || !isAbsolute(dir)) return false; const target = comparable(dir); const cwd = comparable(process.cwd()); if (target === cwd) return false; + const home = comparable(homedir()); // From the home folder (or above it) every per-user install dir is inside; skip only the folder. - return contains(cwd, comparable(homedir())) || !contains(cwd, target); + if (contains(cwd, home)) return true; + // `npm run` puts every ancestor's node_modules/.bin on PATH, so from a subfolder the whole project is untrusted. + return !contains(projectRoot(cwd, home), target); } /** Whether `file` is an absolute path in a folder {@link isTrustedDirectory} accepts. */