UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

78 lines (77 loc) • 3.08 kB
import { a as canonicalPathFromExistingAncestor, w as root } from "./fs-safe-BbWvmdVn.mjs"; import "./security-runtime-C-zE_Zy_.mjs"; import { n as matchesFileIdentity, t as fileIdentity } from "./path-binding-CgKQ8yCF.mjs"; import path from "node:path"; import fs from "node:fs/promises"; //#region extensions/file-transfer/src/node-host/file-write-path.ts function fileWriteError(e, t, n) { return { ok: false, code: e, message: t, ...n ? { canonicalPath: n } : {} }; } async function canonicalTargetForSymlinkError(e, t) { const n = e.cause && typeof e.cause === "object" && "canonicalPath" in e.cause && typeof e.cause.canonicalPath === "string" ? e.cause.canonicalPath : void 0; if (n) return n; try { return await fs.realpath(t); } catch { return await canonicalPathFromExistingAncestor(t).catch(() => void 0); } } function symlinkRedirectError(e, t) { return fileWriteError(e, "path traverses a symlink; refusing because followSymlinks=false (set plugins.entries.file-transfer.config.nodes.<node>.followSymlinks=true to allow, or update allowWritePaths to the resolved path)", t); } function writeFsSafeError(e, t) { if (e.code === "symlink") return fileWriteError("SYMLINK_TARGET_DENIED", `path is a symlink; refusing to write through it: ${t}`); if (e.code === "not-file") return fileWriteError("IS_DIRECTORY", `path resolves to a directory: ${t}`); if (e.code === "already-exists") return fileWriteError("EXISTS_NO_OVERWRITE", `file already exists and overwrite is false: ${t}`); return fileWriteError("WRITE_ERROR", e.message, t); } async function captureWriteBinding(e, t) { let n = path.dirname(e); for (;;) try { const e = await fs.stat(n, { bigint: true }); if (!e.isDirectory()) throw new Error(`write anchor is not a directory: ${n}`); const r = fileIdentity(e); return { kind: "write", anchorPath: n, anchorDevice: r.device, anchorInode: r.inode, ...t ? { targetDevice: t.device, targetInode: t.inode } : {} }; } catch (e) { if ((e && typeof e === "object" && "code" in e ? e.code : void 0) !== "ENOENT") throw e; const t = path.dirname(n); if (t === n) throw e; n = t; } } async function openBoundWriteRoot(e) { let t; try { t = await root(e.binding.anchorPath); const n = await fs.stat(t.rootReal, { bigint: true }); if (!matchesFileIdentity(n, { device: e.binding.anchorDevice, inode: e.binding.anchorInode })) throw new Error("write anchor changed"); } catch { return fileWriteError("CANONICAL_PATH_CHANGED", "filesystem identity differs from the authorized target", e.canonicalTargetPath); } const n = path.relative(t.rootReal, e.canonicalTargetPath); if (!n || path.isAbsolute(n) || n === ".." || n.startsWith(`..${path.sep}`)) return fileWriteError("WRITE_ERROR", "write target is outside the authorized anchor"); return { ok: true, anchorRoot: t, relativeTarget: n }; } //#endregion export { symlinkRedirectError as a, openBoundWriteRoot as i, captureWriteBinding as n, writeFsSafeError as o, fileWriteError as r, canonicalTargetForSymlinkError as t };