UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

315 lines (314 loc) • 12.6 kB
import { r as sha256HexPrefixCore } from "./node-crypto-B6yuTjmm.mjs"; import "./crypto-digest-B2m8wIBj.mjs"; import { n as POSIX_INLINE_COMMAND_FLAGS, o as hasPosixInteractiveStartupBeforeInlineCommand, s as hasPosixLoginStartupBeforeInlineCommand } from "./shell-inline-command-BOQk0l-l.mjs"; import { i as isGeneratedHashedArgPattern, r as isCwdBoundHashedArgPattern } from "./exec-command-resolution-DaBYjPqy.mjs"; import { a as extractBindableShellWrapperInlineCommand, p as isShellWrapperInvocation } from "./shell-wrapper-resolution-BEK558J4.mjs"; import { n as maxAsk, r as minSecurity } from "./exec-approvals-policy-BGzNHuNm.mjs"; import { t as resolveExecApprovalsFromFileInternal } from "./exec-approvals-resolver-C6IwmycS.mjs"; import { r as resolveAllowAlwaysPatternEntries } from "./exec-approvals-allowlist-f3-u9Y6p.mjs"; import "./exec-wrapper-resolution-BXAtcqj7.mjs"; import { n as canonicalizeExecApprovalPolicyRules, t as buildExecApprovalPolicyRuleKey } from "./exec-approval-policy-snapshot-V-E97G5Z.mjs"; //#region src/infra/exec-approvals-allow-always.ts function hasDurableExecApproval(e) { return hasExactCommandDurableExecApproval({ allowlist: e.allowlist, commandText: e.commandText }) || e.analysisOk && e.segmentAllowlistEntries.length > 0 && e.segmentAllowlistEntries.every((e) => e?.source === "allow-always"); } function buildDurableCommandApprovalPattern(e) { return `=command:${sha256HexPrefixCore(e, 16)}`; } function buildNodeCommandApprovalPattern(e) { return `=node-command:${sha256HexPrefixCore(e, 16)}`; } function hasNodeCommandAllowAlwaysMarker(e) { const t = e.commandText?.trim(); if (!t) return false; const n = buildNodeCommandApprovalPattern(t); return (e.allowlist ?? []).some((e) => e.source === "allow-always" && e.pattern === n); } function hasExactCommandDurableExecApproval(e) { const t = e.commandText?.trim(); if (!t) return false; const n = buildDurableCommandApprovalPattern(t); return (e.allowlist ?? []).some((e) => e.source === "allow-always" && (e.pattern === n || typeof e.commandText === "string" && e.commandText.trim() === t)); } /** Callers pass whether their final, post-gate authorization depends on a durable grant. */ function resolveDurableExecApprovalRequirement(e) { if (!e.durableApprovalRequired) return null; return hasExactCommandDurableExecApproval({ allowlist: e.allowlist, commandText: e.commandText }) ? "exact-command" : "segment-allowlist"; } function buildAllowlistEntryMatchKey(e) { return JSON.stringify([e.pattern, e.argPattern ?? null]); } /** Captures effective file policy while excluding ids and mutable usage metadata. */ function createExecApprovalPolicySnapshot(e) { const t = resolveExecApprovalsFromFileInternal({ file: e.file, agentId: e.agentId }); return { security: t.agent.security, ask: t.agent.ask, askFallback: t.agent.askFallback, autoAllowSkills: t.agent.autoAllowSkills, allowlistRules: canonicalizeExecApprovalPolicyRules(t.allowlist.map((e) => { const t = { pattern: e.pattern }; if (e.argPattern !== void 0) t.argPattern = e.argPattern; if (e.source === "allow-always") t.source = e.source; return t; })) }; } function isExecApprovalPolicySnapshotCurrent(e, t) { const n = new Set(t.allowlistRules.map(buildExecApprovalPolicyRuleKey)); return e.security === t.security && e.ask === t.ask && e.askFallback === t.askFallback && e.autoAllowSkills === t.autoAllowSkills && e.allowlistRules.every((e) => { const t = buildExecApprovalPolicyRuleKey(e); if (n.has(t)) return true; return e.source === void 0 && n.has(buildExecApprovalPolicyRuleKey({ ...e, source: "allow-always" })); }); } function resolveAllowAlwaysPatternCoverage(e) { const t = /* @__PURE__ */ new Map(); let n = 0; for (const r of e.segments) { const i = isShellWrapperInvocation(r.argv); const a = resolveAllowAlwaysPatternEntries({ segments: [r], cwd: e.cwd, env: e.env, platform: e.platform, strictInlineEval: e.strictInlineEval }); if (a.length === 0) continue; if (!i) n += 1; for (const e of a) t.set(`${e.pattern}\x00${e.argPattern ?? ""}`, e); } return { complete: e.segments.length > 0 && n === e.segments.length, patterns: [...t.values()] }; } function hasRuntimeShellPayload(e) { const t = extractBindableShellWrapperInlineCommand([...e]); return Boolean(t && (/(?:\$[A-Za-z0-9_@*?#$!-]|\$\{|`|\$\()/u.test(t) || hasPosixInteractiveStartupBeforeInlineCommand(e, POSIX_INLINE_COMMAND_FLAGS) || hasPosixLoginStartupBeforeInlineCommand(e, POSIX_INLINE_COMMAND_FLAGS))); } function resolvePlanPersistenceState(e) { if (!e) return { reusablePatternsAllowed: true, reasons: [] }; if (!e.ok) return { reusablePatternsAllowed: false, reasons: ["unplanned"] }; const t = /* @__PURE__ */ new Set(); let n = true; const r = e.groups.flatMap((e) => e.candidates); for (const e of r) { if (e.trustMode === "prompt-only") t.add("prompt-only"); if (e.trustMode === "exact-command") t.add("no-reusable-pattern"); if (e.trustMode === "executable" && !e.allowAlways) t.add("no-reusable-pattern"); n = n && e.allowAlways; if (hasRuntimeShellPayload(e.sourceSegment.argv)) t.add("runtime-payload"); if (e.transport.kind === "shell-wrapper" && hasRuntimeShellPayload(e.transport.wrapperArgv)) t.add("runtime-payload"); } return { reusablePatternsAllowed: n, reasons: [...t] }; } function resolveAllowAlwaysPersistenceDecision(e) { const t = resolvePlanPersistenceState(e.authorizationPlan); const n = new Set(t.reasons); if (e.runtimePayload === true) n.add("runtime-payload"); const r = e.commandText?.trim(); const i = [...n].filter((e) => e !== "no-reusable-pattern"); if (i.length > 0) return { kind: "one-shot", reasons: i }; if (e.preparedCoverage?.complete === true && e.preparedCoverage.patterns.length > 0) return { kind: "patterns", patterns: e.preparedCoverage.patterns, ...r ? { commandText: r } : {} }; if (t.reusablePatternsAllowed) { const t = resolveAllowAlwaysPatternCoverage({ segments: e.segments, cwd: e.cwd, env: e.env, platform: e.platform, strictInlineEval: e.strictInlineEval }); if (t.patterns.length > 0) return { kind: "patterns", patterns: t.patterns, ...r && t.complete ? { commandText: r } : {} }; } n.add("no-reusable-pattern"); return { kind: "one-shot", reasons: [...n] }; } function applyAllowAlwaysDecision(e) { const t = e.decision.kind === "patterns" ? [...e.decision.patterns] : []; const n = e.decision.commandText?.trim(); if (n) t.push({ pattern: e.decision.kind === "exact-command" ? buildDurableCommandApprovalPattern(n) : buildNodeCommandApprovalPattern(n) }); if (!e.agentId) throw new Error("Exec allowlist update requires an explicit agent id."); const r = new Set(t.filter((e) => isCwdBoundHashedArgPattern(e.argPattern)).map((e) => e.pattern)); const i = e.file.agents?.[e.agentId]; const a = i?.allowlist ?? []; let o = a.filter((e) => !(r.has(e.pattern) && e.source === "allow-always" && !isCwdBoundHashedArgPattern(e.argPattern))); let s = o.length !== a.length; for (const e of t) { const t = e.pattern.trim(); if (!t) continue; const n = e.argPattern === "" ? void 0 : e.argPattern; const matches = (e) => e.pattern === t && (e.argPattern ?? void 0) === n; const r = o.find(matches); if (r?.source === "allow-always") continue; const i = Date.now(); if (r) o = o.map((e) => matches(e) ? { ...e, argPattern: n, source: "allow-always", lastUsedAt: i } : e); else o.push({ id: crypto.randomUUID(), pattern: t, argPattern: n, source: "allow-always", lastUsedAt: i }); s = true; } return s ? { ...e.file, agents: { ...e.file.agents, [e.agentId]: { ...i, allowlist: o } } } : null; } //#endregion //#region src/infra/exec-approvals-authorization.kernel.ts function assertCurrentUsageAuthorization(e) { const t = resolveExecApprovalsFromFileInternal({ file: e.file, agentId: e.agentId, overrides: { security: e.authorization.security, ask: e.authorization.ask } }); const n = e.authorization.bypassHostApprovalFloors ? e.authorization.security : minSecurity(e.authorization.security, t.agent.security); const r = e.authorization.bypassHostApprovalFloors ? e.authorization.ask : maxAsk(e.authorization.ask, t.agent.ask); if (n === "deny") throw new Error("Exec approval changed before execution"); if (e.authorization.source === "explicit-approval" || e.authorization.source === "auto-review") { const t = e.authorization.policySnapshot; if (!t || !isExecApprovalPolicySnapshotCurrent(t, createExecApprovalPolicySnapshot({ file: e.file, agentId: e.agentId }))) throw new Error("Exec approval changed before execution"); } if (e.authorization.source === "explicit-approval") return; if (e.authorization.source === "auto-review") { if (r === "always") throw new Error("Exec approval changed before execution"); return; } let i = n; if (e.authorization.source === "ask-fallback") { const r = minSecurity(n, t.agent.askFallback); if (r === "deny" || r !== e.authorization.security) throw new Error("Exec approval changed before execution"); if (r === "full") return; i = r; } else if (n !== e.authorization.security || r !== e.authorization.ask) throw new Error("Exec approval changed before execution"); if (i !== "allowlist") return; if (e.authorization.requireExactCommandApproval) { if (!hasExactCommandDurableExecApproval({ allowlist: t.allowlist, commandText: e.command })) throw new Error("Exec approval changed before execution"); return; } if (e.authorization.requireDurableAllowlistApproval) { const n = new Set(t.allowlist.filter((e) => e.source === "allow-always").map(buildAllowlistEntryMatchKey)); if (e.matchKeys.size === 0 || [...e.matchKeys].some((e) => !n.has(e))) throw new Error("Exec approval changed before execution"); } if (!e.authorization.allowlistSatisfied) throw new Error("Exec approval changed before execution"); const a = new Set(t.allowlist.map(buildAllowlistEntryMatchKey)); if ([...e.matchKeys].some((e) => !a.has(e))) throw new Error("Exec approval changed before execution"); if (e.authorization.requireAutoAllowSkills && !t.agent.autoAllowSkills) throw new Error("Exec approval changed before execution"); } function applyRecordedAllowlistMetadata(e) { const { keys: t } = e; if (t.size === 0) return null; if (!e.agentId) throw new Error("Exec allowlist metadata update requires an explicit agent id."); const n = new Map(e.matches.filter((e) => e.resolvedPath !== void 0).map((e) => [buildAllowlistEntryMatchKey(e), e.resolvedPath])); const r = e.agentId; const i = e.file.agents ?? {}; let a = false; const o = { ...i }; for (const s of r === "*" ? [r] : ["*", r]) { const r = i[s]; if (!r?.allowlist) continue; let c = false; const l = r.allowlist.map((r) => { if (!t.has(buildAllowlistEntryMatchKey(r))) return r; a = true; c = true; return Object.assign({}, r, { id: r.id ?? crypto.randomUUID(), lastUsedAt: Date.now(), lastUsedCommand: isGeneratedHashedArgPattern(r.argPattern) ? void 0 : e.command, lastResolvedPath: n.get(buildAllowlistEntryMatchKey(r)) ?? e.resolvedPath }); }); if (c) o[s] = { ...r, allowlist: l }; } return a ? { ...e.file, agents: o } : null; } function applyExecAuthorizationCommit(e, t) { if (t.allowAlwaysDecision && t.allowAlwaysDecision.kind !== "one-shot") { if (t.authorization.source !== "explicit-approval") throw new Error("Allow-always persistence requires explicit approval"); } const n = new Set(t.matches.filter((e) => e.pattern).map(buildAllowlistEntryMatchKey)); assertCurrentUsageAuthorization({ file: e, agentId: t.agentId, command: t.command, matchKeys: n, authorization: t.authorization }); const r = t.allowAlwaysDecision && t.allowAlwaysDecision.kind !== "one-shot" ? applyAllowAlwaysDecision({ file: e, agentId: t.agentId, decision: t.allowAlwaysDecision }) : null; return applyRecordedAllowlistMetadata({ ...t, file: r ?? e, keys: n }) ?? r; } //#endregion export { hasDurableExecApproval as a, isExecApprovalPolicySnapshotCurrent as c, resolveDurableExecApprovalRequirement as d, createExecApprovalPolicySnapshot as i, resolveAllowAlwaysPatternCoverage as l, assertCurrentUsageAuthorization as n, hasExactCommandDurableExecApproval as o, buildAllowlistEntryMatchKey as r, hasNodeCommandAllowAlwaysMarker as s, applyExecAuthorizationCommit as t, resolveAllowAlwaysPersistenceDecision as u };