|
1 | | -import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 5 |
|
3 | 6 | const runInstallPolicyMock = vi.fn(); |
4 | 7 | const findBlockedManifestDependenciesMock = vi.fn(); |
@@ -42,8 +45,15 @@ const { |
42 | 45 | preflightPluginNpmInstallPolicyRuntime, |
43 | 46 | scanBundleInstallSourceRuntime, |
44 | 47 | scanFileInstallSourceRuntime, |
| 48 | + scanInstalledPackageDependencyTreeRuntime, |
45 | 49 | } = await import("./install-security-scan.runtime.js"); |
46 | 50 |
|
| 51 | +const tempDirs: string[] = []; |
| 52 | + |
| 53 | +afterEach(async () => { |
| 54 | + await Promise.all(tempDirs.splice(0).map((dir) => fs.rm(dir, { force: true, recursive: true }))); |
| 55 | +}); |
| 56 | + |
47 | 57 | function expectOnlyOperatorPolicyRan() { |
48 | 58 | expect(runInstallPolicyMock).toHaveBeenCalledTimes(1); |
49 | 59 | expect(findBlockedManifestDependenciesMock).not.toHaveBeenCalled(); |
@@ -176,6 +186,67 @@ describe("install security scan official bypass", () => { |
176 | 186 | }); |
177 | 187 | }); |
178 | 188 |
|
| 189 | +describe("installed dependency tree scan", () => { |
| 190 | + it("accepts a managed host link declared as a runtime dependency", async () => { |
| 191 | + findBlockedManifestDependenciesMock.mockReturnValue([]); |
| 192 | + const npmRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-install-scan-")); |
| 193 | + tempDirs.push(npmRoot); |
| 194 | + const packageDir = path.join(npmRoot, "node_modules", "runtime-plugin"); |
| 195 | + const hostLink = path.join(packageDir, "node_modules", "openclaw"); |
| 196 | + await fs.mkdir(path.dirname(hostLink), { recursive: true }); |
| 197 | + await fs.writeFile( |
| 198 | + path.join(packageDir, "package.json"), |
| 199 | + JSON.stringify({ |
| 200 | + name: "runtime-plugin", |
| 201 | + dependencies: { openclaw: "2026.7.1" }, |
| 202 | + }), |
| 203 | + "utf8", |
| 204 | + ); |
| 205 | + await fs.symlink(process.cwd(), hostLink, "junction"); |
| 206 | + |
| 207 | + const result = await scanInstalledPackageDependencyTreeRuntime({ |
| 208 | + allowManagedNpmRootPackagePeerSymlinks: true, |
| 209 | + dependencyScanRootDir: npmRoot, |
| 210 | + logger: {}, |
| 211 | + packageDir, |
| 212 | + pluginId: "runtime-plugin", |
| 213 | + }); |
| 214 | + |
| 215 | + expect(result).toBeUndefined(); |
| 216 | + expect(runInstallPolicyMock).toHaveBeenCalledTimes(1); |
| 217 | + }); |
| 218 | + |
| 219 | + it("rejects an openclaw dependency symlink that does not target the trusted host", async () => { |
| 220 | + findBlockedManifestDependenciesMock.mockReturnValue([]); |
| 221 | + const npmRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-install-scan-")); |
| 222 | + const outsideRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-install-outside-")); |
| 223 | + tempDirs.push(npmRoot, outsideRoot); |
| 224 | + const packageDir = path.join(npmRoot, "node_modules", "runtime-plugin"); |
| 225 | + const hostLink = path.join(packageDir, "node_modules", "openclaw"); |
| 226 | + await fs.mkdir(path.dirname(hostLink), { recursive: true }); |
| 227 | + await fs.writeFile( |
| 228 | + path.join(packageDir, "package.json"), |
| 229 | + JSON.stringify({ |
| 230 | + name: "runtime-plugin", |
| 231 | + dependencies: { openclaw: "2026.7.1" }, |
| 232 | + }), |
| 233 | + "utf8", |
| 234 | + ); |
| 235 | + await fs.writeFile(path.join(outsideRoot, "package.json"), '{"name":"openclaw"}', "utf8"); |
| 236 | + await fs.symlink(outsideRoot, hostLink, "junction"); |
| 237 | + |
| 238 | + await expect( |
| 239 | + scanInstalledPackageDependencyTreeRuntime({ |
| 240 | + allowManagedNpmRootPackagePeerSymlinks: true, |
| 241 | + dependencyScanRootDir: npmRoot, |
| 242 | + logger: {}, |
| 243 | + packageDir, |
| 244 | + pluginId: "runtime-plugin", |
| 245 | + }), |
| 246 | + ).rejects.toThrow("installed dependency scan found package outside install root"); |
| 247 | + }); |
| 248 | +}); |
| 249 | + |
179 | 250 | describe("legacy file install scan compatibility", () => { |
180 | 251 | it("preserves policy and hook metadata for published lazy install chunks", async () => { |
181 | 252 | const warnings: string[] = []; |
|
0 commit comments