Files
hyperframes/packages/cli/src/utils/windowsExeSearch.ts
T
James RussoandClaude Opus 5.5 83eae240fb fix(cli): start helper programs only from trusted PATH folders on every platform (#7787)
* fix(cli): start helper programs only from trusted PATH folders on every platform

At startup the CLI and its worker entry points rewrite their own PATH to
absolute folders outside the working directory, on every platform, so a
program planted in the project (a relative or empty PATH entry,
node_modules/.bin, or any folder inside the cwd) is never run by name.
Child processes inherit the trimmed PATH. media-use reuses the same helper
instead of its private copy.

doctor resolves docker with findTrustedExecutable and runs it by absolute
path with execFileSync, no shell. A docker found only in the project's
node_modules/.bin is reported as not found, with a hint that it was skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cli): keep stale-install detection and Windows system folders after PATH trim

The stale hyperframes check for reverse start reads the PATH the process
started with, so an older copy in the project's node_modules/.bin is still
reported. It only reads the filesystem and never runs that copy.

On Windows the trim now keeps %SystemRoot%, System32, System32\Wbem and
the WindowsPowerShell folder even when the CLI starts inside them, so an
elevated prompt in C:\Windows\System32 can still run cmd, taskkill,
tasklist, powershell and tar. PATH entries wrapped in double quotes are
unquoted before the check instead of being dropped. PATH order is kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cli): resolve the empty-PATH placeholder only when it is needed

Computing it at import threw in test environments where import.meta.url is not a file URL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 01:24:20 +00:00

79 lines
2.7 KiB
TypeScript

import { delimiter, win32 } from "node:path";
import { fileURLToPath } from "node:url";
import { trustedPathDirs } from "@hyperframes/parsers/ff-binaries";
const neverCreatedPathFolder = () =>
fileURLToPath(new URL("no-trusted-path-folder", import.meta.url));
function keyOf(env: NodeJS.ProcessEnv, name: string): string | undefined {
return Object.keys(env).find((key) => key.toUpperCase() === name);
}
function windowsFolderKey(dir: string): string {
return dir.replaceAll("/", "\\").replace(/\\+$/, "").toLowerCase();
}
function windowsSystemFolders(env: NodeJS.ProcessEnv): Set<string> {
const rootKey = keyOf(env, "SYSTEMROOT");
const root = (rootKey && env[rootKey]) || "C:\\Windows";
const system32 = win32.join(root, "System32");
return new Set(
[
root,
system32,
win32.join(system32, "Wbem"),
win32.join(system32, "WindowsPowerShell", "v1.0"),
].map(windowsFolderKey),
);
}
function unquoted(dir: string): string {
return dir.length > 1 && dir.startsWith('"') && dir.endsWith('"') ? dir.slice(1, -1) : dir;
}
function windowsTrustedDirs(pathValue: string, env: NodeJS.ProcessEnv): string[] {
const entries = pathValue.split(win32.delimiter).map(unquoted);
const trusted = new Set(trustedPathDirs(entries.join(delimiter)));
const system = windowsSystemFolders(env);
return [
...new Set(entries.filter((dir) => trusted.has(dir) || system.has(windowsFolderKey(dir)))),
];
}
export function withOnlyTrustedPathFolders(
env: NodeJS.ProcessEnv,
platform: NodeJS.Platform = process.platform,
): NodeJS.ProcessEnv {
const pathKey = keyOf(env, "PATH");
if (!pathKey) return { ...env };
const value = env[pathKey] ?? "";
const dirs = platform === "win32" ? windowsTrustedDirs(value, env) : trustedPathDirs(value);
const separator = platform === "win32" ? win32.delimiter : delimiter;
return { ...env, [pathKey]: dirs.length > 0 ? dirs.join(separator) : neverCreatedPathFolder() };
}
export function skipWorkingDirectoryInExeSearch(
env: NodeJS.ProcessEnv = process.env,
platform: NodeJS.Platform = process.platform,
): void {
if (platform === "win32") env.NoDefaultCurrentDirectoryInExePath ??= "1";
}
export function searchOnlyTrustedFolders(
env: NodeJS.ProcessEnv = process.env,
platform: NodeJS.Platform = process.platform,
): void {
const pathKey = keyOf(env, "PATH");
if (pathKey) env[pathKey] = withOnlyTrustedPathFolders(env, platform)[pathKey];
skipWorkingDirectoryInExeSearch(env, platform);
}
const pathKeyAtStart = keyOf(process.env, "PATH");
const PATH_AT_START = (pathKeyAtStart && process.env[pathKeyAtStart]) || "";
export function originalPath(): string {
return PATH_AT_START;
}
searchOnlyTrustedFolders();