UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 6.67 kB
import{r as canonicalPathFromExistingAncestor$1,t as FsSafeError,v as resolveAbsolutePathForWrite,x as root}from"./fs-safe-Bxzu1j4h.mjs";import{t as _usingCtx}from"./usingCtx-CUVyQ_FT.mjs";import{n as sha256Hex}from"./node-crypto-DPk8agfR.mjs";import"./security-runtime-Y28JTB1K.mjs";import{n as matchesFileIdentity,r as readPathBinding,t as fileIdentity}from"./path-binding-DEwRklUS.mjs";import{r as rejectCanonicalPathChange}from"./path-errors-BePH4DjW.mjs";import{a as symlinkRedirectError,i as openBoundWriteRoot,n as captureWriteBinding,o as writeFsSafeError,r as fileWriteError,t as canonicalTargetForSymlinkError}from"./file-write-path-D1RZarC9.mjs";import{n as normalizeBase64ForCompare,t as inspectStrictBase64}from"./base64-CICthlm-.mjs";import path from"node:path";import fs from"node:fs/promises";import{overwriteFileHandle}from"@openclaw/fs-safe/advanced"; //!extensions/file-transfer/src/node-host/file-write.ts const MAX_CONTENT_BYTES=16777216;async function handleFileWrite(e){const t=typeof e?.path===`string`?e.path:``;const n=typeof e?.contentBase64===`string`;const r=n?e.contentBase64:``;const i=e?.overwrite===true;const a=e?.createParents===true;const o=typeof e?.expectedSha256===`string`?e.expectedSha256:void 0;const s=e?.followSymlinks===true;const c=e?.preflightOnly===true;const l=e?.rejectHardlinks===true;if(!t)return fileWriteError(`INVALID_PATH`,`path is required`);if(t.includes(`\0`))return fileWriteError(`INVALID_PATH`,`path must not contain NUL bytes`);if(!path.isAbsolute(t))return fileWriteError(`INVALID_PATH`,`path must be absolute`);if(!n)return fileWriteError(`INVALID_BASE64`,`contentBase64 is required`);const u=inspectStrictBase64(r);if(u===void 0)return fileWriteError(`INVALID_BASE64`,`contentBase64 is not valid base64`);if(u>MAX_CONTENT_BYTES)return fileWriteError(`FILE_TOO_LARGE`,`decoded content is ${u} bytes; maximum is ${MAX_CONTENT_BYTES} bytes (16 MB)`);const d=Buffer.from(r,`base64`);const f=d.toString(`base64`);if(normalizeBase64ForCompare(f)!==normalizeBase64ForCompare(r))return fileWriteError(`INVALID_BASE64`,`contentBase64 is not valid base64`);let p;let m;let h;try{const e=await resolveAbsolutePathForWrite(t,{symlinks:s?`follow`:`reject`});p=e.path;m=e.parentDir;h=e.parentExists}catch(e){if(e instanceof FsSafeError&&e.code===`symlink`)return symlinkRedirectError(`SYMLINK_REDIRECT`,await canonicalTargetForSymlinkError(e,t));throw e}const g=await canonicalPathFromExistingAncestor$1(p);const _=rejectCanonicalPathChange(e.expectedCanonicalPath,g);if(_)return _;const v=readPathBinding(e.expectedBinding);if(e.expectedBinding!==void 0&&v?.kind!==`write`)return fileWriteError(`CANONICAL_PATH_CHANGED`,`filesystem identity differs from the authorized target`,g);if(!h){if(!a)return fileWriteError(`PARENT_NOT_FOUND`,`parent directory does not exist: ${m}`);if(!c&&!v)try{await fs.mkdir(m,{recursive:true})}catch(e){const t=e instanceof Error?e.message:String(e);return fileWriteError(`WRITE_ERROR`,`failed to create parent directories: ${t}`)}}try{await resolveAbsolutePathForWrite(p,{symlinks:s?`follow`:`reject`})}catch(e){if(e instanceof FsSafeError&&e.code===`symlink`)return symlinkRedirectError(`SYMLINK_REDIRECT`,await canonicalTargetForSymlinkError(e,p));throw e}const y=path.basename(p);let b=false;let x;try{const e=await fs.lstat(p,{bigint:true});if(e.isSymbolicLink())return fileWriteError(`SYMLINK_TARGET_DENIED`,`path is a symlink; refusing to write through it: ${p}`);if(e.isDirectory())return fileWriteError(`IS_DIRECTORY`,`path resolves to a directory: ${p}`);if(!i)return fileWriteError(`EXISTS_NO_OVERWRITE`,`file already exists and overwrite is false: ${p}`);if(l&&e.nlink>1n)return fileWriteError(`HARDLINK_TARGET_DENIED`,`refusing to overwrite a file with hard links`);b=true;x=fileIdentity(e)}catch(e){const t=e instanceof FsSafeError?e.code:e.code;if(t!==`not-found`&&t!==`ENOENT`){const t=e instanceof Error?e.message:String(e);if(t.toLowerCase().includes(`permission`))return fileWriteError(`PERMISSION_DENIED`,`permission denied: ${p}`);return fileWriteError(`WRITE_ERROR`,`unexpected stat error: ${t}`)}}const S=sha256Hex(d);if(o&&o.toLowerCase()!==S)return fileWriteError(`INTEGRITY_FAILURE`,`sha256 mismatch: expected ${o.toLowerCase()}, got ${S}`,p);if(c)return{ok:true,path:g,size:d.length,sha256:S,overwritten:b,binding:await captureWriteBinding(g,x),...l?{rejectHardlinks:true}:{}};if(v?.kind===`write`){const e=v.targetDevice&&v.targetInode?{device:v.targetDevice,inode:v.targetInode}:void 0;if(e){let t;try{t=await fs.open(g,`r+`)}catch{return fileWriteError(`CANONICAL_PATH_CHANGED`,`filesystem identity differs from the authorized target`,g)}try{const n=await t.stat({bigint:true});if(!n.isFile()||!matchesFileIdentity(n,e))return fileWriteError(`CANONICAL_PATH_CHANGED`,`filesystem identity differs from the authorized target`,g);if(l&&n.nlink>1n)return fileWriteError(`HARDLINK_TARGET_DENIED`,`refusing to overwrite a file with hard links`);await overwriteFileHandle(t,d);await t.sync();return{ok:true,path:g,size:d.length,sha256:S,overwritten:true,binding:{kind:`existing`,...fileIdentity(n)}}}finally{await t.close().catch(()=>void 0)}}const t=await openBoundWriteRoot({binding:v,canonicalTargetPath:g});if(!t.ok)return t;const{anchorRoot:n,relativeTarget:r}=t;try{try{var C=_usingCtx();await n.create(r,d,{mkdir:true});const e=C.a(await n.open(r));const t=await e.handle.stat({bigint:true});return{ok:true,path:e.realPath,size:d.length,sha256:S,overwritten:false,binding:{kind:`existing`,...fileIdentity(t)}}}catch(e){C.e=e}finally{await C.d()}}catch(e){if(e instanceof FsSafeError&&e.code===`already-exists`)return fileWriteError(`CANONICAL_PATH_CHANGED`,`filesystem identity differs from the authorized target`,g);if(e instanceof FsSafeError)return writeFsSafeError(e,g);return fileWriteError(`WRITE_ERROR`,`failed to write file: ${String(e)}`)}}const w=await root(m);try{if(i)await w.write(y,d);else await w.create(y,d)}catch(e){if(e instanceof FsSafeError)return writeFsSafeError(e,p);const t=e instanceof Error?e.message:String(e);if(t.toLowerCase().includes(`permission`)||t.toLowerCase().includes(`access`))return fileWriteError(`PERMISSION_DENIED`,`permission denied writing to: ${m}`);return fileWriteError(`WRITE_ERROR`,`failed to write file: ${t}`)}let T=p;let E;try{try{var D=_usingCtx();const e=D.a(await w.open(y));T=e.realPath;E=fileIdentity(await e.handle.stat({bigint:true}))}catch(e){D.e=e}finally{await D.d()}}catch(e){if(e instanceof FsSafeError)return writeFsSafeError(e,p)}return{ok:true,path:T,size:d.length,sha256:S,overwritten:b,binding:{kind:`existing`,...E??fileIdentity(await fs.stat(T,{bigint:true}))}}}export{handleFileWrite};