|
| 1 | +import path from "node:path"; |
| 2 | +import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce"; |
| 3 | +import type { OpenClawConfig } from "../config/types.openclaw.js"; |
| 4 | +import { packageNameMatchesId } from "../infra/install-safe-path.js"; |
| 5 | +import type { InstallPolicySource } from "../security/install-policy.js"; |
| 6 | +import { matchesExpectedPluginId, validatePluginId } from "./install-paths.js"; |
| 7 | +import { |
| 8 | + buildDirectoryInstallResult, |
| 9 | + defaultLogger, |
| 10 | + emitSuccessfulPluginInstallSecurityEvent, |
| 11 | + ensureOpenClawExtensions, |
| 12 | + formatUnresolvedOpenClawPeerLinkError, |
| 13 | + hasPackageRuntimeDependencies, |
| 14 | + loadPluginInstallRuntime, |
| 15 | + runInstallSourceScan, |
| 16 | + sourceFamilyForInstallPolicyKind, |
| 17 | + sourceFamilyForInstallPolicySource, |
| 18 | + validateOpenClawPackageInstallCompatibility, |
| 19 | +} from "./install-shared.js"; |
| 20 | +import { |
| 21 | + PLUGIN_INSTALL_ERROR_CODE, |
| 22 | + type InstallPluginResult, |
| 23 | + type PackageInstallCommonParams, |
| 24 | + type PackageManifest, |
| 25 | + type PluginInstallFailureResult, |
| 26 | + type PluginInstallLogger, |
| 27 | + type PluginInstallPolicyRequest, |
| 28 | +} from "./install-types.js"; |
| 29 | +import { validatePackageExtensionEntriesForInstall } from "./package-entry-resolution.js"; |
| 30 | +import { linkOpenClawPeerDependencies } from "./plugin-peer-link.js"; |
| 31 | + |
| 32 | +type ValidatedPackagePlugin = { |
| 33 | + manifest: PackageManifest; |
| 34 | + pluginId: string; |
| 35 | + manifestName?: string; |
| 36 | + version?: string; |
| 37 | + extensions: string[]; |
| 38 | + hasRuntimeDependencies: boolean; |
| 39 | + peerDependencies: Record<string, string>; |
| 40 | +}; |
| 41 | + |
| 42 | +export async function validatePackagePluginInstallSource(params: { |
| 43 | + runtime: Awaited<ReturnType<typeof loadPluginInstallRuntime>>; |
| 44 | + packageDir: string; |
| 45 | + expectedPluginId?: string; |
| 46 | + requirePluginManifest?: boolean; |
| 47 | + allowSourceTypeScriptEntries?: boolean; |
| 48 | + dangerouslyForceUnsafeInstall?: boolean; |
| 49 | + trustedSourceLinkedOfficialInstall?: boolean; |
| 50 | + config?: OpenClawConfig; |
| 51 | + installPolicyRequest?: PluginInstallPolicyRequest; |
| 52 | + logger: PluginInstallLogger; |
| 53 | + mode: "install" | "update"; |
| 54 | + resolveEffectiveMode?: (pluginId: string) => Promise<"install" | "update">; |
| 55 | +}): Promise< |
| 56 | + | { |
| 57 | + ok: true; |
| 58 | + plugin: ValidatedPackagePlugin; |
| 59 | + } |
| 60 | + | PluginInstallFailureResult |
| 61 | +> { |
| 62 | + const manifestPath = path.join(params.packageDir, "package.json"); |
| 63 | + if (!(await params.runtime.fileExists(manifestPath))) { |
| 64 | + return { ok: false, error: "extracted package missing package.json" }; |
| 65 | + } |
| 66 | + |
| 67 | + let manifest: PackageManifest; |
| 68 | + try { |
| 69 | + manifest = await params.runtime.readJsonFile<PackageManifest>(manifestPath); |
| 70 | + } catch (err) { |
| 71 | + return { ok: false, error: `invalid package.json: ${String(err)}` }; |
| 72 | + } |
| 73 | + |
| 74 | + const pkgName = normalizeOptionalString(manifest.name) ?? ""; |
| 75 | + const npmPluginId = pkgName || "plugin"; |
| 76 | + const ocManifestResult = params.runtime.loadPluginManifest(params.packageDir); |
| 77 | + if (!ocManifestResult.ok && params.requirePluginManifest) { |
| 78 | + return { |
| 79 | + ok: false, |
| 80 | + error: `package missing valid openclaw.plugin.json: ${ocManifestResult.error}`, |
| 81 | + code: PLUGIN_INSTALL_ERROR_CODE.MISSING_PLUGIN_MANIFEST, |
| 82 | + }; |
| 83 | + } |
| 84 | + const manifestPluginId = |
| 85 | + ocManifestResult.ok && ocManifestResult.manifest.id |
| 86 | + ? ocManifestResult.manifest.id.trim() |
| 87 | + : undefined; |
| 88 | + |
| 89 | + const pluginId = manifestPluginId ?? npmPluginId; |
| 90 | + const pluginIdError = validatePluginId(pluginId); |
| 91 | + if (pluginIdError) { |
| 92 | + return { ok: false, error: pluginIdError }; |
| 93 | + } |
| 94 | + if ( |
| 95 | + !matchesExpectedPluginId({ |
| 96 | + expectedPluginId: params.expectedPluginId, |
| 97 | + pluginId, |
| 98 | + manifestPluginId, |
| 99 | + npmPluginId, |
| 100 | + }) |
| 101 | + ) { |
| 102 | + return { |
| 103 | + ok: false, |
| 104 | + error: `plugin id mismatch: expected ${params.expectedPluginId}, got ${pluginId}`, |
| 105 | + code: PLUGIN_INSTALL_ERROR_CODE.PLUGIN_ID_MISMATCH, |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + if (manifestPluginId && !packageNameMatchesId(npmPluginId, manifestPluginId)) { |
| 110 | + params.logger.info?.( |
| 111 | + `Plugin manifest id "${manifestPluginId}" differs from npm package name "${npmPluginId}"; using manifest id as the config key.`, |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + const packageMetadata = params.runtime.getPackageManifestMetadata(manifest); |
| 116 | + const compatibilityError = validateOpenClawPackageInstallCompatibility({ |
| 117 | + runtime: params.runtime, |
| 118 | + pluginId, |
| 119 | + packageMetadata, |
| 120 | + }); |
| 121 | + if (compatibilityError) { |
| 122 | + return compatibilityError; |
| 123 | + } |
| 124 | + |
| 125 | + const extensionsResult = ensureOpenClawExtensions({ |
| 126 | + manifest, |
| 127 | + }); |
| 128 | + if (!extensionsResult.ok) { |
| 129 | + return { |
| 130 | + ok: false, |
| 131 | + error: extensionsResult.error, |
| 132 | + code: extensionsResult.code, |
| 133 | + }; |
| 134 | + } |
| 135 | + const extensions = extensionsResult.entries; |
| 136 | + |
| 137 | + const extensionValidation = await validatePackageExtensionEntriesForInstall({ |
| 138 | + packageDir: params.packageDir, |
| 139 | + extensions, |
| 140 | + manifest, |
| 141 | + allowSourceTypeScriptEntries: params.allowSourceTypeScriptEntries, |
| 142 | + }); |
| 143 | + if (!extensionValidation.ok) { |
| 144 | + return { |
| 145 | + ok: false, |
| 146 | + error: extensionValidation.error, |
| 147 | + code: PLUGIN_INSTALL_ERROR_CODE.INVALID_OPENCLAW_EXTENSIONS, |
| 148 | + }; |
| 149 | + } |
| 150 | + |
| 151 | + const scanMode = params.resolveEffectiveMode |
| 152 | + ? await params.resolveEffectiveMode(pluginId) |
| 153 | + : params.mode; |
| 154 | + const scanResult = await runInstallSourceScan({ |
| 155 | + subject: `Plugin "${pluginId}"`, |
| 156 | + pluginId, |
| 157 | + mode: scanMode, |
| 158 | + sourceFamily: sourceFamilyForInstallPolicySource( |
| 159 | + params.installPolicyRequest?.source, |
| 160 | + sourceFamilyForInstallPolicyKind(params.installPolicyRequest?.kind, "installed-package"), |
| 161 | + ), |
| 162 | + scan: async () => |
| 163 | + await params.runtime.scanPackageInstallSource({ |
| 164 | + dangerouslyForceUnsafeInstall: params.dangerouslyForceUnsafeInstall, |
| 165 | + trustedSourceLinkedOfficialInstall: params.trustedSourceLinkedOfficialInstall, |
| 166 | + packageDir: params.packageDir, |
| 167 | + config: params.config, |
| 168 | + pluginId, |
| 169 | + logger: params.logger, |
| 170 | + extensions, |
| 171 | + ...(packageMetadata ? { packageMetadata } : {}), |
| 172 | + requestKind: params.installPolicyRequest?.kind, |
| 173 | + requestedSpecifier: params.installPolicyRequest?.requestedSpecifier, |
| 174 | + source: params.installPolicyRequest?.source, |
| 175 | + mode: scanMode, |
| 176 | + packageName: pkgName || undefined, |
| 177 | + manifestId: manifestPluginId, |
| 178 | + version: typeof manifest.version === "string" ? manifest.version : undefined, |
| 179 | + }), |
| 180 | + }); |
| 181 | + if (scanResult) { |
| 182 | + return scanResult; |
| 183 | + } |
| 184 | + |
| 185 | + return { |
| 186 | + ok: true, |
| 187 | + plugin: { |
| 188 | + manifest, |
| 189 | + pluginId, |
| 190 | + manifestName: pkgName || undefined, |
| 191 | + version: typeof manifest.version === "string" ? manifest.version : undefined, |
| 192 | + extensions, |
| 193 | + hasRuntimeDependencies: hasPackageRuntimeDependencies(manifest), |
| 194 | + peerDependencies: manifest.peerDependencies ?? {}, |
| 195 | + }, |
| 196 | + }; |
| 197 | +} |
| 198 | + |
| 199 | +export async function scanAndLinkInstalledPackage(params: { |
| 200 | + runtime: Awaited<ReturnType<typeof loadPluginInstallRuntime>>; |
| 201 | + installedDir: string; |
| 202 | + additionalDependencyPackageDirs?: string[]; |
| 203 | + dependencyScanRootDir?: string; |
| 204 | + pluginId: string; |
| 205 | + peerDependencies: Record<string, string>; |
| 206 | + dangerouslyForceUnsafeInstall?: boolean; |
| 207 | + trustedSourceLinkedOfficialInstall?: boolean; |
| 208 | + mode?: "install" | "update"; |
| 209 | + requestKind?: PluginInstallPolicyRequest["kind"]; |
| 210 | + requestedSpecifier?: string; |
| 211 | + config?: OpenClawConfig; |
| 212 | + source?: InstallPolicySource; |
| 213 | + logger: PluginInstallLogger; |
| 214 | +}): Promise<Extract<InstallPluginResult, { ok: false }> | null> { |
| 215 | + const scanResult = await runInstallSourceScan({ |
| 216 | + subject: `Plugin "${params.pluginId}"`, |
| 217 | + pluginId: params.pluginId, |
| 218 | + mode: params.mode, |
| 219 | + sourceFamily: sourceFamilyForInstallPolicySource( |
| 220 | + params.source, |
| 221 | + sourceFamilyForInstallPolicyKind(params.requestKind, "installed-package"), |
| 222 | + ), |
| 223 | + scan: async () => |
| 224 | + await params.runtime.scanInstalledPackageDependencyTree({ |
| 225 | + ...(params.additionalDependencyPackageDirs |
| 226 | + ? { additionalPackageDirs: params.additionalDependencyPackageDirs } |
| 227 | + : {}), |
| 228 | + allowManagedNpmRootPackagePeerSymlinks: |
| 229 | + params.dependencyScanRootDir !== undefined && |
| 230 | + path.resolve(params.dependencyScanRootDir) !== path.resolve(params.installedDir), |
| 231 | + dangerouslyForceUnsafeInstall: params.dangerouslyForceUnsafeInstall, |
| 232 | + dependencyScanRootDir: params.dependencyScanRootDir, |
| 233 | + logger: params.logger, |
| 234 | + mode: params.mode, |
| 235 | + packageDir: params.installedDir, |
| 236 | + pluginId: params.pluginId, |
| 237 | + config: params.config, |
| 238 | + ...(params.requestKind ? { requestKind: params.requestKind } : {}), |
| 239 | + requestedSpecifier: params.requestedSpecifier, |
| 240 | + source: params.source, |
| 241 | + trustedSourceLinkedOfficialInstall: params.trustedSourceLinkedOfficialInstall, |
| 242 | + }), |
| 243 | + }); |
| 244 | + if (scanResult) { |
| 245 | + return scanResult; |
| 246 | + } |
| 247 | + const peerLinkRepair = await linkOpenClawPeerDependencies({ |
| 248 | + installedDir: params.installedDir, |
| 249 | + peerDependencies: params.peerDependencies, |
| 250 | + logger: params.logger, |
| 251 | + }); |
| 252 | + if (peerLinkRepair.skipped > 0) { |
| 253 | + return { |
| 254 | + ok: false, |
| 255 | + error: formatUnresolvedOpenClawPeerLinkError(params.pluginId), |
| 256 | + }; |
| 257 | + } |
| 258 | + return null; |
| 259 | +} |
| 260 | + |
| 261 | +export async function installPluginFromInstalledPackageDir( |
| 262 | + params: { |
| 263 | + additionalDependencyPackageDirs?: string[]; |
| 264 | + emitSuccessSecurityEvent?: boolean; |
| 265 | + packageDir: string; |
| 266 | + dependencyScanRootDir?: string; |
| 267 | + } & PackageInstallCommonParams, |
| 268 | +): Promise<InstallPluginResult> { |
| 269 | + return await installPluginFromInstalledPackageDirInternal(params); |
| 270 | +} |
| 271 | + |
| 272 | +async function installPluginFromInstalledPackageDirInternal( |
| 273 | + params: { |
| 274 | + additionalDependencyPackageDirs?: string[]; |
| 275 | + emitSuccessSecurityEvent?: boolean; |
| 276 | + packageDir: string; |
| 277 | + dependencyScanRootDir?: string; |
| 278 | + } & PackageInstallCommonParams, |
| 279 | +): Promise<InstallPluginResult> { |
| 280 | + const runtime = await loadPluginInstallRuntime(); |
| 281 | + const { logger } = runtime.resolveTimedInstallModeOptions(params, defaultLogger); |
| 282 | + const validated = await validatePackagePluginInstallSource({ |
| 283 | + runtime, |
| 284 | + packageDir: params.packageDir, |
| 285 | + expectedPluginId: params.expectedPluginId, |
| 286 | + requirePluginManifest: params.requirePluginManifest, |
| 287 | + allowSourceTypeScriptEntries: params.allowSourceTypeScriptEntries, |
| 288 | + dangerouslyForceUnsafeInstall: params.dangerouslyForceUnsafeInstall, |
| 289 | + trustedSourceLinkedOfficialInstall: params.trustedSourceLinkedOfficialInstall, |
| 290 | + config: params.config, |
| 291 | + installPolicyRequest: params.installPolicyRequest, |
| 292 | + logger, |
| 293 | + mode: params.mode ?? "install", |
| 294 | + }); |
| 295 | + if (!validated.ok) { |
| 296 | + return validated; |
| 297 | + } |
| 298 | + const postInstallError = await scanAndLinkInstalledPackage({ |
| 299 | + runtime, |
| 300 | + installedDir: params.packageDir, |
| 301 | + ...(params.additionalDependencyPackageDirs |
| 302 | + ? { additionalDependencyPackageDirs: params.additionalDependencyPackageDirs } |
| 303 | + : {}), |
| 304 | + dependencyScanRootDir: params.dependencyScanRootDir, |
| 305 | + pluginId: validated.plugin.pluginId, |
| 306 | + peerDependencies: validated.plugin.peerDependencies, |
| 307 | + dangerouslyForceUnsafeInstall: params.dangerouslyForceUnsafeInstall, |
| 308 | + trustedSourceLinkedOfficialInstall: params.trustedSourceLinkedOfficialInstall, |
| 309 | + config: params.config, |
| 310 | + mode: params.mode ?? "install", |
| 311 | + ...(params.installPolicyRequest?.kind ? { requestKind: params.installPolicyRequest.kind } : {}), |
| 312 | + requestedSpecifier: params.installPolicyRequest?.requestedSpecifier, |
| 313 | + source: params.installPolicyRequest?.source, |
| 314 | + logger, |
| 315 | + }); |
| 316 | + if (postInstallError) { |
| 317 | + return postInstallError; |
| 318 | + } |
| 319 | + const result = buildDirectoryInstallResult({ |
| 320 | + pluginId: validated.plugin.pluginId, |
| 321 | + targetDir: params.packageDir, |
| 322 | + manifestName: validated.plugin.manifestName, |
| 323 | + version: validated.plugin.version, |
| 324 | + extensions: validated.plugin.extensions, |
| 325 | + }); |
| 326 | + if (params.emitSuccessSecurityEvent !== false) { |
| 327 | + emitSuccessfulPluginInstallSecurityEvent(result, { |
| 328 | + dryRun: params.dryRun, |
| 329 | + mode: params.mode ?? "install", |
| 330 | + sourceFamily: sourceFamilyForInstallPolicyKind( |
| 331 | + params.installPolicyRequest?.kind, |
| 332 | + "installed-package", |
| 333 | + ), |
| 334 | + trustedSourceLinkedOfficialInstall: params.trustedSourceLinkedOfficialInstall, |
| 335 | + }); |
| 336 | + } |
| 337 | + return result; |
| 338 | +} |
0 commit comments