openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
4 lines • 6.01 kB
JavaScript
import{l as isRecord}from"./record-coerce-BTbqdJ3-.mjs";import{r as isPathInside}from"./path-guards-DGttsOJI.mjs";import{n as isSandboxHostPathAbsolute,r as normalizeSandboxHostPath}from"./host-paths-CmrZGmeh.mjs";import{n as normalizeMountContainerPath,u as sandboxMountOptionsReadOnly}from"./workspace-mounts-DefzGu84.mjs";import{i as execContainer}from"./container-engine-BMQJ1AeL.mjs";import fs from"node:fs";import path from"node:path";import os from"node:os";
//!src/agents/sandbox/docker-mount-source.ts
const SELF_INSPECT_TIMEOUT_MS=5e3;const MAX_SELF_CANDIDATES=8;const SELF_IDENTITY_PROBE=`const fs=require("node:fs");process.stdout.write(JSON.stringify([fs.readFileSync("/proc/sys/kernel/random/boot_id","utf8").trim(),fs.readlinkSync("/proc/self/ns/mnt")]));`;let sourceNamespace;function parseInspectedSandboxMounts(e,t){if(!Array.isArray(e))throw new Error(`Container inspect did not return a mount table.`);const n=e.map(e=>{if(!isRecord(e)||typeof e.Type!==`string`||typeof e.Destination!==`string`||!path.posix.isAbsolute(e.Destination)||typeof e.RW!==`boolean`||e.Type===`bind`&&(typeof e.Source!==`string`||!isSandboxHostPathAbsolute(e.Source)))throw new Error(`Container inspect returned an invalid mount entry.`);return{type:e.Type,source:typeof e.Source===`string`?normalizeSandboxHostPath(e.Source):``,destination:normalizeMountContainerPath(e.Destination),writable:e.RW}});if(t!=null&&!isRecord(t))throw new Error(`Container inspect returned invalid tmpfs destinations.`);for(const[e,r]of Object.entries(t??{})){if(!path.posix.isAbsolute(e)||typeof r!==`string`)throw new Error(`Container inspect returned an invalid tmpfs destination.`);n.push({type:`tmpfs`,source:``,destination:normalizeMountContainerPath(e),writable:!sandboxMountOptionsReadOnly(r)})}return[...new Map(n.map(e=>[e.destination,e])).values()]}function readOptionalProcFile(e){try{return fs.readFileSync(e,`utf8`)}catch{return``}}async function discoverSourceNamespace(e){const t=readOptionalProcFile(`/proc/self/cgroup`);const n=[];for(const e of t.matchAll(/(?:\/docker\/|docker-)([a-f0-9]{64})(?:\/|\.scope|$)/gm)){const t=e[1];if(t)n.push(t)}for(const e of readOptionalProcFile(`/proc/self/mountinfo`).split(`
`)){const t=e.split(` `);if(!/^\/etc\/(?:hosts|hostname|resolv\.conf)$/.test(t[4]??``))continue;const r=t[3]?.match(/\/containers\/([a-f0-9]{64})\/(?:hosts|hostname|resolv\.conf)$/)?.[1];if(r)n.push(r)}if(!fs.existsSync(`/.dockerenv`)&&!fs.existsSync(`/run/.containerenv`)&&n.length===0)return;const r=[.../* @__PURE__ */ new Set([os.hostname(),...n])].slice(0,MAX_SELF_CANDIDATES);const i=AbortSignal.timeout(SELF_INSPECT_TIMEOUT_MS);let a;try{const t=JSON.stringify([fs.readFileSync(`/proc/sys/kernel/random/boot_id`,`utf8`).trim(),fs.readlinkSync(`/proc/self/ns/mnt`)]);for(const n of r)try{const r=await execContainer(e,[`inspect`,`--type`,`container`,`--format`,`{"Id":{{json .ID}},"Mounts":{{json .Mounts}},"Tmpfs":{{json .HostConfig.Tmpfs}}}`,n],{signal:i});const a=JSON.parse(r.stdout);if(!isRecord(a)||typeof a.Id!==`string`||!/^[a-f0-9]{64}$/.test(a.Id))throw new Error(`Container inspect did not return a full container ID.`);if((await execContainer(e,[`exec`,a.Id,process.execPath,`-e`,SELF_IDENTITY_PROBE],{signal:i})).stdout!==t)throw new Error(`The selected Docker daemon did not identify this Gateway container.`);return parseInspectedSandboxMounts(a.Mounts,a.Tmpfs)}catch(e){a=e;if(i.aborted)break}}catch(e){a=e}throw new Error(`Cannot resolve sandbox bind sources from this Gateway container. Connect Docker to the daemon that runs the Gateway and make its container inspectable, then restart the Gateway. A hostname alone cannot establish container identity.`,{cause:a})}async function resolveDockerSourceNamespace(e){if(e.id!==`docker`||process.platform!==`linux`)return;const t=JSON.stringify([e,process.env.DOCKER_HOST,process.env.DOCKER_CONTEXT]);if(sourceNamespace?.key===t)return await sourceNamespace.mounts;const n={key:t,mounts:discoverSourceNamespace(e)};sourceNamespace=n;try{return await n.mounts}catch(e){if(sourceNamespace===n)sourceNamespace=void 0;throw e}}function translateSandboxMountSources(e){const t=fs.realpathSync(e.source);if(!e.allowedRoots.some(e=>isPathInside(fs.realpathSync(e),t)))throw new Error(`Sandbox mount source ${e.source} escapes its Gateway workspace roots.`);const n=e.mounts.filter(e=>isPathInside(e.destination,t)).toSorted((e,t)=>t.destination.length-e.destination.length)[0];if(!n||n.type!==`bind`)throw new Error(`Sandbox mount source ${e.source} ${n?`uses an unsupported ${n.type} mount`:`is not backed by a Gateway bind mount`}. Bind-mount the workspace and OpenClaw state directories from the Docker host into the Gateway, then restart the Gateway.`);if(!n.writable&&!e.readOnly)throw new Error(`Sandbox mount source ${e.source} is read-only in the Gateway. Use workspaceAccess=ro or make the Gateway bind writable before requesting a writable sandbox.`);const r=normalizeMountContainerPath(e.containerPath);const i=[{hostPath:path.posix.join(n.source,path.posix.relative(n.destination,t)),containerPath:r,readOnly:e.readOnly}];for(const n of e.mounts.toSorted((e,t)=>e.destination<t.destination?-1:e.destination>t.destination?1:0)){if(n.destination===t||!isPathInside(t,n.destination))continue;const a=normalizeMountContainerPath(path.posix.join(r,path.posix.relative(t,n.destination)));if(e.shadowedTargets.some(e=>isPathInside(e,a)))continue;if(n.type!==`bind`)throw new Error(`Sandbox mount source ${n.destination} uses an unsupported nested ${n.type} mount. Use Gateway bind mounts for this subtree or replace its sandbox destination with an explicit Docker bind, then restart the Gateway.`);if(!isPathInside(t,fs.realpathSync(n.destination)))throw new Error(`Sandbox mount source ${n.destination} escapes its Gateway workspace root.`);i.push({hostPath:n.source,containerPath:a,readOnly:e.readOnly||!n.writable})}return i}export{resolveDockerSourceNamespace as n,translateSandboxMountSources as r,parseInspectedSandboxMounts as t};