UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

241 lines (240 loc) • 7.32 kB
import { g as resolveExecWrapperTrustPlan } from "./exec-command-resolution-DaBYjPqy.mjs"; import { t as shellEscape } from "./shell-quote-DeRN90_2.mjs"; import { h as resolvePlannedSegmentArgv } from "./exec-approvals-allowlist-f3-u9Y6p.mjs"; //#region src/infra/exec-authorization-render.ts const SHELL_BARE_TOKEN_PATTERN = /^[A-Za-z0-9_@%+=:,./-]+$/u; function renderBareShellToken(e) { return e.length > 0 && SHELL_BARE_TOKEN_PATTERN.test(e) ? e : shellEscape(e); } function hasUnquotedShellExpansionSource(e) { let t = null; let n = false; let r = true; let i = false; for (const a of e) { if (n) { n = false; i = false; continue; } if (t !== "single" && a === "\\") { n = true; i = false; continue; } if (t === "single" || t === "ansi") { if (a === "'") t = null; continue; } if (t === "double") { if (a === "\"") t = null; continue; } if (a === "'") { t = i ? "ansi" : "single"; i = false; continue; } i = a === "$" && !i; if (a === "\"") { t = "double"; continue; } if (/\s/u.test(a)) { r = true; continue; } if (a === "~" && r) return true; if (a === "{" || a === "*" || a === "?" || a === "[") return true; r = false; } return false; } function hasArgumentShellExpansionSource(e) { const t = Math.max(0, e.sourceStep.executableSpan.endIndex - e.sourceStep.span.startIndex); return hasUnquotedShellExpansionSource(e.sourceStep.text.slice(t)); } function validateSpan(e) { const { span: t } = e; if (!Number.isInteger(t.startIndex) || !Number.isInteger(t.endIndex) || t.startIndex < 0 || t.endIndex > e.command.length || t.startIndex >= t.endIndex) return { ok: false, reason: "invalid source span" }; if (e.command.slice(t.startIndex, t.endIndex) !== e.expectedText) return { ok: false, reason: "source span mismatch" }; return { ok: true, command: "" }; } function validatedReplacement(e, t, n, r) { const i = validateSpan({ command: e, span: t, expectedText: n }); return i.ok ? { startIndex: t.startIndex, endIndex: t.endIndex, text: r } : i; } function sourceStepSlice(e) { const t = Math.max(0, e.span.startIndex - e.candidate.sourceStep.span.startIndex); const n = Math.max(t, e.span.endIndex - e.candidate.sourceStep.span.startIndex); return e.candidate.sourceStep.text.slice(t, n); } function replacementForCandidate(e) { if (e.mode === "enforced" && hasArgumentShellExpansionSource(e.candidate)) return { ok: false, reason: "shell expansion in enforced arguments" }; if (!(e.mode === "enforced" ? e.satisfiedBy !== "safeBuiltins" : e.satisfiedBy === "safeBins" || e.satisfiedBy === "inlineChain")) return null; const t = resolvePlannedSegmentArgv(e.candidate.sourceSegment); if (!t) return { ok: false, reason: "segment execution plan unavailable" }; if (e.satisfiedBy === "safeBins" && hasArgumentShellExpansionSource(e.candidate)) return { ok: false, reason: "shell expansion in safe-bin arguments" }; if (e.mode === "enforced" && e.candidate.transport.kind === "shell-wrapper") return { ok: false, reason: "shell quoting required in wrapper payload" }; if (e.candidate.sourceSegment.resolution?.wrapperChain?.length) return validatedReplacement(e.command, e.candidate.sourceStep.span, e.candidate.sourceStep.text, t.map(renderBareShellToken).join(" ")); const n = t[0]; if (!n) return { ok: false, reason: "segment execution plan unavailable" }; const r = renderBareShellToken(n); if (e.satisfiedBy === "safeBins" && e.candidate.transport.kind === "shell-wrapper" && r !== n) return { ok: false, reason: "shell quoting required in wrapper payload" }; const i = e.candidate.sourceStep.executableSpan; return validatedReplacement(e.command, i, sourceStepSlice({ candidate: e.candidate, span: i }), r); } function applyReplacements(e) { const t = e.replacements.toSorted((e, t) => e.startIndex - t.startIndex); let n = -1; for (const e of t) { if (e.startIndex < n) return { ok: false, reason: "overlapping replacement ranges" }; n = e.endIndex; } let r = e.command; for (const e of t.toReversed()) r = r.slice(0, e.startIndex) + e.text + r.slice(e.endIndex); return { ok: true, command: r }; } function buildAuthorizedShellCommandFromPlan(e) { if (!e.plan.ok) return { ok: false, reason: e.plan.reason }; if (e.plan.dialect !== "posix-shell") return { ok: false, reason: "unsupported command dialect" }; const t = e.plan.groups.flatMap((e) => e.candidates); const n = e.segmentSatisfiedBy ?? []; if (n.length !== t.length) return { ok: false, reason: "segment metadata mismatch" }; const r = []; for (const [i, a] of t.entries()) { const t = replacementForCandidate({ command: e.plan.originalCommand, candidate: a, mode: e.mode, satisfiedBy: n[i] }); if (!t) continue; if ("ok" in t) return t; r.push(t); } return applyReplacements({ command: e.plan.originalCommand, replacements: r }); } /** Pins reviewed dispatches while retaining argument expansion and wrapper semantics. */ function buildReviewedShellCommandFromPlan(e) { if (!e.plan.ok) return { ok: false, reason: e.plan.reason }; if (e.plan.dialect !== "posix-shell") return { ok: false, reason: "unsupported command dialect" }; const t = e.plan.groups.flatMap((e) => e.candidates); if (e.segmentSatisfiedBy && e.segmentSatisfiedBy.length !== t.length) return { ok: false, reason: "segment metadata mismatch" }; const n = []; for (const [r, i] of t.entries()) { if (e.segmentSatisfiedBy?.[r] === "safeBuiltins") continue; const { sourceSegment: t, sourceStep: a } = i; const o = t.sourceArgv ?? t.argv; const { dispatchChain: s } = resolveExecWrapperTrustPlan(o); if (i.transport.kind !== "direct" || t.resolution?.policyBlocked || !s) return { ok: false, reason: "dispatch chain cannot be rendered" }; if (a.argvSpans?.length !== o.length || a.argv.length !== o.length || !a.argv.every((e, t) => e === o[t])) return { ok: false, reason: "argument source spans unavailable" }; const c = validateSpan({ command: e.plan.originalCommand, span: a.span, expectedText: a.text }); if (!c.ok) return c; for (const [r, c] of s.entries()) { const l = o.length - c.length; const u = a.argvSpans[l]; if (!u || !c.every((e, t) => e === o[l + t]) || u.startIndex < a.span.startIndex || u.endIndex > a.span.endIndex) return { ok: false, reason: "dispatch source span mismatch" }; const d = r === s.length - 1 ? t.argv : c.slice(0, 1); const f = e.binding.operands.find((e) => e.executable === true && e.snapshot.argvIndex === 0 && e.argv.length === d.length && e.argv.every((e, t) => e === d[t])); if (!f?.executable || !f.invocationPath.startsWith("/")) return { ok: false, reason: "bound executable unavailable" }; const p = validatedReplacement(e.plan.originalCommand, u, sourceStepSlice({ candidate: i, span: u }), shellEscape(f.invocationPath)); if ("ok" in p) return p; n.push(p); } } if (n.length === 0) return { ok: false, reason: "no dispatches to render" }; return applyReplacements({ command: e.plan.originalCommand, replacements: n }); } //#endregion export { buildReviewedShellCommandFromPlan as n, buildAuthorizedShellCommandFromPlan as t };