Skip to content

Commit e7114c0

Browse files
author
Peter Steinberger
committed
fix(plugins): resolve marketplace aliases without recursive cycles
1 parent fb9c8e7 commit e7114c0

2 files changed

Lines changed: 185 additions & 11 deletions

File tree

‎src/plugins/marketplace.test.ts‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from "node:fs/promises";
33
import os from "node:os";
44
import path from "node:path";
55
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
6+
import * as jsonFiles from "../infra/json-files.js";
67
import { withEnvAsync } from "../test-utils/env.js";
78
import { withTempDir } from "../test-utils/temp-dir.js";
89
import {
@@ -102,6 +103,22 @@ async function writeLocalMarketplaceFixture(params: {
102103
return writeMarketplaceManifest(params.rootDir, params.manifest);
103104
}
104105

106+
async function withKnownMarketplaceRegistry<T>(
107+
marketplaces: Record<string, unknown>,
108+
run: (homeDir: string) => Promise<T>,
109+
): Promise<T> {
110+
return await withTempDir("openclaw-marketplace-known-", async (homeDir) => {
111+
const openClawHome = path.join(homeDir, "openclaw-home");
112+
const registryPath = path.join(homeDir, ".claude", "plugins", "known_marketplaces.json");
113+
await fs.mkdir(path.dirname(registryPath), { recursive: true });
114+
await fs.mkdir(openClawHome, { recursive: true });
115+
await fs.writeFile(registryPath, JSON.stringify(marketplaces));
116+
return await withEnvAsync({ HOME: homeDir, OPENCLAW_HOME: openClawHome }, async () =>
117+
run(homeDir),
118+
);
119+
});
120+
}
121+
105122
function mockRemoteMarketplaceClone(params: {
106123
manifest: unknown;
107124
pluginDir?: string;
@@ -533,6 +550,151 @@ describe("marketplace plugins", () => {
533550
});
534551
});
535552

553+
const cyclicKnownMarketplaces = [
554+
{
555+
label: "self-referential",
556+
marketplace: "loop",
557+
marketplaces: {
558+
loop: { source: { source: "path", path: "loop" } },
559+
},
560+
cycle: "loop -> loop",
561+
},
562+
{
563+
label: "two-marketplace",
564+
marketplace: "alpha",
565+
marketplaces: {
566+
alpha: { source: { source: "path", path: "beta" } },
567+
beta: { source: { source: "path", path: "alpha" } },
568+
},
569+
cycle: "alpha -> beta -> alpha",
570+
},
571+
] as const;
572+
573+
it.each(cyclicKnownMarketplaces)(
574+
"rejects a $label known marketplace alias cycle while listing",
575+
async ({ marketplace, marketplaces, cycle }) => {
576+
await withKnownMarketplaceRegistry(marketplaces, async () => {
577+
const registryRead = vi.spyOn(jsonFiles, "tryReadJson");
578+
for (let read = 0; read <= Object.keys(marketplaces).length; read += 1) {
579+
registryRead.mockResolvedValueOnce(marketplaces);
580+
}
581+
// Bound the unfixed recursive loader so the regression never leaves a runaway task.
582+
registryRead.mockResolvedValueOnce({});
583+
584+
try {
585+
const result = await listMarketplacePlugins({ marketplace });
586+
587+
expect(result).toEqual({
588+
ok: false,
589+
error: `known marketplace source cycle: ${cycle}`,
590+
});
591+
expect(registryRead).toHaveBeenCalledTimes(1);
592+
expect(runCommandWithTimeoutMock).not.toHaveBeenCalled();
593+
expect(fetchWithSsrFGuardMock).not.toHaveBeenCalled();
594+
} finally {
595+
registryRead.mockRestore();
596+
}
597+
});
598+
},
599+
);
600+
601+
it.each(cyclicKnownMarketplaces)(
602+
"rejects a $label known marketplace alias cycle before installing",
603+
async ({ marketplace, marketplaces, cycle }) => {
604+
await withKnownMarketplaceRegistry(marketplaces, async () => {
605+
const registryRead = vi.spyOn(jsonFiles, "tryReadJson");
606+
for (let read = 0; read <= Object.keys(marketplaces).length; read += 1) {
607+
registryRead.mockResolvedValueOnce(marketplaces);
608+
}
609+
// Bound the unfixed recursive loader so the lifecycle-owning call always settles.
610+
registryRead.mockResolvedValueOnce({});
611+
612+
try {
613+
const result = await installPluginFromMarketplace({
614+
marketplace,
615+
plugin: "frontend-design",
616+
});
617+
618+
expect(result).toEqual({
619+
ok: false,
620+
error: `known marketplace source cycle: ${cycle}`,
621+
});
622+
expect(registryRead).toHaveBeenCalledTimes(1);
623+
expect(installPluginFromPathMock).not.toHaveBeenCalled();
624+
expect(runCommandWithTimeoutMock).not.toHaveBeenCalled();
625+
expect(fetchWithSsrFGuardMock).not.toHaveBeenCalled();
626+
} finally {
627+
registryRead.mockRestore();
628+
}
629+
});
630+
},
631+
);
632+
633+
it("resolves independent known marketplace alias calls from one registry snapshot each", async () => {
634+
await withKnownMarketplaceRegistry({}, async (homeDir) => {
635+
const marketplaceRoot = path.join(homeDir, "known-marketplace");
636+
const pluginDir = path.join(marketplaceRoot, "plugins", "frontend-design");
637+
await writeLocalMarketplaceFixture({
638+
rootDir: marketplaceRoot,
639+
pluginDir,
640+
manifest: {
641+
plugins: [{ name: "frontend-design", source: "./plugins/frontend-design" }],
642+
},
643+
});
644+
await fs.writeFile(
645+
path.join(homeDir, ".claude", "plugins", "known_marketplaces.json"),
646+
JSON.stringify({
647+
alpha: { source: { source: "path", path: "beta" } },
648+
beta: {
649+
installLocation: marketplaceRoot,
650+
source: { source: "path", path: "alpha" },
651+
},
652+
}),
653+
);
654+
installPluginFromPathMock.mockResolvedValue({
655+
ok: true,
656+
pluginId: "frontend-design",
657+
targetDir: "/tmp/frontend-design",
658+
version: "0.1.0",
659+
extensions: ["index.ts"],
660+
});
661+
const registryRead = vi.spyOn(jsonFiles, "tryReadJson");
662+
663+
try {
664+
const listed = await listMarketplacePlugins({ marketplace: "alpha" });
665+
const installed = await installPluginFromMarketplace({
666+
marketplace: "alpha",
667+
plugin: "frontend-design",
668+
});
669+
670+
expect(listed).toMatchObject({ ok: true, sourceLabel: "beta" });
671+
expectLocalMarketplaceInstallResult({
672+
result: installed,
673+
pluginDir,
674+
marketplaceSource: "alpha",
675+
});
676+
expect(registryRead).toHaveBeenCalledTimes(2);
677+
expect(runCommandWithTimeoutMock).not.toHaveBeenCalled();
678+
expect(fetchWithSsrFGuardMock).not.toHaveBeenCalled();
679+
} finally {
680+
registryRead.mockRestore();
681+
}
682+
});
683+
});
684+
685+
it("preserves ordinary source fallback for an invalid known marketplace alias", async () => {
686+
await withKnownMarketplaceRegistry({ broken: { source: { source: "path" } } }, async () => {
687+
const result = await listMarketplacePlugins({ marketplace: "broken" });
688+
689+
expect(result).toEqual({
690+
ok: false,
691+
error: "unsupported marketplace source: broken",
692+
});
693+
expect(runCommandWithTimeoutMock).not.toHaveBeenCalled();
694+
expect(fetchWithSsrFGuardMock).not.toHaveBeenCalled();
695+
});
696+
});
697+
536698
it("installs remote marketplace plugins from relative paths inside the cloned repo", async () => {
537699
mockRemoteMarketplaceClone({
538700
pluginDir: path.join("plugins", "frontend-design"),

‎src/plugins/marketplace.ts‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -672,27 +672,39 @@ async function loadMarketplace(params: {
672672
return undefined;
673673
};
674674

675+
// Resolve aliases against one snapshot so a cycle cannot retain a plugin lifecycle lease.
675676
const knownMarketplaces = await readClaudeKnownMarketplaces();
676-
const known = knownMarketplaces[params.source];
677-
if (known) {
677+
const visitedKnownMarketplaces = new Set<string>();
678+
let source = params.source;
679+
680+
while (true) {
681+
const known = knownMarketplaces[source];
682+
if (!known) {
683+
break;
684+
}
685+
if (visitedKnownMarketplaces.has(source)) {
686+
return {
687+
ok: false,
688+
error: `known marketplace source cycle: ${[...visitedKnownMarketplaces, source].join(" -> ")}`,
689+
};
690+
}
691+
visitedKnownMarketplaces.add(source);
692+
678693
if (known.installLocation) {
679694
const local = await resolveLocalMarketplaceSource(known.installLocation);
680695
if (local?.ok) {
681-
return await loadResolvedLocalMarketplace(local, params.source);
696+
return await loadResolvedLocalMarketplace(local, source);
682697
}
683698
}
684699

685700
const normalizedSource = normalizeEntrySource(known.source);
686-
if (normalizedSource.ok) {
687-
return await loadMarketplace({
688-
source: marketplaceEntrySourceToInput(normalizedSource.source),
689-
logger: params.logger,
690-
timeoutMs: params.timeoutMs,
691-
});
701+
if (!normalizedSource.ok) {
702+
break;
692703
}
704+
source = marketplaceEntrySourceToInput(normalizedSource.source);
693705
}
694706

695-
const local = await resolveLocalMarketplaceSource(params.source);
707+
const local = await resolveLocalMarketplaceSource(source);
696708
if (local?.ok === false) {
697709
return local;
698710
}
@@ -702,7 +714,7 @@ async function loadMarketplace(params: {
702714
}
703715

704716
const cloned = await cloneMarketplaceRepo({
705-
source: params.source,
717+
source,
706718
timeoutMs: params.timeoutMs,
707719
logger: params.logger,
708720
});

0 commit comments

Comments
 (0)