UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

174 lines (173 loc) • 6.51 kB
import { r as asNullableRecord } from "./record-coerce-CVPuDqcc.mjs"; import { l as readConfigFileSnapshotForWrite } from "./io.runtime-DsJ2dLCQ.mjs"; import { t as mutateConfigFile } from "./mutate-DhAOi0-2.mjs"; import "./string-coerce-runtime-BwAZZDf5.mjs"; import { t as createClackPrompter } from "./clack-prompter-oqVcatgQ.mjs"; import "./setup-runtime-DiZlyPkf.mjs"; import "./config-mutation-CGDddIEg.mjs"; import "./policy-CaGTXNYW.mjs"; import path from "node:path"; import fs from "node:fs/promises"; //#region extensions/file-transfer/src/approvals-migration.ts function listLegacyApprovalItems(e) { const t = asNullableRecord(e); if (!t || t.policyVersion === 2) return []; const n = asNullableRecord(t.nodes); if (!n) return []; const r = []; const i = /* @__PURE__ */ new Set(); for (const [e, t] of Object.entries(n)) { const n = asNullableRecord(t); if (!n) continue; for (const [t, a] of [["read", "allowReadPaths"], ["write", "allowWritePaths"]]) { const o = Array.isArray(n[a]) ? n[a] : []; for (const n of o) if (typeof n === "string" && n.length > 0) { const a = `${e}\0${t}\0${n}`; if (i.has(a)) continue; i.add(a); r.push({ selector: e, kind: t, path: n }); } } } return r; } function applyApprovalMigration(e, t) { const n = asNullableRecord(e) ?? {}; const r = structuredClone(n); const i = asNullableRecord(r.nodes) ?? {}; r.nodes = i; const a = []; for (const e of t) { if (e.action === "keep-glob") continue; const t = asNullableRecord(i[e.item.selector]); if (!t) continue; const n = e.item.kind === "read" ? "allowReadPaths" : "allowWritePaths"; t[n] = (Array.isArray(t[n]) ? t[n] : []).filter((t) => t !== e.item.path); if (e.action === "exact") a.push(e.item); } r.pendingReapprovals = a; r.policyVersion = 2; return r; } //#endregion //#region extensions/file-transfer/src/cli.ts function readPluginConfig(e) { const t = asNullableRecord(e); const n = asNullableRecord(t?.plugins); const r = asNullableRecord(n?.entries); const i = asNullableRecord(r?.["file-transfer"]); return asNullableRecord(i?.config); } function resolveMigrationBackupPath(e) { const t = e.snapshot.includeProvenance?.findLast((e) => e.path.length <= 1 && e.path[0] === "plugins"); const n = t?.path.length === 1 && t.kind === "single" && !t.hasSiblingOverrides && t.targetPath ? t.targetPath : e.snapshot.path; return `${path.normalize(n)}.bak`; } async function runApprovalMigration(e) { const t = await readConfigFileSnapshotForWrite(); if (!t.snapshot.valid) throw new Error("OpenClaw config is invalid; fix it before migrating file-transfer approvals"); const n = asNullableRecord(t.snapshot.sourceConfig); if (asNullableRecord(n?.gateway)?.mode === "remote") throw new Error("This migration must run on the Gateway host because it updates that host's file-transfer policy."); const r = readPluginConfig(t.snapshot.sourceConfig); const i = listLegacyApprovalItems(r); if (i.length === 0) { const t = { status: "ok", changed: false, message: "No legacy permissions need review." }; process.stdout.write(e.json ? `${JSON.stringify(t)}\n` : `${t.message}\n`); return; } if (e.json || !process.stdin.isTTY) { const t = { status: "needs-input", changed: false, items: i, command: "openclaw file-transfer approvals migrate" }; if (e.json) { process.stdout.write(`${JSON.stringify(t, null, 2)}\n`); process.exitCode = 2; return; } throw new Error("File-transfer permissions need interactive review. Run `openclaw file-transfer approvals migrate` in a terminal."); } const a = createClackPrompter(); await a.intro("Review file-transfer permissions"); await a.note("Older positive permissions remain inactive until this review finishes. Deny rules and transfer limits remain active."); const o = []; for (const e of i) { const t = await a.select({ message: `${e.selector} · ${e.kind} · ${e.path}`, options: [ { value: "exact", label: "Require exact reapproval", hint: "Next use prompts once, then binds the actual node and command" }, { value: "keep-glob", label: "Keep as an intentional wildcard", hint: "Retains the current glob behavior" }, { value: "remove", label: "Remove this permission" } ] }); o.push({ item: e, action: t }); } const s = o.filter((e) => e.action === "keep-glob").length; const c = o.filter((e) => e.action === "exact").length; const l = o.filter((e) => e.action === "remove").length; await a.note(`Exact paths requiring one reapproval: ${c}\nIntentional wildcards: ${s}\nRemoved: ${l}`, "Migration plan"); await a.note("Older OpenClaw versions cannot read the migrated format. To downgrade, restore the adjacent config backup shown after migration before starting the older version.", "Downgrade"); if (e.dryRun) { await a.outro("Dry run complete. No config was changed."); return; } if (!await a.confirm({ message: "Apply this migration?", initialValue: true })) { await a.outro("Cancelled. No config was changed."); return; } const u = applyApprovalMigration(r, o); const d = resolveMigrationBackupPath(t); const f = await fs.stat(d).catch(() => null); await mutateConfigFile({ base: "source", baseHash: t.snapshot.hash, writeOptions: t.writeOptions, afterWrite: { mode: "none", reason: "file-transfer approval policy migration" }, mutate: (e) => { const t = e.plugins ??= {}; const n = t.entries ??= {}; const r = n["file-transfer"] ??= {}; r.config = u; } }); const p = await fs.stat(d).catch(() => null); const m = Boolean(p && (!f || p.ino !== f.ino || p.mtimeMs !== f.mtimeMs || p.size !== f.size)); await a.outro(m ? `File-transfer permissions updated. Exact paths will prompt once on next use. Config backup: ${d}` : "File-transfer permissions updated. Exact paths will prompt once on next use. The standard config backup could not be verified."); } function registerFileTransferCli(e) { e.command("file-transfer").description("Review file-transfer standing approvals").command("approvals").description("Manage standing approvals").command("migrate").description("Review and migrate older file-transfer permissions").option("--dry-run", "Review choices without changing config", false).option("--json", "Report unresolved legacy permissions as JSON", false).action(async (e) => { await runApprovalMigration(e); }); } //#endregion export { registerFileTransferCli };