openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 3.63 kB
JavaScript
import path from"node:path";import{isWindowsDrivePath}from"@openclaw/fs-safe/archive";
//!src/infra/backup-archive-path-policy.ts
function normalizeWindowsNamespaceAlias(e){const t=e.replaceAll(`\\`,`/`).match(/^\/\/[?.]\/(.*)$/i);if(!t)return e;const n=t[1]??``;const r=/^UNC\/(.*)$/i.exec(n);const i=r?`//${r[1]??``}`:/^[A-Za-z]:\//.test(n)?n:void 0;if(!i)return e;return e.includes(`\\`)?i.replaceAll(`/`,`\\`):i}function encodeAbsolutePathForBackupArchive(e){const t=normalizeWindowsNamespaceAlias(e).replaceAll(`\\`,`/`);const n=t.match(/^([A-Za-z]):\/(.*)$/);if(n){const e=n[1]?.toUpperCase()??`UNKNOWN`;const t=n[2]??``;return path.posix.join(`windows`,e,t)}if(t.startsWith(`/`))return path.posix.join(`posix`,t.slice(1));return path.posix.join(`relative`,t)}function buildBackupArchivePath(e,t){return path.posix.join(e,`payload`,encodeAbsolutePathForBackupArchive(t))}function assertPortableRelativePathSyntax(e,t,n=e){if(e.startsWith(`/`)||isWindowsDrivePath(e))throw new Error(`${t} must be relative: ${n}`);if(e.includes(`\\`))throw new Error(`${t} must use forward slashes: ${n}`)}function normalizeArchivePath(e,t){const n=e.replace(/\/+$/u,``);if(!n)throw new Error(`${t} is empty.`);assertPortableRelativePathSyntax(n,t,e);if(n.split(`/`).some(e=>e===`.`||e===`..`))throw new Error(`${t} contains path traversal segments: ${e}`);return path.posix.normalize(n)}function normalizeArchiveRoot(e){const t=normalizeArchivePath(e,`Backup manifest archiveRoot`);if(t.includes(`/`))throw new Error(`Backup manifest archiveRoot must be a single path segment: ${e}`);return t}function isArchivePathWithin(e,t){const n=path.posix.relative(t,e);return n===``||!n.startsWith(`../`)&&n!==`..`}function resolvePortableArchivePathKey(e){return e.normalize(`NFC`).toLowerCase()}function findPortableArchiveEntryPathCollision(e){const t=/* @__PURE__ */ new Map;for(const n of e){const e=resolvePortableArchivePathKey(n.normalized);const r=t.get(e);if(r&&r!==n.normalized)return{first:r,second:n.normalized};t.set(e,n.normalized)}}function recordArchiveSymbolicLink(e){if(!e.linkpath||e.linkpath.includes(`\0`))throw new Error(`Archive symbolic link is missing its target: ${e.entryPath}`);const t=normalizeArchivePath(e.entryPath,`Archive symbolic link path`);if(!e.assets.find(({archivePath:e})=>isArchivePathWithin(t,e))||!isArchivePathWithin(t,normalizeArchiveRoot(e.archiveRoot)))throw new Error(`Archive symbolic link is outside the declared backup assets: ${e.entryPath} -> ${e.linkpath}`);if(!e.hasExternalLinkReport){assertPortableRelativePathSyntax(e.linkpath,`Archive symbolic link target`);const n=path.posix.join(path.posix.dirname(t),e.linkpath);if(!e.assets.some(({archivePath:e})=>isArchivePathWithin(n,e)))throw new Error(`Backup manifest external symbolic links do not match archive entries.`)}else if(!e.state)throw new Error(`Backup manifest is missing the symbolic-link state boundary.`);const n=e.platform===`win32`?path.win32:path.posix;const r=n.isAbsolute(e.linkpath);const i=r?n:path.posix;const a=r?n.normalize(e.linkpath):path.posix.join(path.posix.dirname(t),e.platform===`win32`?e.linkpath.replaceAll(`\\`,`/`):e.linkpath);const o=e.state?i.relative(r?e.state.sourcePath:e.state.archivePath,a):void 0;const s=o===void 0?void 0:i.isAbsolute(o)||o===`..`||o.startsWith(`..${i.sep}`);return{entryPath:e.entryPath,linkpath:e.linkpath,external:s}}export{normalizeArchiveRoot as a,resolvePortableArchivePathKey as c,normalizeArchivePath as i,findPortableArchiveEntryPathCollision as n,normalizeWindowsNamespaceAlias as o,isArchivePathWithin as r,recordArchiveSymbolicLink as s,buildBackupArchivePath as t};