openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 5.72 kB
JavaScript
import{F as e,M as t,O as n,R as r,i,n as a,p as o,t as s}from"./openclaw-state-read-read-open-flags-CtNL1rJs.mjs";import{A as c,Z as l,c as u,ht as d,r as f}from"./openclaw-state-read-root-symlink-policy-AldRD6ik.mjs";import p from"node:fs";import m from"node:path";function isExpectedPathError(e){let t=typeof e==`object`&&e&&`code`in e?String(e.code):``;return t===`ENOENT`||t===`ENOTDIR`||t===`ELOOP`}function openPinnedFileSync(e){let a=e.filePath,c=e.resolvedPath,l=e.ioFs??p,u=e.allowedType??`file`,f=e.rejectPathSymlink===!0,m={rejectHardlinks:e.rejectHardlinks,rejectLeafSymlinks:e.rejectLeafSymlinks,maxBytes:e.maxBytes},h=s({constants:l.constants}),g=e.admittedFd??null;try{if(t(a,`filesystem`,`file path uses a Windows filesystem namespace alias`),c!==void 0&&t(c,`filesystem`,`resolved path uses a Windows filesystem namespace alias`),n(a)||f&&l.lstatSync(a).isSymbolicLink())return{ok:!1,reason:`validation`};let r=c??(l===p?d(a):l.realpathSync(a));if(t(r,`filesystem`,`resolved path uses a Windows filesystem namespace alias`),n(r))return{ok:!1,reason:`validation`};let s=o(()=>{let e=l.lstatSync(r,{bigint:!0});return assertAllowedStat(e,u,m),e});g??=l.openSync(r,h);let _=o(()=>{let e=l.fstatSync(g,{bigint:!0});return assertAllowedStat(e,u,m),e},s);o(()=>{let e=l.lstatSync(r,{bigint:!0});return assertAllowedStat(e,u,m),e},_);let v=e.finalAdmission?.({path:r,descriptorIdentity:_})??r,y=i(_),b={ok:!0,path:v,fd:g,stat:y,identity:_};return g=null,b}catch(e){return e instanceof r?{ok:!1,reason:`validation`,error:e}:isExpectedPathError(e)?{ok:!1,reason:`path`,error:e}:{ok:!1,reason:`io`,error:e}}finally{if(g!==null)try{l.closeSync(g)}catch{}}}function assertAllowedStat(e,t,n){if(e.isSymbolicLink()&&n.rejectLeafSymlinks)throw new r(`symlink`,`symlink not allowed`);if(e.isSymbolicLink()||!(t===`directory`?e.isDirectory():e.isFile()))throw new r(`not-file`,`path does not have the required file type`);if(n.rejectHardlinks&&e.isFile()&&e.nlink>1n)throw new r(`hardlink`,`path must not be hardlinked`);if(n.maxBytes!==void 0&&e.isFile()&&e.size>n.maxBytes)throw new r(`too-large`,`file exceeds byte limit`)}function rootIdentityMismatch(e){return new r(`path-mismatch`,`canonical root directory identity changed or could not be verified`,e===void 0?{}:{cause:e})}function inspectCanonicalRoot(n,r,i){try{t(r);let a=n===p?e(r):r;return o(()=>{let e=n.lstatSync(a,{bigint:!0});if(!e.isDirectory()||e.isSymbolicLink())throw rootIdentityMismatch();return e},i)}catch(e){throw i&&isExpectedPathError(e)?rootIdentityMismatch(e):e}}function observeCanonicalRoot(t,n){try{let r=inspectCanonicalRoot(t,n),i=Object.freeze({dev:r.dev,ino:r.ino}),a=n;return t===p&&(a=d.native(e(n)),a!==n&&inspectCanonicalRoot(t,a,i)),{ok:!0,path:a,identity:i}}catch(e){return{ok:!1,error:e}}}function resolveConsumedPath(e,t){return e===p?d.native(t):e.realpathSync(t)}function createRootFileFinalAdmission(e,n,i,a=!0){let inspectRoot=(t,n)=>{inspectCanonicalRoot(e,t,n)};return({path:s,descriptorIdentity:u})=>{inspectCanonicalRoot(e,n.path,n.identity);let d=resolveConsumedPath(e,s);t(d,`filesystem`,`resolved file path uses a Windows filesystem namespace alias`);let f=c({rootPath:n.path,candidatePath:d,rootIdentity:n.identity,inspectCandidateRoot:inspectRoot});if(!f)throw new r(`outside-workspace`,`resolved path escapes ${i}`);try{let t=o(()=>e.lstatSync(f.path,{bigint:!0}),u);if(a&&t.isFile()&&t.nlink>1n)throw l()}catch(e){throw isExpectedPathError(e)?new r(`path-mismatch`,`canonical file identity changed or could not be verified`,{cause:e}):e}return inspectCanonicalRoot(e,n.path,n.identity),f.path}}function canUseRootFileOpen(e){return typeof e.openSync==`function`&&typeof e.closeSync==`function`&&typeof e.fstatSync==`function`&&typeof e.lstatSync==`function`&&typeof e.realpathSync==`function`&&typeof e.readFileSync==`function`&&typeof e.constants==`object`&&e.constants!==null}function absoluteRootFilePath(e){if(m.isAbsolute(e))return e;let t=m.parse(e).root;return`${t?m.resolve(t):process.cwd()}${m.sep}${e.slice(t.length)}`}function openRootFileSync(e){let n=e.ioFs??p,r=e.absolutePath,i;try{let a=absoluteRootFilePath(r);t(a);let o=e.rootPath,s=e.rootRealPath,c=e.boundaryLabel,l;i=mapResolvedRootPath(a,c,u({absolutePath:a,rootPath:o,rootCanonicalPath:s,boundaryLabel:c,...f(e.symlinks??(e.rejectSymlinks===!1?`follow-within-root`:`reject`)),skipLexicalRootCheck:e.skipLexicalRootCheck},e=>{l=observeCanonicalRoot(n,e)}),l)}catch(e){i=toBoundaryValidationError(e)}return finalizeRootFileOpen({resolved:i,maxBytes:e.maxBytes,rejectHardlinks:e.rejectHardlinks,allowedType:e.allowedType,ioFs:n})}function finalizeRootFileOpen(e){if(`ok`in e.resolved)return e.resolved;let t=e.resolved,n=e.rejectHardlinks??!0,r=openPinnedFileSync({filePath:t.absolutePath,resolvedPath:t.resolvedPath,rejectHardlinks:n,maxBytes:e.maxBytes,allowedType:e.allowedType,ioFs:e.ioFs,admittedFd:e.admittedFd,rejectLeafSymlinks:e.rejectLeafSymlinks,finalAdmission:createRootFileFinalAdmission(e.ioFs,t.rootObservation,t.boundaryLabel,n)});return r.ok?{ok:!0,path:r.path,fd:r.fd,stat:r.stat,exactIdentity:a(r.identity),rootRealPath:t.rootRealPath}:r}function toBoundaryValidationError(e){return{ok:!1,reason:`validation`,error:e}}function mapResolvedRootPath(e,t,n,i){return i?i.ok?{absolutePath:e,resolvedPath:n.canonicalPath,rootRealPath:i.path,boundaryLabel:t,rootObservation:i}:toRootObservationError(i.error):toBoundaryValidationError(new r(`path-mismatch`,`canonical root identity was not observed`))}function toRootObservationError(e){return e instanceof r?toBoundaryValidationError(e):isExpectedPathError(e)?{ok:!1,reason:`path`,error:e}:{ok:!1,reason:`io`,error:e}}export{openRootFileSync as n,canUseRootFileOpen as t};