UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

168 lines (167 loc) • 3.84 kB
import path from "node:path"; //#region src/agents/mcp-docker-command.ts const dockerName = (e) => /^docker(?:\.exe)?$/iu.test(path.basename(e)); function literalShellWords(e) { const t = []; let n = ""; let r = false; let i; for (let a = 0; a < e.length; a++) { const o = e[a]; if (i === "'") { if (o === i) i = void 0; else n += o; } else if (o === "\\") { const t = e[++a]; if (t === void 0 || t === "\n" || t === "\r") return; if (i === "\"" && !/["\\$`]/u.test(t)) n += "\\"; n += t; r = true; } else if (i === "\"") { if (o === i) i = void 0; else if (o === "$" || o === "`") return; else n += o; } else if (o === "'" || o === "\"") { i = o; r = true; } else if (o === " " || o === " ") { if (r) t.push(n); n = ""; r = false; } else { if (/[\n\r$`;&|<>(){}*?[\]~#]/u.test(o)) return; n += o; r = true; } } if (i) return; if (r) t.push(n); return t; } const booleanOptions = /* @__PURE__ */ new Set([ "--rm", "--init", "--interactive", "--read-only", "--privileged", "--oom-kill-disable", "--sig-proxy", "--publish-all" ]); const valueOptions = /* @__PURE__ */ new Set([ "--name", "--label", "--env", "--env-file", "--volume", "--volumes-from", "--mount", "--workdir", "--user", "--group-add", "--network", "--network-alias", "--hostname", "--entrypoint", "--platform", "--memory", "--memory-swap", "--cpus", "--cpu-shares", "--cpuset-cpus", "--shm-size", "--ulimit", "--pids-limit", "--security-opt", "--cap-add", "--cap-drop", "--device", "--add-host", "--dns", "--dns-search", "--publish", "--expose", "--restart", "--pull", "--stop-signal", "--stop-timeout", "--runtime", "--tmpfs" ]); const shortValues = { "-e": "--env", "-v": "--volume", "-w": "--workdir", "-u": "--user", "-l": "--label", "-p": "--publish", "-h": "--hostname", "-m": "--memory", "-c": "--cpu-shares" }; /** Only known Docker run option boundaries may receive launch-owned flags. */ function parseMcpDockerCommand(e, t = []) { let n = e; let r = [...t]; if (!dockerName(e)) { if (!/^(?:sh|bash|dash|zsh)$/u.test(path.basename(e))) return { kind: "other" }; if (!t.some((e) => /\bdocker\b/u.test(e))) return { kind: "other" }; const i = path.basename(e) === "sh" && t.length === 2 && t[0] === "-c" ? literalShellWords(t[1]) : void 0; if (!i || i[0] !== "exec" || !dockerName(i[1] ?? "")) return { kind: "unsafe", reason: "Docker shell wrapper is not a single literal exec command" }; n = i[1]; r = i.slice(2); } const i = r[0] === "run" ? 0 : r[0] === "container" && r[1] === "run" ? 1 : -1; if (i < 0) return { kind: "unsafe", reason: "expected docker run without global options" }; r = ["run", ...r.slice(i + 1)]; let a = false; for (let e = 1; e < r.length; e++) { const t = r[e]; if (t === "--") { if (r[e + 1]) return { kind: "docker", command: n, args: r, hasName: a }; break; } if (!t.startsWith("-")) return { kind: "docker", command: n, args: r, hasName: a }; if (t === "-i" || t === "-P") continue; const i = t.indexOf("="); const o = i < 0 ? t : t.slice(0, i); if (booleanOptions.has(o)) { if (i >= 0 && !/^(?:true|false)$/u.test(t.slice(i + 1))) break; continue; } const s = shortValues[o] ?? o; if (!valueOptions.has(s)) return { kind: "unsafe", reason: "unsupported or conflicting Docker run option" }; const c = i < 0 ? r[++e] : t.slice(i + 1); if (!c) break; if (s === "--label" && c.split("=", 1)[0].startsWith("openclaw.mcp.")) return { kind: "unsafe", reason: "Docker run uses reserved openclaw.mcp labels" }; if (s === "--name") a = true; } return { kind: "unsafe", reason: "Docker run is missing an image or option value" }; } //#endregion export { parseMcpDockerCommand as t };