UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

122 lines (121 loc) • 4.97 kB
import { r as normalizeExecAsk } from "./exec-approvals-core-GflUelcW.mjs"; //#region src/infra/exec-approvals-policy.ts function requiresExecApproval(params) { if (params.ask === "always") return true; if (params.durableApprovalSatisfied === true) return false; return params.ask === "on-miss" && params.security === "allowlist" && (!params.analysisOk || !params.allowlistSatisfied); } function normalizeCommandName(value) { return (value ?? "").split(/[\\/]/).pop()?.toLowerCase() ?? ""; } function textMentionsSecurityAuditSuppressions(value) { const normalized = value.toLowerCase(); return normalized.includes("security.audit.suppressions") || /["']?security["']?[\s\S]{0,200}["']?audit["']?[\s\S]{0,200}["']?suppressions["']?/.test(normalized); } function isReadOnlySecurityAuditSuppressionInspection(argv) { let offset = normalizeCommandName(argv[0]) === "pnpm" && argv[1] === "openclaw" ? 1 : 0; if (normalizeCommandName(argv[offset]) !== "openclaw") return false; offset += 1; while (offset < argv.length) { const arg = argv[offset]; if (["--dev", "--no-color"].includes(arg ?? "")) { offset += 1; continue; } if ([ "--profile", "--container", "--log-level" ].includes(arg ?? "")) { offset += 2; continue; } if (arg?.startsWith("--profile=") || arg?.startsWith("--container=") || arg?.startsWith("--log-level=")) { offset += 1; continue; } break; } return argv[offset] === "config" && [ "get", "schema", "validate" ].includes(argv[offset + 1] ?? ""); } function removeParsedSegmentText(command, segments) { let remaining = command; for (const segment of segments) { const raw = (segment.raw ?? segment.argv?.join(" "))?.trim(); if (!raw) continue; remaining = remaining.replace(raw, " "); } return remaining; } function commandRequiresSecurityAuditSuppressionApproval(params) { let sawSegmentMention = false; for (const segment of params.segments) { if (!textMentionsSecurityAuditSuppressions(`${segment.raw ?? ""} ${segment.argv.join(" ")}`)) continue; sawSegmentMention = true; if (!isReadOnlySecurityAuditSuppressionInspection(segment.argv)) return true; } if (sawSegmentMention) { if (textMentionsSecurityAuditSuppressions(removeParsedSegmentText(params.command, params.segments))) return true; return false; } return textMentionsSecurityAuditSuppressions(params.command); } function minSecurity(a, b) { const order = { deny: 0, allowlist: 1, full: 2 }; return order[a] <= order[b] ? a : b; } function maxAsk(a, b) { const order = { off: 0, "on-miss": 1, always: 2 }; return order[a] >= order[b] ? a : b; } const DEFAULT_EXEC_APPROVAL_DECISIONS = [ "allow-once", "allow-always", "deny" ]; const OPTIONAL_EXEC_APPROVAL_DECISIONS = ["allow-always"]; const OPTIONAL_EXEC_APPROVAL_DECISION_SET = new Set(OPTIONAL_EXEC_APPROVAL_DECISIONS); function isOptionalExecApprovalDecision(decision) { return OPTIONAL_EXEC_APPROVAL_DECISION_SET.has(decision); } function collectExecApprovalUnavailableDecisionSet(decisions) { const unavailable = /* @__PURE__ */ new Set(); if (!Array.isArray(decisions)) return unavailable; for (const decision of decisions) if (isOptionalExecApprovalDecision(decision)) unavailable.add(decision); return unavailable; } function normalizeExecApprovalUnavailableDecisions(decisions) { const unavailable = collectExecApprovalUnavailableDecisionSet(decisions); return OPTIONAL_EXEC_APPROVAL_DECISIONS.filter((decision) => unavailable.has(decision)); } function resolveExecApprovalAllowedDecisions(params) { if (normalizeExecAsk(params?.ask) === "always" || params?.allowAlwaysPersistence?.kind === "one-shot") return ["allow-once", "deny"]; return DEFAULT_EXEC_APPROVAL_DECISIONS; } function resolveExecApprovalUnavailableDecisions(params) { const allowed = new Set(resolveExecApprovalAllowedDecisions(params)); return OPTIONAL_EXEC_APPROVAL_DECISIONS.filter((decision) => !allowed.has(decision)); } function resolveExecApprovalRequestAllowedDecisions(params) { const policyDecisions = resolveExecApprovalAllowedDecisions({ ask: params?.ask }); const unavailableDecisions = collectExecApprovalUnavailableDecisionSet(params?.unavailableDecisions); if (unavailableDecisions.size === 0) return policyDecisions; return policyDecisions.filter((decision) => !isOptionalExecApprovalDecision(decision) || !unavailableDecisions.has(decision)); } function isExecApprovalDecisionAllowed(params) { return resolveExecApprovalAllowedDecisions({ ask: params.ask }).includes(params.decision); } //#endregion export { maxAsk as a, requiresExecApproval as c, resolveExecApprovalUnavailableDecisions as d, isExecApprovalDecisionAllowed as i, resolveExecApprovalAllowedDecisions as l, OPTIONAL_EXEC_APPROVAL_DECISIONS as n, minSecurity as o, commandRequiresSecurityAuditSuppressionApproval as r, normalizeExecApprovalUnavailableDecisions as s, DEFAULT_EXEC_APPROVAL_DECISIONS as t, resolveExecApprovalRequestAllowedDecisions as u };