UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

112 lines (111 loc) • 4.05 kB
import { J as pruneMapToMaxSize, S as canAccessSync } from "./redact-BRF0hr6G.mjs"; import { i as getWindowsProgramFilesRoots, n as getWindowsInstallRoots } from "./windows-install-roots-CNvr-0dO.mjs"; import fsSync from "node:fs"; import path from "node:path"; //#region src/infra/resolve-system-bin.ts const UNIX_BASE_TRUSTED_DIRS = [ "/usr/bin", "/bin", "/usr/sbin", "/sbin" ]; const DARWIN_STANDARD_DIRS = ["/opt/homebrew/bin", "/usr/local/bin"]; const LINUX_STANDARD_DIRS = ["/usr/local/bin"]; const WIN_PATHEXT = [ ".exe", ".cmd", ".bat", ".com" ]; const WINDOWS_PROGRAM_FILES_TOOL_DIR_PREFIXES = ["ImageMagick-", "GraphicsMagick-"]; const WINDOWS_PROGRAM_FILES_TOOL_DIRS = ["ImageMagick", "GraphicsMagick"]; const RESOLVED_BIN_CACHE_LIMIT = 512; const resolvedCacheStrict = /* @__PURE__ */ new Map(); const resolvedCacheStandard = /* @__PURE__ */ new Map(); function cacheResolvedSystemBin(e, t, n) { e.set(t, n); pruneMapToMaxSize(e, RESOLVED_BIN_CACHE_LIMIT); } function collectWindowsProgramFilesToolDirs(e) { const t = WINDOWS_PROGRAM_FILES_TOOL_DIRS.map((t) => path.win32.join(e, t)); try { for (const n of fsSync.readdirSync(e, { withFileTypes: true })) if (n.isDirectory() && WINDOWS_PROGRAM_FILES_TOOL_DIR_PREFIXES.some((e) => n.name.startsWith(e))) t.push(path.win32.join(e, n.name)); } catch {} return t; } /** * Build the trusted-dir list for Windows. Only system-managed directories * are included; user-profile paths like %LOCALAPPDATA% are excluded. */ function buildWindowsTrustedDirs() { const e = []; const { systemRoot: t } = getWindowsInstallRoots(); e.push(path.win32.join(t, "System32")); e.push(path.win32.join(t, "SysWOW64")); e.push(path.win32.join(t, "System32", "WindowsPowerShell", "v1.0")); for (const t of getWindowsProgramFilesRoots()) { e.push(path.win32.join(t, "OpenSSL-Win64", "bin")); e.push(path.win32.join(t, "OpenSSL", "bin")); e.push(path.win32.join(t, "ffmpeg", "bin")); } return e; } function buildWindowsStandardDirs() { const { systemRoot: e } = getWindowsInstallRoots(); const t = path.win32.parse(e).root; const n = [path.win32.join(t, "ProgramData", "chocolatey", "bin")]; for (const e of getWindowsProgramFilesRoots()) n.push(...collectWindowsProgramFilesToolDirs(e)); return n; } function buildUnixTrustedDirs(e) { const t = [...UNIX_BASE_TRUSTED_DIRS]; const n = process.platform; if (n === "linux") { t.push("/run/current-system/sw/bin"); t.push("/snap/bin"); } if (e === "standard") { if (n === "darwin") t.push(...DARWIN_STANDARD_DIRS); else if (n === "linux") t.push(...LINUX_STANDARD_DIRS); } return t; } const trustedDirs = {}; function getTrustedDirs(e) { return trustedDirs[e] ??= process.platform !== "win32" ? buildUnixTrustedDirs(e) : e === "strict" ? buildWindowsTrustedDirs() : [...getTrustedDirs("strict"), ...buildWindowsStandardDirs()]; } /** * Resolve a binary name to an absolute path by searching only trusted system * directories. Returns `null` when the binary is not found. Results are cached * for the lifetime of the process. * * This MUST be used instead of bare binary names in `execFile`/`spawn` calls * for internal infrastructure binaries (ffmpeg, ffprobe, openssl, etc.) to * prevent PATH-hijack attacks via user-writable directories. */ function resolveSystemBin(e, t) { const n = t?.trust ?? "strict"; const r = (t?.extraDirs?.length ?? 0) > 0; const i = n === "standard" ? resolvedCacheStandard : resolvedCacheStrict; if (!r) { const t = i.get(e); if (t !== void 0) { i.delete(e); i.set(e, t); return t; } } const a = [...getTrustedDirs(n), ...t?.extraDirs ?? []]; const o = process.platform === "win32"; const s = o && path.win32.extname(e).length > 0; for (const t of a) { const n = o && !s ? WIN_PATHEXT.map((n) => path.win32.join(t, e + n)) : [path.join(t, e)]; for (const t of n) if (canAccessSync(t, o ? fsSync.constants.R_OK : fsSync.constants.X_OK)) { if (!r) cacheResolvedSystemBin(i, e, t); return t; } } return null; } //#endregion export { resolveSystemBin as t };