UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

182 lines (181 loc) • 5.05 kB
import { F as resolveTimerTimeoutMs, k as parseStrictNonNegativeInteger, o as LruCache } from "./path-guards-quCEQx1i.mjs"; import { i as sanitizeHostExecEnv } from "./host-env-security-Cdlya5OY.mjs"; import { t as formatErrorMessage } from "./errors-hqLS1E8Y.mjs"; import { n as isTruthyEnvValue } from "./env-C62NiKTo.mjs"; import "./executable-path-BtaxFk1u.mjs"; import { execFileSync, spawn } from "node:child_process"; import fsSync from "node:fs"; import path from "node:path"; import os from "node:os"; //#region src/infra/shell-env.ts const DEFAULT_TIMEOUT_MS = 15e3; const DEFAULT_MAX_BUFFER_BYTES = 2097152; const DEFAULT_SHELL = "/bin/sh"; const LOGIN_SHELL_ENV_COMMAND = "printf '\\0'; env -0"; let lastAppliedKeys = []; let cachedEtcShells; const loginShellEnvProbeCache = new LruCache(64); function resolveShellExecEnv(e) { const t = sanitizeHostExecEnv({ baseEnv: e }); const n = os.homedir().trim(); if (n) t.HOME = n; else delete t.HOME; delete t.ZDOTDIR; return t; } function resolveTimeoutMs(e) { return resolveTimerTimeoutMs(e, DEFAULT_TIMEOUT_MS, 0); } function readEtcShells() { if (cachedEtcShells !== void 0) return cachedEtcShells; try { const e = fsSync.readFileSync("/etc/shells", "utf8").split(/\r?\n/).map((e) => e.trim()).filter((e) => e.length > 0 && !e.startsWith("#") && path.isAbsolute(e)); cachedEtcShells = new Set(e); } catch { cachedEtcShells = null; } return cachedEtcShells; } function isTrustedShellPath(e) { if (!path.isAbsolute(e)) return false; if (path.normalize(e) !== e) return false; return readEtcShells()?.has(e) === true; } function resolveShell(e) { const t = e.SHELL?.trim(); if (t && isTrustedShellPath(t)) return t; return DEFAULT_SHELL; } function createLoginShellExecSpec(e) { const t = e.purpose === "environment-import" && path.basename(e.shell) === "bash" ? ["-lic", LOGIN_SHELL_ENV_COMMAND] : [ "-l", "-c", LOGIN_SHELL_ENV_COMMAND ]; const n = { encoding: "buffer", timeout: e.timeoutMs, maxBuffer: DEFAULT_MAX_BUFFER_BYTES, env: e.env, windowsHide: true, stdio: [ "ignore", "pipe", "pipe" ], detached: true }; return { shell: e.shell, args: t, options: n }; } function parseShellEnv(e) { const t = /* @__PURE__ */ new Map(); const n = e.indexOf(0); if (n < 0) return t; const r = e.subarray(n + 1).toString("utf8").split("\0"); for (const e of r) { const n = e.indexOf("="); if (n <= 0) continue; t.set(e.slice(0, n), e.slice(n + 1)); } return t; } function createLoginShellEnvCacheKey(e) { const t = Object.entries(e.execEnv).filter(([e]) => { if (e === "HOME" || e === "PATH" || e === "TERM" || e === "LANG" || e === "LC_ALL" || e === "LC_CTYPE" || e === "USER" || e === "LOGNAME" || e === "TMPDIR") return true; return e.startsWith("XDG_") || e.startsWith("OPENCLAW_"); }).toSorted(([e], [t]) => e.localeCompare(t)); return JSON.stringify([ e.shell, e.timeoutMs, e.purpose, t ]); } function prepareLoginShellEnvProbe(e) { const t = { timeoutMs: resolveTimeoutMs(e.timeoutMs), shell: resolveShell(e.env), env: resolveShellExecEnv(e.env), purpose: e.purpose }; return { request: t, cacheKey: createLoginShellEnvCacheKey({ ...t, execEnv: t.env }) }; } function probeLoginShellEnv(e) { if (process.platform === "win32") return { ok: true, shellEnv: /* @__PURE__ */ new Map() }; const { request: t, cacheKey: n } = prepareLoginShellEnvProbe(e); const r = e.purpose === "environment-import" ? loginShellEnvProbeCache : void 0; const i = r?.get(n); if (i) return { ok: true, shellEnv: new Map(i) }; try { const e = createLoginShellExecSpec(t); const i = parseShellEnv(execFileSync(e.shell, e.args, e.options)); r?.set(n, [...i.entries()]); return { ok: true, shellEnv: i }; } catch (e) { return { ok: false, error: formatErrorMessage(e) }; } } function loadShellEnvFallback(e) { const t = e.logger ?? console; const n = e.expectedKeys.filter((t) => !Object.hasOwn(e.env, t)); if (n.length === 0) { lastAppliedKeys = []; return []; } const r = probeLoginShellEnv({ env: e.env, timeoutMs: e.timeoutMs, purpose: "environment-import" }); if (!r.ok) { t.warn(`[openclaw] shell env fallback failed: ${r.error}`); lastAppliedKeys = []; return []; } const i = []; for (const t of n) { const n = r.shellEnv.get(t); if (!n?.trim()) continue; e.env[t] = n; i.push(t); } lastAppliedKeys = i; return i; } function shouldEnableShellEnvFallback(e) { return isTruthyEnvValue(e.OPENCLAW_LOAD_SHELL_ENV); } function shouldDeferShellEnvFallback(e) { return isTruthyEnvValue(e.OPENCLAW_DEFER_SHELL_ENV_FALLBACK); } function resolveShellEnvFallbackTimeoutMs(e) { const t = e.OPENCLAW_SHELL_ENV_TIMEOUT_MS?.trim(); if (!t) return DEFAULT_TIMEOUT_MS; const n = parseStrictNonNegativeInteger(t); if (n === void 0) return DEFAULT_TIMEOUT_MS; return resolveTimeoutMs(n); } //#endregion export { loadShellEnvFallback, resolveShellEnvFallbackTimeoutMs, shouldDeferShellEnvFallback, shouldEnableShellEnvFallback };