UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 2.12 kB
import path from"node:path"; //!packages/media-core/src/inbound-path-policy.ts const WILDCARD_SEGMENT=`*`;const WINDOWS_DRIVE_ABS_RE=/^[A-Za-z]:\//;const WINDOWS_DRIVE_ROOT_RE=/^[A-Za-z]:$/;function normalizePosixAbsolutePath(e){const t=e.trim();if(!t||t.includes(`\0`))return;const n=path.posix.normalize(t.replaceAll(`\\`,`/`));if(!(n.startsWith(`/`)||WINDOWS_DRIVE_ABS_RE.test(n))||n===`/`)return;const r=n.endsWith(`/`)?n.slice(0,-1):n;if(WINDOWS_DRIVE_ROOT_RE.test(r))return;return WINDOWS_DRIVE_ABS_RE.test(r)?r.toLowerCase():r}function splitPathSegments(e){return e.split(`/`).filter(Boolean)}function joinAbsolutePathSegments(e,t){const n=t.join(`/`);if(!WINDOWS_DRIVE_ABS_RE.test(e))return`/${n}`;return t.length===1?`${n}/`:n}function normalizeInboundPathRootPattern(e){const t=normalizePosixAbsolutePath(e);if(!t)return;const n=splitPathSegments(t);return n.length>0&&n.every(e=>e===WILDCARD_SEGMENT||!e.includes(`*`))?t:void 0}function isValidInboundPathRootPattern(e){return normalizeInboundPathRootPattern(e)!==void 0}function normalizeInboundPathRoots(e){const t=[];const n=/* @__PURE__ */ new Set;for(const r of e??[]){if(typeof r!==`string`)continue;const e=normalizeInboundPathRootPattern(r);if(!e||n.has(e))continue;n.add(e);t.push(e)}return t}function mergeInboundPathRoots(...e){return normalizeInboundPathRoots(e.flatMap(e=>e??[]))}function resolveInboundPathRoot(e){const t=normalizePosixAbsolutePath(e.filePath);if(!t)return;const n=normalizeInboundPathRoots(e.roots);const r=n.length>0?n:normalizeInboundPathRoots(e.fallbackRoots??void 0);const i=splitPathSegments(t);for(const e of r){const n=splitPathSegments(e);if(i.length<n.length||n.some((e,t)=>e!==WILDCARD_SEGMENT&&e!==i[t]))continue;const r=i.slice(0,n.length);const a=n.indexOf(WILDCARD_SEGMENT);return{anchorRoot:joinAbsolutePathSegments(t,a===-1?r:n.slice(0,a)),matchedRoot:joinAbsolutePathSegments(t,r)}}}function isInboundPathAllowed(e){return resolveInboundPathRoot(e)!==void 0}export{resolveInboundPathRoot as a,normalizeInboundPathRoots as i,isValidInboundPathRootPattern as n,mergeInboundPathRoots as r,isInboundPathAllowed as t};