From eca242b971c213cbc9735cc280f1ad5209f71371 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 3 Mar 2026 01:43:19 +0000 Subject: [PATCH] refactor(tests): dedupe manifest registry link fixture setup --- src/plugins/manifest-registry.test.ts | 163 +++++++++++++------------- 1 file changed, 79 insertions(+), 84 deletions(-) diff --git a/src/plugins/manifest-registry.test.ts b/src/plugins/manifest-registry.test.ts index 54e0e9c2b5b..9212c6fcf05 100644 --- a/src/plugins/manifest-registry.test.ts +++ b/src/plugins/manifest-registry.test.ts @@ -47,6 +47,74 @@ function countDuplicateWarnings(registry: ReturnType) { + return registry.diagnostics.some((diag) => diag.message.includes("unsafe plugin manifest path")); +} + +function expectUnsafeWorkspaceManifestRejected(params: { + id: string; + mode: "symlink" | "hardlink"; +}) { + const fixture = prepareLinkedManifestFixture({ id: params.id, mode: params.mode }); + if (!fixture.linked) { + return; + } + const registry = loadSingleCandidateRegistry({ + idHint: params.id, + rootDir: fixture.rootDir, + origin: "workspace", + }); + expect(registry.plugins).toHaveLength(0); + expect(hasUnsafeManifestDiagnostic(registry)).toBe(true); +} + afterEach(() => { while (tempDirs.length > 0) { const dir = tempDirs.pop(); @@ -169,104 +237,31 @@ describe("loadPluginManifestRegistry", () => { }); it("rejects manifest paths that escape plugin root via symlink", () => { - const rootDir = makeTempDir(); - const outsideDir = makeTempDir(); - const outsideManifest = path.join(outsideDir, "openclaw.plugin.json"); - const linkedManifest = path.join(rootDir, "openclaw.plugin.json"); - fs.writeFileSync(path.join(rootDir, "index.ts"), "export default function () {}", "utf-8"); - fs.writeFileSync( - outsideManifest, - JSON.stringify({ id: "unsafe-symlink", configSchema: { type: "object" } }), - "utf-8", - ); - try { - fs.symlinkSync(outsideManifest, linkedManifest); - } catch { - return; - } - - const registry = loadRegistry([ - createPluginCandidate({ - idHint: "unsafe-symlink", - rootDir, - origin: "workspace", - }), - ]); - expect(registry.plugins).toHaveLength(0); - expect( - registry.diagnostics.some((diag) => diag.message.includes("unsafe plugin manifest path")), - ).toBe(true); + expectUnsafeWorkspaceManifestRejected({ id: "unsafe-symlink", mode: "symlink" }); }); it("rejects manifest paths that escape plugin root via hardlink", () => { if (process.platform === "win32") { return; } - const rootDir = makeTempDir(); - const outsideDir = makeTempDir(); - const outsideManifest = path.join(outsideDir, "openclaw.plugin.json"); - const linkedManifest = path.join(rootDir, "openclaw.plugin.json"); - fs.writeFileSync(path.join(rootDir, "index.ts"), "export default function () {}", "utf-8"); - fs.writeFileSync( - outsideManifest, - JSON.stringify({ id: "unsafe-hardlink", configSchema: { type: "object" } }), - "utf-8", - ); - try { - fs.linkSync(outsideManifest, linkedManifest); - } catch (err) { - if ((err as NodeJS.ErrnoException).code === "EXDEV") { - return; - } - throw err; - } - - const registry = loadRegistry([ - createPluginCandidate({ - idHint: "unsafe-hardlink", - rootDir, - origin: "workspace", - }), - ]); - expect(registry.plugins).toHaveLength(0); - expect( - registry.diagnostics.some((diag) => diag.message.includes("unsafe plugin manifest path")), - ).toBe(true); + expectUnsafeWorkspaceManifestRejected({ id: "unsafe-hardlink", mode: "hardlink" }); }); it("allows bundled manifest paths that are hardlinked aliases", () => { if (process.platform === "win32") { return; } - const rootDir = makeTempDir(); - const outsideDir = makeTempDir(); - const outsideManifest = path.join(outsideDir, "openclaw.plugin.json"); - const linkedManifest = path.join(rootDir, "openclaw.plugin.json"); - fs.writeFileSync(path.join(rootDir, "index.ts"), "export default function () {}", "utf-8"); - fs.writeFileSync( - outsideManifest, - JSON.stringify({ id: "bundled-hardlink", configSchema: { type: "object" } }), - "utf-8", - ); - try { - fs.linkSync(outsideManifest, linkedManifest); - } catch (err) { - if ((err as NodeJS.ErrnoException).code === "EXDEV") { - return; - } - throw err; + const fixture = prepareLinkedManifestFixture({ id: "bundled-hardlink", mode: "hardlink" }); + if (!fixture.linked) { + return; } - const registry = loadRegistry([ - createPluginCandidate({ - idHint: "bundled-hardlink", - rootDir, - origin: "bundled", - }), - ]); + const registry = loadSingleCandidateRegistry({ + idHint: "bundled-hardlink", + rootDir: fixture.rootDir, + origin: "bundled", + }); expect(registry.plugins.some((entry) => entry.id === "bundled-hardlink")).toBe(true); - expect( - registry.diagnostics.some((diag) => diag.message.includes("unsafe plugin manifest path")), - ).toBe(false); + expect(hasUnsafeManifestDiagnostic(registry)).toBe(false); }); });