openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.96 kB
JavaScript
import{c as resolveUserPath}from"./home-dir-DEN5DisQ.mjs";import"./utils-Dlk0IYAa.mjs";import{t as resolvePathViaExistingAncestorSync}from"./boundary-path-BeuXNL_4.mjs";import{n as resolveActivePluginInstallDir}from"./install-root-context-CqAZmMOW.mjs";import{c as safePathSegmentHashed,l as unscopedPackageName,o as resolveSafeInstallDir,s as safeDirName}from"./install-safe-path-muBIhnx6.mjs";import{lstatSync,realpathSync}from"node:fs";import path from"node:path";import{createHash}from"node:crypto";
//!src/plugins/install-paths.ts
function encodePluginInstallDirName(e){const t=e.trim();if(!t.includes(`/`))return safeDirName(t);return`@${safePathSegmentHashed(t)}`}function validatePluginId(e){const t=e.trim();if(!t)return`invalid plugin name: missing`;if(t.includes(`\\`))return`invalid plugin name: path separators not allowed`;const n=t.split(`/`);if(n.some(e=>!e))return`invalid plugin name: malformed scope`;if(n.some(e=>e===`.`||e===`..`))return`invalid plugin name: reserved path segment`;if(n.length===1){if(t.startsWith(`@`))return`invalid plugin name: scoped ids must use @scope/name format`;return null}if(n.length!==2)return`invalid plugin name: path separators not allowed`;if(!n[0]?.startsWith(`@`)||n[0].length<2)return`invalid plugin name: scoped ids must use @scope/name format`;return null}function matchesExpectedPluginId(e){if(!e.expectedPluginId)return true;if(e.expectedPluginId===e.pluginId)return true;return!e.manifestPluginId&&e.pluginId===e.npmPluginId&&e.expectedPluginId===unscopedPackageName(e.npmPluginId)}function resolveDefaultPluginExtensionsDir(e=process.env,t){return resolveActivePluginInstallDir(`extensions`,e,t)}function resolveDefaultPluginNpmDir(e=process.env,t){return resolveActivePluginInstallDir(`npm`,e,t)}function encodePluginNpmProjectDirName(e){const t=e.trim();if(!t)throw new Error(`invalid npm package name: missing`);return safePathSegmentHashed(t)}function resolvePluginNpmProjectsDir(e){const t=e?resolveUserPath(e):resolveDefaultPluginNpmDir();return path.join(t,`projects`)}function resolvePluginNpmProjectDir(e){return path.join(resolvePluginNpmProjectsDir(e.npmDir),encodePluginNpmProjectDirName(e.packageName))}const PLUGIN_NPM_GENERATION_PROJECT_SEPARATOR=`__openclaw-generation__`;const PLUGIN_NPM_GENERATION_KEY_HASH_CHARS=16;const PLUGIN_NPM_GENERATION_KEY_DIR_NAME_PATTERN=new RegExp(`^g-[a-f0-9]{${PLUGIN_NPM_GENERATION_KEY_HASH_CHARS}}$`,`u`);function resolvePluginNpmGenerationProjectDirPrefix(e){return`${encodePluginNpmProjectDirName(e)}${PLUGIN_NPM_GENERATION_PROJECT_SEPARATOR}`}function isPluginNpmManagedPath(e){const t=path.resolve(e.npmDir??resolveDefaultPluginNpmDir());const n=path.resolve(e.managedPath);try{return lstatSync(t).isDirectory()&&path.relative(t,n)===path.relative(realpathSync(t),e.allowMissing?resolvePathViaExistingAncestorSync(n):realpathSync(n))}catch{return false}}function isPluginNpmProjectDir(e){const t=path.resolve(e.npmDir??resolveDefaultPluginNpmDir());const n=path.resolve(e.projectDir);if(path.dirname(n)!==path.resolve(resolvePluginNpmProjectsDir(t)))return false;if(!isPluginNpmManagedPath({managedPath:n,npmDir:t}))return false;if(!isPluginNpmManagedPath({managedPath:path.join(n,`node_modules`,...e.packageName.split(`/`)),npmDir:t,allowMissing:true}))return false;if(n===path.resolve(resolvePluginNpmProjectDir(e)))return true;const r=path.basename(n);const i=resolvePluginNpmGenerationProjectDirPrefix(e.packageName);return r.startsWith(i)&&PLUGIN_NPM_GENERATION_KEY_DIR_NAME_PATTERN.test(r.slice(i.length))}function encodePluginNpmGenerationKeyDirName(e){return`g-${createHash(`sha256`).update(e).digest(`hex`).slice(0,PLUGIN_NPM_GENERATION_KEY_HASH_CHARS)}`}function resolvePluginNpmGenerationProjectDir(e){return path.join(resolvePluginNpmProjectsDir(e.npmDir),`${resolvePluginNpmGenerationProjectDirPrefix(e.packageName)}${encodePluginNpmGenerationKeyDirName(e.generationKey)}`)}function resolvePluginNpmPackageDir(e){return path.join(resolvePluginNpmProjectDir(e),`node_modules`,...e.packageName.split(`/`))}function resolveDefaultPluginGitDir(e=process.env,t){return resolveActivePluginInstallDir(`git`,e,t)}function resolvePluginInstallDir(e,t){const n=t?resolveUserPath(t):resolveDefaultPluginExtensionsDir();const r=validatePluginId(e);if(r)throw new Error(r);const i=resolveSafeInstallDir({baseDir:n,id:e,invalidNameMessage:`invalid plugin name: path traversal detected`,nameEncoder:encodePluginInstallDirName});if(!i.ok)throw new Error(i.error);return i.path}export{resolveDefaultPluginExtensionsDir as a,resolvePluginInstallDir as c,resolvePluginNpmPackageDir as d,resolvePluginNpmProjectDir as f,matchesExpectedPluginId as i,resolvePluginNpmGenerationProjectDir as l,validatePluginId as m,isPluginNpmManagedPath as n,resolveDefaultPluginGitDir as o,resolvePluginNpmProjectsDir as p,isPluginNpmProjectDir as r,resolveDefaultPluginNpmDir as s,encodePluginInstallDirName as t,resolvePluginNpmGenerationProjectDirPrefix as u};