openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
128 lines (127 loc) • 5.68 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { b as uniqueStrings } from "./string-normalization-DLmfYZXI.mjs";
import { r as resolvePathViaExistingAncestorSync } from "./boundary-path-C2gXEiXe.mjs";
import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs";
import { t as resolveConfigDir } from "./config-dir-DMqvtq2W.mjs";
import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs";
import "./utils-C8YECn2Q.mjs";
import { _ as resolveDeliveryQueueMediaDir } from "./paths-DAcAg5aV.mjs";
import { f as resolveAgentWorkspaceDir } from "./agent-scope-config-DmXgFGKP.mjs";
import { n as resolvePreferredOpenClawTmpDir } from "./tmp-openclaw-dir-Bf0oU1Cn.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import { t as resolveEffectiveToolFsRootExpansionAllowed } from "./tool-fs-policy-dDd9W7xo.mjs";
import { t as resolveLocalMediaPath } from "./local-media-path-R9G3PyAB.mjs";
import path from "node:path";
//#region src/media/local-roots.ts
let cachedPreferredTmpDir;
function resolveCanonicalRoot(e) {
return resolvePathViaExistingAncestorSync(path.resolve(e));
}
function buildMediaLocalRoots(e, t) {
const n = path.resolve(e);
const r = path.resolve(t);
cachedPreferredTmpDir ||= resolvePreferredOpenClawTmpDir();
return Array.from(/* @__PURE__ */ new Set([
cachedPreferredTmpDir,
path.join(r, "media"),
path.join(n, "media"),
resolveDeliveryQueueMediaDir(n),
path.join(n, "canvas"),
path.join(n, "workspace"),
path.join(n, "sandboxes")
]));
}
/** Returns the process default roots where local media reads may resolve generated/cache files. */
function getDefaultMediaLocalRoots() {
return buildMediaLocalRoots(resolveStateDir(), resolveConfigDir());
}
/**
* Drops shared isolation parents from a media root list.
*
* Roots overlapping the shared `<state>/sandboxes` parent are removed unless they live inside the
* supplied session workspace, so sibling sandboxes and the shared parent itself never re-enter the
* allowlist through base construction or source-parent expansion. The shared `<state>/workspace`
* parent is removed only when the caller supplies a session workspace outside it (a sandboxed
* session); callers without session context keep the legacy shared-workspace grant.
*/
function filterSharedMediaLocalRoots(e, t) {
const n = resolveCanonicalRoot(path.join(t.resolvedStateDir, "sandboxes"));
const r = resolveCanonicalRoot(path.join(t.resolvedStateDir, "workspace"));
const i = t.sessionWorkspaceDir ? resolveCanonicalRoot(t.sessionWorkspaceDir) : void 0;
const a = i !== void 0 && !isPathInside(i, n) ? i : void 0;
const overlaps = (e, t) => isPathInside(e, t) || isPathInside(t, e);
const o = [];
for (const t of e) {
const e = resolveCanonicalRoot(t);
const s = a !== void 0 && isPathInside(a, e);
if (overlaps(n, e) && !s) continue;
if (i !== void 0 && overlaps(r, e) && !s) continue;
o.push(e);
}
return o;
}
/**
* Default local media roots with shared isolation parents removed.
*
* Inbound attachment processing must not inherit sibling-sandbox grants from the process default
* list; callers re-add their own session/agent workspace explicitly when one applies.
*/
function getSessionSafeDefaultMediaLocalRoots(e) {
return filterSharedMediaLocalRoots(getDefaultMediaLocalRoots(), {
resolvedStateDir: path.resolve(resolveStateDir()),
sessionWorkspaceDir: e
});
}
/**
* Adds exact agent/session workspaces without exposing shared agent or sandbox roots.
*
* Callers that need to send media from a sandbox must pass the authoritative active
* session workspace, not a path derived from the requested media source. Omitting that
* context intentionally denies sandbox files under workspace-only filesystem policy.
*/
function getAgentScopedMediaLocalRoots(e, t, n) {
const r = resolveStateDir();
const i = path.resolve(r);
const a = filterSharedMediaLocalRoots(buildMediaLocalRoots(r, resolveConfigDir()), {
resolvedStateDir: i,
sessionWorkspaceDir: n
});
const o = normalizeOptionalString(t);
const s = normalizeOptionalString(n) ?? (o ? resolveAgentWorkspaceDir(e, o) : void 0);
if (!s) return a;
const c = resolveCanonicalRoot(s);
if (c === resolveCanonicalRoot(path.join(i, "sandboxes"))) return a;
if (!a.includes(c)) a.push(c);
return a;
}
/** Adds only concrete local source parent directories to an existing root allowlist. */
function appendLocalMediaParentRoots(e, t) {
const n = uniqueStrings(e.map((e) => path.resolve(e)));
for (const e of t ?? []) {
const t = resolveLocalMediaPath(e);
if (!t) continue;
const r = path.dirname(t);
if (r === path.parse(r).root) continue;
const i = resolveCanonicalRoot(r);
if (!n.includes(i)) n.push(i);
}
return n;
}
/**
* Resolves outbound media roots, expanding for local sources only when filesystem policy allows it.
* Pass `sessionWorkspaceDir` from trusted session context to retain access to that exact sandbox.
*/
function getAgentScopedMediaLocalRootsForSources(e) {
const t = getAgentScopedMediaLocalRoots(e.cfg, e.agentId, e.sessionWorkspaceDir);
if (!resolveEffectiveToolFsRootExpansionAllowed(e)) return t;
const n = appendLocalMediaParentRoots(t, e.mediaSources);
const r = n.filter((e) => !t.includes(e));
const i = filterSharedMediaLocalRoots(r, {
resolvedStateDir: path.resolve(resolveStateDir()),
sessionWorkspaceDir: e.sessionWorkspaceDir
});
if (i.length === r.length) return n;
return Array.from(/* @__PURE__ */ new Set([...t, ...i]));
}
//#endregion
export { getSessionSafeDefaultMediaLocalRoots as a, getDefaultMediaLocalRoots as i, getAgentScopedMediaLocalRoots as n, getAgentScopedMediaLocalRootsForSources as r, appendLocalMediaParentRoots as t };