UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

150 lines (149 loc) • 5.62 kB
import { d as asPositiveFiniteNumber } from "./number-coercion-D6u-4YD8.mjs"; import { t as createPluginRuntimeProcessEntrypoint } from "./runtime-process-manifest-DmUAODRC.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { w as root } from "./fs-safe-BbWvmdVn.mjs"; import { a as resolveRuntimeWorkerUrl, n as resolveRuntimeWorkerArgv } from "./runtime-worker-url-DRDkoS4J.mjs"; import { t as runCommandBuffered } from "./exec-aRIvS9s4.mjs"; import { n as ArchiveLimitError } from "./archive-xLn9WKtS.mjs"; import "./security-runtime-C-zE_Zy_.mjs"; import "./process-runtime-DcBPEA8Y.mjs"; import "./number-runtime-W4chuk8B.mjs"; import "./error-runtime-BM740gJj.mjs"; import "./archive-CjZHeh-7.mjs"; import { a as DIR_FETCH_MAX_ENTRIES, i as DIR_FETCH_HARD_MAX_BYTES, n as inspectDirFetchArchive, r as DIR_FETCH_DEFAULT_MAX_BYTES } from "./dir-fetch-archive-DwOataU3.mjs"; import { n as matchesFileIdentity } from "./path-binding-CgKQ8yCF.mjs"; import { i as resolveBoundReadDirectory, n as readAbsolutePath, o as statRequiredDirectory, t as createReadErrorClassifier } from "./path-errors-Bc8by22e.mjs"; import path from "node:path"; import crypto from "node:crypto"; import fs from "node:fs/promises"; //#region extensions/file-transfer/src/node-host/dir-fetch-archive.ts const CANONICAL_PATH_CHANGED_EXIT_CODE = 78; const workerEntry = createPluginRuntimeProcessEntrypoint("fileTransferDirFetch", import.meta.url); async function createTarArchive(e, t, n, r, i) { const a = process.platform !== "win32" ? "/usr/bin/tar" : "tar"; const o = 6e4; const s = await runCommandBuffered([ process.execPath, ...resolveRuntimeWorkerArgv(resolveRuntimeWorkerUrl(workerEntry)), e, t, n, r, a ], { discardOutput: { stderr: true }, maxOutputBytes: { stdout: i, stderr: 65536 }, timeoutMs: o }).catch(() => null); if (!s) return "ERROR"; if (s.termination === "timeout") return "TIMEOUT"; if (s.termination === "output-limit" && s.outputLimitStream === "stdout") return "TOO_LARGE"; if (s.termination === "exit" && s.code === CANONICAL_PATH_CHANGED_EXIT_CODE) return "CANONICAL_PATH_CHANGED"; return s.termination === "exit" && s.code === 0 ? s.stdout : "ERROR"; } //#endregion //#region extensions/file-transfer/src/node-host/dir-fetch.ts const classifyFsError = createReadErrorClassifier({ ENOENT: "NOT_FOUND" }); async function listTreeEntries(e, t, n) { const r = []; const i = await root(e); const a = await fs.stat(i.rootReal, { bigint: true }); if (!matchesFileIdentity(a, n)) throw Object.assign(/* @__PURE__ */ new Error("filesystem identity differs from the authorized target"), { code: "CANONICAL_PATH_CHANGED" }); async function visit(e) { const n = await i.list(e, { withFileTypes: true }); for (const i of n.toSorted((e, t) => e.name.localeCompare(t.name) || (e.name < t.name ? -1 : 1))) { const n = path.posix.join(e, i.name); r.push(n); if (r.length > t) return false; if (i.isDirectory && !await visit(n)) return false; } return true; } return await visit(".") ? r : "TOO_MANY"; } async function handleDirFetch(e) { const t = readAbsolutePath(e.path); if (typeof t !== "string") return t; const n = Math.min(Math.floor(asPositiveFiniteNumber(e.maxBytes) ?? 8388608), DIR_FETCH_HARD_MAX_BYTES); const r = e.followSymlinks === true; const i = e.preflightOnly === true; const a = await resolveBoundReadDirectory({ requestedPath: t, followSymlinks: r, classifyError: classifyFsError, notFoundMessage: "directory not found", expectedCanonicalPath: e.expectedCanonicalPath, expectedBinding: e.expectedBinding }); if (!a.ok) return a; const { canonicalPath: o, identity: s } = a; const failure = (e, t) => ({ ok: false, code: e, message: t, canonicalPath: o }); let c; if (i) { let e; try { e = await listTreeEntries(o, DIR_FETCH_MAX_ENTRIES, s); } catch (e) { return failure((e && typeof e === "object" && "code" in e ? e.code : void 0) === "CANONICAL_PATH_CHANGED" ? "CANONICAL_PATH_CHANGED" : classifyFsError(e), `preflight readdir failed: ${String(e)}`); } if (e === "TOO_MANY") return failure("TREE_TOO_LARGE", "directory tree exceeds 5000 entries during preflight"); c = e; } const l = await createTarArchive(o, o, s.device, s.inode, n); if (l === "TOO_LARGE") return failure("TREE_TOO_LARGE", `tarball exceeded ${n} byte limit ${i ? "during preflight" : "mid-stream"}`); if (l === "TIMEOUT") return failure("READ_ERROR", "tar command exceeded 60s wall-clock timeout (slow filesystem or symlink loop?)"); if (l === "CANONICAL_PATH_CHANGED") return failure("CANONICAL_PATH_CHANGED", "resolved path differs from the authorized target"); if (l === "ERROR") { if (i) { const e = await statRequiredDirectory(o, classifyFsError); if (!e.ok) return e; } return failure("READ_ERROR", "tar command failed"); } if (c) return { ok: true, path: o, tarBase64: "", tarBytes: 0, sha256: "", fileCount: c.length, entries: c, preflightOnly: true, binding: { kind: "existing", ...s } }; const u = crypto.createHash("sha256").update(l).digest("hex"); const d = l.toString("base64"); const f = l.byteLength; let p; try { p = await inspectDirFetchArchive(l, 1e4); } catch (e) { return failure(e instanceof ArchiveLimitError ? "TREE_TOO_LARGE" : "READ_ERROR", `archive inspection failed: ${formatErrorMessage(e)}`); } return { ok: true, path: o, tarBase64: d, tarBytes: f, sha256: u, fileCount: p.length, entries: p, binding: { kind: "existing", ...s } }; } //#endregion export { handleDirFetch as t };