|
1 | 1 | import { describe, expect, test } from "bun:test"; |
2 | 2 | import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | | -import { join } from "node:path"; |
| 4 | +import { isAbsolute, join } from "node:path"; |
| 5 | +import type { GlobalSettingsWriter } from "../../src/mcp/add-server.js"; |
| 6 | +import type { ProjectTrustStore } from "../../src/trust/project-trust.js"; |
| 7 | +import { withMockedModule } from "../helpers/mock-module.js"; |
5 | 8 | import { |
6 | 9 | dedupePluginModules, |
7 | 10 | discoverUserPlugins, |
@@ -305,3 +308,213 @@ describe("path plugin trust across working directories", () => { |
305 | 308 | } |
306 | 309 | }); |
307 | 310 | }); |
| 311 | + |
| 312 | +// The /plugins add-by-path ordering probe (CL-8991): `trustPathPlugins` is |
| 313 | +// mocked once for this file so each `addPath` below can observe whether any |
| 314 | +// plugin code ran before the grant resolved. The wrapper delegates to the |
| 315 | +// real store with an explicit home, so behavior — including the existing |
| 316 | +// tests above, which always pass their own home — is unchanged. |
| 317 | +const addPathProbe = { |
| 318 | + home: "", |
| 319 | + marker: "", |
| 320 | + trustCalls: [] as string[][], |
| 321 | + markerAtTrust: [] as boolean[], |
| 322 | +}; |
| 323 | + |
| 324 | +function resetAddPathProbe(home: string, marker: string): void { |
| 325 | + addPathProbe.home = home; |
| 326 | + addPathProbe.marker = marker; |
| 327 | + addPathProbe.trustCalls = []; |
| 328 | + addPathProbe.markerAtTrust = []; |
| 329 | +} |
| 330 | + |
| 331 | +await withMockedModule( |
| 332 | + import.meta.resolve("../../src/trust/path-trust.js"), |
| 333 | + (real: typeof import("../../src/trust/path-trust.js")) => ({ |
| 334 | + ...real, |
| 335 | + trustPathPlugins: async (paths: string[], home?: string) => { |
| 336 | + addPathProbe.trustCalls.push([...paths]); |
| 337 | + addPathProbe.markerAtTrust.push( |
| 338 | + await Bun.file(addPathProbe.marker).exists(), |
| 339 | + ); |
| 340 | + return real.trustPathPlugins(paths, home ?? addPathProbe.home); |
| 341 | + }, |
| 342 | + }), |
| 343 | +); |
| 344 | + |
| 345 | +function stubSettingsWriter(): GlobalSettingsWriter { |
| 346 | + return { |
| 347 | + enqueue: async <T>(job: () => Promise<T>): Promise<T> => job(), |
| 348 | + update: async () => null, |
| 349 | + updateAt: async () => null, |
| 350 | + mutate: async () => "ok" as const, |
| 351 | + mutateAt: async () => "ok" as const, |
| 352 | + }; |
| 353 | +} |
| 354 | + |
| 355 | +async function makeAddPathAdmin(base: string): Promise<{ |
| 356 | + addPath: ( |
| 357 | + path: string, |
| 358 | + ) => Promise<{ ok: boolean; message: string; id?: string }>; |
| 359 | +}> { |
| 360 | + const backend = await import("../../src/tui/plugins-admin-backend.js"); |
| 361 | + const emptyProjectTrust: ProjectTrustStore = { |
| 362 | + trustedPluginPaths: [], |
| 363 | + trustedMcpFingerprints: [], |
| 364 | + trustedGrantFingerprints: [], |
| 365 | + }; |
| 366 | + const state = backend.createPluginsAdminState({ |
| 367 | + cwd: base, |
| 368 | + settings: undefined, |
| 369 | + modules: [], |
| 370 | + pathTrust: { trustedPluginPaths: [] }, |
| 371 | + projectTrust: emptyProjectTrust, |
| 372 | + }); |
| 373 | + return backend.createPluginsAdmin({ |
| 374 | + state, |
| 375 | + globalSettingsPath: join(base, "settings.json"), |
| 376 | + globalSettingsWriter: stubSettingsWriter(), |
| 377 | + noteWarnings: () => undefined, |
| 378 | + }); |
| 379 | +} |
| 380 | + |
| 381 | +describe("addPath grants path trust before importing plugin code", () => { |
| 382 | + test("single plugin: no JS runs before trustPathPlugins resolves", async () => { |
| 383 | + const base = await mkdtemp(join(tmpdir(), "corbits-addpath-order-")); |
| 384 | + try { |
| 385 | + const home = join(base, "home"); |
| 386 | + await mkdir(home, { recursive: true }); |
| 387 | + const pluginDir = join(base, "shared-plugin"); |
| 388 | + const marker = join(base, "RCE_MARKER"); |
| 389 | + await writeCommandPlugin(pluginDir, "shared-plugin", marker); |
| 390 | + resetAddPathProbe(home, marker); |
| 391 | + |
| 392 | + const admin = await makeAddPathAdmin(base); |
| 393 | + const result = await admin.addPath("shared-plugin"); |
| 394 | + expect(result).toEqual({ |
| 395 | + ok: true, |
| 396 | + message: "Added shared-plugin", |
| 397 | + id: "shared-plugin", |
| 398 | + }); |
| 399 | + |
| 400 | + expect(addPathProbe.trustCalls).toEqual([[pluginDir]]); |
| 401 | + for (const p of addPathProbe.trustCalls.flat()) |
| 402 | + expect(isAbsolute(p)).toBe(true); |
| 403 | + expect(addPathProbe.markerAtTrust).toEqual([false]); |
| 404 | + expect(await Bun.file(marker).exists()).toBe(true); |
| 405 | + expect((await loadPathTrust(home)).trustedPluginPaths).toEqual([ |
| 406 | + pluginDir, |
| 407 | + ]); |
| 408 | + } finally { |
| 409 | + await rm(base, { recursive: true, force: true }); |
| 410 | + } |
| 411 | + }); |
| 412 | + |
| 413 | + test("marketplace root: trust covers expanded members, still before any import", async () => { |
| 414 | + const base = await mkdtemp(join(tmpdir(), "corbits-addpath-mkt-")); |
| 415 | + try { |
| 416 | + const home = join(base, "home"); |
| 417 | + await mkdir(home, { recursive: true }); |
| 418 | + const root = join(base, "marketplace"); |
| 419 | + const marker = join(base, "ROOT_MARKER"); |
| 420 | + await writeCommandPlugin(root, "mkt-root", marker); |
| 421 | + const alpha = join(root, "plugins", "alpha"); |
| 422 | + const beta = join(root, "plugins", "beta"); |
| 423 | + await writeCommandPlugin(alpha, "alpha"); |
| 424 | + await writeCommandPlugin(beta, "beta"); |
| 425 | + await mkdir(join(root, ".claude-plugin"), { recursive: true }); |
| 426 | + await writeFile( |
| 427 | + join(root, ".claude-plugin", "marketplace.json"), |
| 428 | + JSON.stringify({ |
| 429 | + plugins: [ |
| 430 | + { name: "alpha", source: "./plugins/alpha" }, |
| 431 | + { name: "beta", source: "./plugins/beta" }, |
| 432 | + ], |
| 433 | + }), |
| 434 | + "utf8", |
| 435 | + ); |
| 436 | + resetAddPathProbe(home, marker); |
| 437 | + |
| 438 | + const admin = await makeAddPathAdmin(base); |
| 439 | + const result = await admin.addPath("marketplace"); |
| 440 | + expect(result).toEqual({ |
| 441 | + ok: true, |
| 442 | + message: "Added mkt-root", |
| 443 | + id: "mkt-root", |
| 444 | + }); |
| 445 | + |
| 446 | + expect(addPathProbe.trustCalls).toEqual([[alpha, beta]]); |
| 447 | + for (const p of addPathProbe.trustCalls.flat()) |
| 448 | + expect(isAbsolute(p)).toBe(true); |
| 449 | + expect(addPathProbe.markerAtTrust).toEqual([false]); |
| 450 | + expect(await Bun.file(marker).exists()).toBe(true); |
| 451 | + expect((await loadPathTrust(home)).trustedPluginPaths).toEqual([ |
| 452 | + alpha, |
| 453 | + beta, |
| 454 | + ]); |
| 455 | + } finally { |
| 456 | + await rm(base, { recursive: true, force: true }); |
| 457 | + } |
| 458 | + }); |
| 459 | + |
| 460 | + test("bogus path grants nothing", async () => { |
| 461 | + const base = await mkdtemp(join(tmpdir(), "corbits-addpath-bogus-")); |
| 462 | + try { |
| 463 | + const home = join(base, "home"); |
| 464 | + await mkdir(home, { recursive: true }); |
| 465 | + resetAddPathProbe(home, join(base, "NEVER")); |
| 466 | + |
| 467 | + const admin = await makeAddPathAdmin(base); |
| 468 | + const result = await admin.addPath("does-not-exist"); |
| 469 | + expect(result).toEqual({ |
| 470 | + ok: false, |
| 471 | + message: "Could not load a plugin at does-not-exist", |
| 472 | + }); |
| 473 | + expect(addPathProbe.trustCalls).toEqual([]); |
| 474 | + expect((await loadPathTrust(home)).trustedPluginPaths).toEqual([]); |
| 475 | + } finally { |
| 476 | + await rm(base, { recursive: true, force: true }); |
| 477 | + } |
| 478 | + }); |
| 479 | + |
| 480 | + test("post-trust import failure reports the load error and keeps the grant", async () => { |
| 481 | + const base = await mkdtemp(join(tmpdir(), "corbits-addpath-broken-")); |
| 482 | + try { |
| 483 | + const home = join(base, "home"); |
| 484 | + await mkdir(home, { recursive: true }); |
| 485 | + const pluginDir = join(base, "broken-plugin"); |
| 486 | + await mkdir(pluginDir, { recursive: true }); |
| 487 | + await writeFile( |
| 488 | + join(pluginDir, "manifest.json"), |
| 489 | + JSON.stringify({ |
| 490 | + id: "broken-plugin", |
| 491 | + name: "broken-plugin", |
| 492 | + kind: "command", |
| 493 | + }), |
| 494 | + "utf8", |
| 495 | + ); |
| 496 | + await writeFile( |
| 497 | + join(pluginDir, "index.ts"), |
| 498 | + `throw new Error("boom");\n`, |
| 499 | + "utf8", |
| 500 | + ); |
| 501 | + resetAddPathProbe(home, join(base, "NEVER")); |
| 502 | + |
| 503 | + const admin = await makeAddPathAdmin(base); |
| 504 | + const result = await admin.addPath("broken-plugin"); |
| 505 | + // Explicit add-by-path is consent: the grant is recorded before the |
| 506 | + // import runs, so a failed import keeps the grant. Only bogus |
| 507 | + // (unresolvable) paths return before the grant. |
| 508 | + expect(result).toEqual({ |
| 509 | + ok: false, |
| 510 | + message: "Could not load a plugin at broken-plugin", |
| 511 | + }); |
| 512 | + expect(addPathProbe.trustCalls).toEqual([[pluginDir]]); |
| 513 | + expect((await loadPathTrust(home)).trustedPluginPaths).toEqual([ |
| 514 | + pluginDir, |
| 515 | + ]); |
| 516 | + } finally { |
| 517 | + await rm(base, { recursive: true, force: true }); |
| 518 | + } |
| 519 | + }); |
| 520 | +}); |
0 commit comments