UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

201 lines (200 loc) • 7.84 kB
import { L as normalizeLowercaseStringOrEmpty } from "./path-guards-quCEQx1i.mjs"; import { n as resolveDiagnosticProcessEnv } from "./process-env-Bx8i77R6.mjs"; import { S as canAccessSync, g as escapeRegExp } from "./redact-BRF0hr6G.mjs"; import { createRequire } from "node:module"; import { execFileSync } from "node:child_process"; import fsSync from "node:fs"; import path from "node:path"; //#region src/infra/windows-appcontainer-spawn.ts var AppContainerSpawnUnsupportedError = class extends Error { constructor(e) { super(`AppContainer: libuv ${e} cannot create child pipes; 1.53.0+ required. Run OpenClaw outside the AppContainer or use a Node build with libuv >= 1.53.0.`); this.code = "APPCONTAINER_SPAWN_UNSUPPORTED"; this.name = "AppContainerSpawnUnsupportedError"; } }; let checked = false; let unsupported; /** libuv before 1.53 loops forever creating child pipes in an AppContainer. */ function assertAppContainerSpawnSupported(e) { if (process.platform !== "win32") return; if (e === "ignore" || e === "inherit" || Array.isArray(e) && e.length >= 3 && e.every((e) => e !== void 0 && e !== null && e !== "pipe" && e !== "overlapped" && e !== "ipc" && !(typeof e === "number" && e < 0))) return; if (!checked) { checked = true; try { const { readCurrentAppContainerSid: e } = createRequire(import.meta.url)("@openclaw/proc-safe/windows-token"); const t = e(); const n = process.versions.uv; const r = Number(n.split(".")[0]); const i = Number(n.split(".")[1]); if (t !== null && (r < 1 || r === 1 && i < 53)) unsupported = new AppContainerSpawnUnsupportedError(n); } catch (e) { import("./subsystem-8jUQe5LA.mjs").then(({ createSubsystemLogger: t }) => { t("process").debug("AppContainer token probe failed; allowing spawn", { error: String(e) }); }).catch(() => {}); } } if (unsupported) throw unsupported; } //#endregion //#region src/infra/windows-install-roots.ts const DEFAULT_WINDOWS_SYSTEM_ROOT = "C:\\Windows"; const DEFAULT_PROGRAM_FILES = "C:\\Program Files"; const DEFAULT_PROGRAM_FILES_X86 = "C:\\Program Files (x86)"; const WINDOWS_NT_CURRENT_VERSION_KEY = "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"; const WINDOWS_CURRENT_VERSION_KEY = "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion"; const REG_QUERY_TIMEOUT_MS = 5e3; let cachedProcessRoots = null; function locateWindowsRegExe() { const e = path.win32.join(DEFAULT_WINDOWS_SYSTEM_ROOT, "System32", "reg.exe"); return canAccessSync(e, fsSync.constants.R_OK) ? e : null; } function trimTrailingSeparators(e) { const t = path.win32.parse(e); let n = e; while (n.length > t.root.length && /[\\/]/.test(n.at(-1) ?? "")) n = n.slice(0, -1); return n; } /** * Windows install roots should be local absolute directories, not drive-relative * paths, UNC shares, or PATH-like lists that could widen trust unexpectedly. */ function normalizeWindowsInstallRoot(e) { if (typeof e !== "string") return null; const t = e.trim(); if (!t || t.includes("\0") || t.includes("\r") || t.includes("\n") || t.includes(";")) return null; const n = trimTrailingSeparators(path.win32.normalize(t)); if (!path.win32.isAbsolute(n) || n.startsWith("\\\\")) return null; const r = path.win32.parse(n); if (!/^[A-Za-z]:\\$/.test(r.root)) return null; if (n.length <= r.root.length) return null; return n; } function getEnvValueCaseInsensitive(e, t) { const n = e[t]; if (n !== void 0) return n; const r = t.toUpperCase(); const i = Object.keys(e).find((e) => e.toUpperCase() === r); return i ? e[i] : void 0; } /** Queries one Windows registry string value via reg.exe; null when absent or unreadable. */ function queryWindowsRegistryValue(e, t, n) { const r = locateWindowsRegExe(); if (!r) return null; for (const i of [["/reg:64"], []]) { const a = n === void 0 ? REG_QUERY_TIMEOUT_MS : Math.min(REG_QUERY_TIMEOUT_MS, Math.ceil(n - performance.now())); if (a <= 0) return null; assertAppContainerSpawnSupported(); try { const n = execFileSync(r, [ "query", e, "/v", t, ...i ], { env: resolveDiagnosticProcessEnv(), encoding: "utf8", stdio: [ "ignore", "pipe", "ignore" ], timeout: a, windowsHide: true }); const o = new RegExp(`^\\s*${escapeRegExp(t)}\\s+REG_[A-Z0-9_]+\\s+(.+)$`, "im"); const s = n.match(o)?.[1]?.trim(); if (s) return s; } catch {} } return null; } function getRegistryProgramFilesRoots() { const readRoot = (e) => normalizeWindowsInstallRoot(queryWindowsRegistryValue(WINDOWS_CURRENT_VERSION_KEY, e) ?? void 0) ?? void 0; return { programFiles: readRoot("ProgramFilesDir"), programFilesX86: readRoot("ProgramFilesDir (x86)"), programW6432: readRoot("ProgramW6432Dir") }; } function buildWindowsInstallRoots(e, t = {}) { const n = normalizeWindowsInstallRoot(getEnvValueCaseInsensitive(e, "ProgramW6432")); const r = t.programW6432 ?? n ?? null; return { systemRoot: t.systemRoot ?? resolveSystemRootFromEnv(e), programFiles: t.programFiles ?? normalizeWindowsInstallRoot(getEnvValueCaseInsensitive(e, "ProgramFiles")) ?? r ?? DEFAULT_PROGRAM_FILES, programFilesX86: t.programFilesX86 ?? normalizeWindowsInstallRoot(getEnvValueCaseInsensitive(e, "ProgramFiles(x86)")) ?? DEFAULT_PROGRAM_FILES_X86, programW6432: r }; } function resolveSystemRootFromEnv(e) { return normalizeWindowsInstallRoot(getEnvValueCaseInsensitive(e, "SystemRoot")) ?? normalizeWindowsInstallRoot(getEnvValueCaseInsensitive(e, "WINDIR")) ?? DEFAULT_WINDOWS_SYSTEM_ROOT; } function getProcessRoots(e) { if (!cachedProcessRoots) { const t = {}; for (const e of [ "SystemRoot", "WINDIR", "ProgramFiles", "ProgramFiles(x86)", "ProgramW6432" ]) t[e] = getEnvValueCaseInsensitive(process.env, e); const n = { env: t, systemRoot: normalizeWindowsInstallRoot(queryWindowsRegistryValue(WINDOWS_NT_CURRENT_VERSION_KEY, "SystemRoot", e) ?? void 0) ?? resolveSystemRootFromEnv(t) }; if (e !== void 0 && performance.now() >= e) return n; cachedProcessRoots = n; } return cachedProcessRoots; } function getWindowsSystemRoot(e, t) { if (e !== process.env) return resolveSystemRootFromEnv(e); const n = getProcessRoots(t); return n.installRoots?.systemRoot ?? n.systemRoot; } function getWindowsInstallRoots(e = process.env) { if (e === process.env) { const e = getProcessRoots(); e.installRoots ??= buildWindowsInstallRoots(e.env, { ...getRegistryProgramFilesRoots(), systemRoot: e.systemRoot }); return e.installRoots; } return buildWindowsInstallRoots(e); } function getWindowsProgramFilesRoots(e = process.env) { const t = getWindowsInstallRoots(e); const n = /* @__PURE__ */ new Set(); const r = []; for (const e of [ t.programW6432, t.programFiles, t.programFilesX86 ]) { if (!e) continue; const t = normalizeLowercaseStringOrEmpty(e); if (n.has(t)) continue; n.add(t); r.push(e); } return r; } function getWindowsCmdExePath(e = process.env) { return getWindowsSystem32ExePath("cmd.exe", e); } function getWindowsSystem32ExePath(e, t = process.env) { if (path.win32.basename(e) !== e || !/^[A-Za-z0-9_.-]+\.exe$/u.test(e)) throw new Error(`Invalid Windows System32 executable name: ${e}`); return path.win32.join(getWindowsSystemRoot(t), "System32", e); } function getWindowsPowerShellExePath(e = process.env, t) { return path.win32.join(getWindowsSystemRoot(e, t), "System32", "WindowsPowerShell", "v1.0", "powershell.exe"); } function getWindowsWmicExePath(e = process.env, t) { return path.win32.join(getWindowsSystemRoot(e, t), "System32", "wbem", "wmic.exe"); } //#endregion export { getWindowsSystem32ExePath as a, getWindowsProgramFilesRoots as i, getWindowsInstallRoots as n, getWindowsWmicExePath as o, getWindowsPowerShellExePath as r, assertAppContainerSpawnSupported as s, getWindowsCmdExePath as t };