openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
163 lines (162 loc) • 6.58 kB
JavaScript
import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs";
import { t as FsSafeError, u as openLocalFileSafely } from "./fs-safe-BbWvmdVn.mjs";
import { t as _usingCtx } from "./usingCtx-rhpPzaNq.mjs";
import { t as MediaReferenceError } from "./inbound-media-uri-DpK4_BLW.mjs";
import { n as captureChannelReadScope } from "./channel-read-authority-BVl4WY3A.mjs";
import { a as resolveInboundPathRoot } from "./inbound-path-policy-BMtpIEux.mjs";
import { i as getDefaultMediaLocalRoots } from "./local-roots-BTz6ULME.mjs";
import { n as resolveInboundMediaReference } from "./media-reference-DDon-yG0.mjs";
import path from "node:path";
import fs from "node:fs/promises";
import { assertNoWindowsNetworkPath, readFileHandleBounded } from "@openclaw/fs-safe/advanced";
//#region src/media/local-media-access.ts
var LocalMediaAccessError = class extends Error {
constructor(e, t, n) {
super(t, n);
this.code = e;
this.name = "LocalMediaAccessError";
}
};
/**
* Lets core classify rejected content without changing loadWebMedia's public error code.
* Removing this boundary would make detailed reply outcomes break plugin error handling.
*/
var HostReadMediaTypeError = class extends LocalMediaAccessError {
constructor(e) {
super("path-not-allowed", e);
}
};
async function resolveCanonicalBoundaryPath(e) {
const t = path.resolve(e);
try {
return await fs.realpath(t);
} catch {
return t;
}
}
/** Resolves an allowlist once for callers that validate several media paths. */
async function resolveLocalMediaRoots(e) {
const t = e ?? getDefaultMediaLocalRoots();
return await Promise.all(t.map(async (e) => {
const t = await resolveCanonicalBoundaryPath(e);
if (t === path.parse(t).root) throw new LocalMediaAccessError("invalid-root", `Invalid localRoots entry (refuses filesystem root): ${e}. Pass a narrower directory.`);
return t;
}));
}
async function resolveLocalMediaPathForContainment(e) {
try {
return await fs.realpath(e);
} catch {
try {
return path.join(await fs.realpath(path.dirname(e)), path.basename(e));
} catch {
return path.resolve(e);
}
}
}
async function resolveLocalMediaBoundary(e, t, n, r) {
if (t === "any") return {
rejectHardlinks: false,
roots: "any"
};
let i;
try {
i = await resolveInboundMediaReference(e);
} catch (e) {
if (n === "reject" && e instanceof MediaReferenceError) throw new LocalMediaAccessError(e.code, e.message, { cause: e });
if (!(e instanceof MediaReferenceError)) throw e;
}
if (i) return {
rejectHardlinks: true,
roots: await resolveLocalMediaRoots([path.dirname(i.physicalPath)])
};
try {
assertNoWindowsNetworkPath(e, "Local media path");
} catch (e) {
throw new LocalMediaAccessError("network-path-not-allowed", e.message, { cause: e });
}
const a = r?.inboundRoots?.length ? resolveInboundPathRoot({
filePath: e,
roots: r.inboundRoots
}) : void 0;
if (a) {
const t = await resolveCanonicalBoundaryPath(a.anchorRoot);
const n = await resolveCanonicalBoundaryPath(a.matchedRoot);
if (!isPathInside(t, n)) throw new LocalMediaAccessError("path-not-allowed", `Local media path is not under an allowed directory: ${e}`);
return {
rejectHardlinks: true,
roots: [n]
};
}
const o = t ?? getDefaultMediaLocalRoots();
const s = await resolveLocalMediaPathForContainment(e);
const c = await r?.resolveRoots?.() ?? await resolveLocalMediaRoots(o);
const l = o[o.findIndex((e) => path.basename(e) === "workspace")];
if (l) {
const n = await resolveCanonicalBoundaryPath(path.dirname(l));
const r = path.relative(n, s);
const i = r.split(path.sep)[0] ?? "";
if (r && isPathInside(n, s) && i.startsWith("workspace-")) {
const r = path.join(n, i);
if (!(t !== void 0 && c.some((e) => isPathInside(r, e) && isPathInside(e, s)))) throw new LocalMediaAccessError("path-not-allowed", `Local media path is not under an allowed directory: ${e}`);
}
}
for (const [e, t] of c.entries()) {
const n = o[e] ?? t;
if (t === path.parse(t).root) throw new LocalMediaAccessError("invalid-root", `Invalid localRoots entry (refuses filesystem root): ${n}. Pass a narrower directory.`);
if (isPathInside(t, s)) return {
rejectHardlinks: false,
roots: c
};
}
throw new LocalMediaAccessError("path-not-allowed", `Local media path is not under an allowed directory: ${e}`);
}
/** Verifies that a local media path is managed inbound media or lives under allowed roots. */
async function assertLocalMediaAllowed(e, t, n) {
await resolveLocalMediaBoundary(e, t, "ignore", n);
}
/** Opens local media against one frozen root boundary without buffering its contents. */
async function openLocalMediaFile(e, t, n) {
const r = captureChannelReadScope();
r?.assertCurrent();
const i = await resolveLocalMediaBoundary(e, t, "reject", n);
const a = n.excludedRoots?.length ? await resolveLocalMediaRoots(n.excludedRoots) : [];
r?.assertCurrent();
const o = await openLocalFileSafely({ filePath: e });
try {
if (a.some((e) => isPathInside(e, o.realPath))) throw new LocalMediaAccessError("path-not-allowed", `Local media path belongs to a remote workspace: ${e}`);
if (i.roots !== "any" && !i.roots.some((e) => isPathInside(e, o.realPath))) throw new LocalMediaAccessError("path-not-allowed", `Local media path is not under an allowed directory: ${e}`);
if (i.rejectHardlinks && o.stat.nlink > 1) throw new FsSafeError("hardlink", "hardlinked path not allowed");
if (o.stat.size > n.maxBytes) throw new FsSafeError("too-large", `file exceeds limit of ${n.maxBytes} bytes (got ${o.stat.size})`);
r?.assertCurrent();
return o;
} catch (e) {
await o[Symbol.asyncDispose]();
throw e;
}
}
/** Opens, revalidates, and bounded-reads local media against one frozen root boundary. */
async function readLocalMediaFile(e, t, n) {
try {
var r = _usingCtx();
const i = captureChannelReadScope();
const a = r.a(await openLocalMediaFile(e, t, n));
if (!i) return await readFileHandleBounded(a.handle, n.maxBytes);
const o = {
fd: a.handle.fd,
async read(e, t, n, r) {
i.assertCurrent();
const o = ArrayBuffer.isView(e) ? typeof t === "object" && t !== null ? await a.handle.read(e, t) : await a.handle.read(e, t, n, r) : await a.handle.read(e);
i.assertCurrent();
return o;
}
};
return await readFileHandleBounded(o, n.maxBytes);
} catch (e) {
r.e = e;
} finally {
await r.d();
}
}
//#endregion
export { readLocalMediaFile as a, openLocalMediaFile as i, LocalMediaAccessError as n, resolveLocalMediaRoots as o, assertLocalMediaAllowed as r, HostReadMediaTypeError as t };