openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 6.07 kB
JavaScript
import{r as isPathInside}from"./path-guards-DGttsOJI.mjs";import{c as openLocalFileSafely,t as FsSafeError}from"./fs-safe-Bxzu1j4h.mjs";import{r as canonicalizeForContainment,t as canonicalizeContainmentRoot}from"./path-containment-Drv7Ulmz.mjs";import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.mjs";import{t as MediaReferenceError}from"./inbound-media-uri-COqbYFEq.mjs";import{n as captureChannelReadScope}from"./channel-read-authority-BheGEIiJ.mjs";import{t as settleCurrentReadPreparations}from"./current-read-authority-J_xgisvq.mjs";import{a as resolveInboundPathRoot}from"./inbound-path-policy-CPnRL2ib.mjs";import{i as getDefaultMediaLocalRoots}from"./local-roots-CThUmghK.mjs";import{n as resolveInboundMediaReference}from"./media-reference-CVYMjRlF.mjs";import path from"node:path";import{assertNoWindowsNetworkPath,readFileHandleBounded}from"@openclaw/fs-safe/advanced";
//!src/media/local-media-access.ts
var LocalMediaAccessError=class extends Error{constructor(e,t,n){super(t,n);this.code=e;this.name=`LocalMediaAccessError`}};var HostReadMediaTypeError=class extends LocalMediaAccessError{constructor(e){super(`path-not-allowed`,e)}};const resolveCanonicalBoundaryPath=canonicalizeContainmentRoot;function localMediaNotFoundError(e,t){return new LocalMediaAccessError(`not-found`,`Local media file not found: ${e}. The path is in an allowed folder, but the file or one of its parent folders does not exist; check the path or create the file.`,{cause:t})}function pathNotAllowedError(e){return new LocalMediaAccessError(`path-not-allowed`,`Local media path is not under an allowed directory: ${e}. Use a file inside the agent workspace or another allowed media folder.`)}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){const t=await canonicalizeForContainment(e);return t.kind===`resolved`?{path:t.path,...t.danglingLink?{danglingLink:t.danglingLink}:{}}:void 0}function brokenLinkError(e,t,n){return new LocalMediaAccessError(`not-found`,`Local media file not found: ${e}. ${t} is a symbolic link to a target that does not exist; fix the link or use the file's real path.`,{cause:n})}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,n]=await settleCurrentReadPreparations([resolveCanonicalBoundaryPath(a.anchorRoot),resolveCanonicalBoundaryPath(a.matchedRoot)]);if(!isPathInside(t,n))throw pathNotAllowedError(e);return{rejectHardlinks:true,roots:[n]}}const o=t??getDefaultMediaLocalRoots();const[s,c]=await settleCurrentReadPreparations([resolveLocalMediaPathForContainment(e),r?.resolveRoots?.()??resolveLocalMediaRoots(o)]);if(!s)throw pathNotAllowedError(e);const l=s.path;const u=o[o.findIndex(e=>path.basename(e)===`workspace`)];if(u){const n=await resolveCanonicalBoundaryPath(path.dirname(u));const r=path.relative(n,l);const i=r.split(path.sep)[0]??``;if(r&&isPathInside(n,l)&&i.startsWith(`workspace-`)){const r=path.join(n,i);if(!(t!==void 0&&c.some(e=>isPathInside(r,e)&&isPathInside(e,l))))throw pathNotAllowedError(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,l))return{rejectHardlinks:false,roots:c,...s.danglingLink?{danglingLink:s.danglingLink}:{}}}throw pathNotAllowedError(e)}async function assertLocalMediaAllowed(e,t,n){await resolveLocalMediaBoundary(e,t,`ignore`,n)}async function openLocalMediaFile(e,t,n){const r=captureChannelReadScope();r?.assertCurrent();const[i,a]=await settleCurrentReadPreparations([resolveLocalMediaBoundary(e,t,`reject`,n),n.excludedRoots?.length?resolveLocalMediaRoots(n.excludedRoots):[]]);r?.assertCurrent();const o=await openLocalFileSafely({filePath:e}).catch(t=>{if(i.danglingLink&&t instanceof FsSafeError&&t.code===`not-found`)throw brokenLinkError(e,i.danglingLink,t);throw t});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 pathNotAllowedError(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}}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()}}export{openLocalMediaFile as a,localMediaNotFoundError as i,LocalMediaAccessError as n,readLocalMediaFile as o,assertLocalMediaAllowed as r,resolveLocalMediaRoots as s,HostReadMediaTypeError as t};