openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 2.7 kB
JavaScript
import{r as canonicalPathFromExistingAncestor,x as root}from"./fs-safe-Bxzu1j4h.mjs";import"./security-runtime-Y28JTB1K.mjs";import{n as matchesFileIdentity,t as fileIdentity}from"./path-binding-DEwRklUS.mjs";import path from"node:path";import fs from"node:fs/promises";
//!extensions/file-transfer/src/node-host/file-write-path.ts
function fileWriteError(e,t,n){return{ok:false,code:e,message:t,...n?{canonicalPath:n}:{}}}async function canonicalTargetForSymlinkError(e,t){const n=e.cause&&typeof e.cause===`object`&&`canonicalPath`in e.cause&&typeof e.cause.canonicalPath===`string`?e.cause.canonicalPath:void 0;if(n)return n;try{return await fs.realpath(t)}catch{return await canonicalPathFromExistingAncestor(t).catch(()=>void 0)}}function symlinkRedirectError(e,t){return fileWriteError(e,`path traverses a symlink; refusing because followSymlinks=false (set plugins.entries.file-transfer.config.nodes.<node>.followSymlinks=true to allow, or update allowWritePaths to the resolved path)`,t)}function writeFsSafeError(e,t){if(e.code===`symlink`)return fileWriteError(`SYMLINK_TARGET_DENIED`,`path is a symlink; refusing to write through it: ${t}`);if(e.code===`not-file`)return fileWriteError(`IS_DIRECTORY`,`path resolves to a directory: ${t}`);if(e.code===`already-exists`)return fileWriteError(`EXISTS_NO_OVERWRITE`,`file already exists and overwrite is false: ${t}`);return fileWriteError(`WRITE_ERROR`,e.message,t)}async function captureWriteBinding(e,t){let n=path.dirname(e);for(;;)try{const e=await fs.stat(n,{bigint:true});if(!e.isDirectory())throw new Error(`write anchor is not a directory: ${n}`);const r=fileIdentity(e);return{kind:`write`,anchorPath:n,anchorDevice:r.device,anchorInode:r.inode,...t?{targetDevice:t.device,targetInode:t.inode}:{}}}catch(e){if((e&&typeof e===`object`&&`code`in e?e.code:void 0)!==`ENOENT`)throw e;const t=path.dirname(n);if(t===n)throw e;n=t}}async function openBoundWriteRoot(e){let t;try{t=await root(e.binding.anchorPath);const n=await fs.stat(t.rootReal,{bigint:true});if(!matchesFileIdentity(n,{device:e.binding.anchorDevice,inode:e.binding.anchorInode}))throw new Error(`write anchor changed`)}catch{return fileWriteError(`CANONICAL_PATH_CHANGED`,`filesystem identity differs from the authorized target`,e.canonicalTargetPath)}const n=path.relative(t.rootReal,e.canonicalTargetPath);if(!n||path.isAbsolute(n)||n===`..`||n.startsWith(`..${path.sep}`))return fileWriteError(`WRITE_ERROR`,`write target is outside the authorized anchor`);return{ok:true,anchorRoot:t,relativeTarget:n}}export{symlinkRedirectError as a,openBoundWriteRoot as i,captureWriteBinding as n,writeFsSafeError as o,fileWriteError as r,canonicalTargetForSymlinkError as t};