UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

40 lines (39 loc) 1.44 kB
import { s as findExistingAncestor } from "./fs-safe-B6pvPGnf.js"; import { t as pathScope } from "./security-runtime-Ckf0kc0h.js"; import path from "node:path"; import fs from "node:fs/promises"; //#region extensions/browser/src/sdk-security-runtime.ts /** * Browser-local SDK security bridge plus directory creation helper. */ /** Ensures an absolute directory exists without escaping its nearest existing ancestor. */ async function ensureAbsoluteDirectory(dirPath, options) { const absolutePath = path.resolve(dirPath); const scopeLabel = options?.scopeLabel ?? "directory"; const existingAncestor = await findExistingAncestor(absolutePath); if (!existingAncestor) return { ok: false, error: /* @__PURE__ */ new Error(`Invalid path: must stay within ${scopeLabel}`) }; if (existingAncestor === absolutePath) { try { const stat = await fs.lstat(absolutePath); if (!stat.isSymbolicLink() && stat.isDirectory()) return { ok: true, path: absolutePath }; } catch {} return { ok: false, error: /* @__PURE__ */ new Error(`Invalid path: must stay within ${scopeLabel}`) }; } const result = await pathScope(existingAncestor, { label: options?.scopeLabel ?? "directory" }).ensureDir(path.relative(existingAncestor, absolutePath), { mode: options?.mode }); if (result.ok) return result; return { ok: false, error: new Error(result.error) }; } //#endregion export { ensureAbsoluteDirectory as t };