| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- import type { McpServerConfig } from "@zcode/contracts";
- import {
- resetCapturedZCodeCuaBrokerCredentialsForTest,
- sanitizeZCodeRuntimeEnvInPlace,
- ZCODE_CUA_OFFICIAL_PLUGIN_ID,
- ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY,
- ZCODE_PLUGIN_ID_ENV_KEY,
- } from "@zcode/shared";
- import { describe, expect, it } from "vitest";
- import {
- injectZCodeCuaBrokerMcpServers,
- omitMcpServers,
- resolveTrustedOfficialCuaServerNames,
- ZCODE_CUA_BROKER_SOCKET_ENV,
- ZCODE_CUA_BROKER_UNAVAILABLE_ENV,
- } from "../src/mcp-config.js";
- // 遗留 bearer token 的环境变量名。broker 已整体改为 identity 模式(socket + plugin
- // authority,无口令),本进程既不产生也不消费它;但 sanitize 仍必须剔除它,因为用户机上
- // 可能装着认 token 的旧 Helper(见 packages/shared/src/runtimeEnv.ts 同名注释)。
- //
- // 刻意写字面量而不是从 src/mcp-config.js import:那里已经不再导出这个常量,而 vitest 下
- // 缺失的命名导出不报链接错误、只是 undefined,于是 `process.env[undefined]` 变成读
- // `"undefined"` 键,`toBeUndefined()` 之类断言全部永真。2026-09-16 评审实测本文件因此
- // 静默失效(2 条断言退化 + 3 条真失败),且子包 tsconfig 只 include src/**,typecheck 看不到。
- const LEGACY_BROKER_TOKEN_ENV = "ZCODE_CUA_PERMISSION_BROKER_TOKEN";
- describe("project MCP config", () => {
- // CUA 已不再声明任何 MCP server(.zcode-plugin/plugin.json 去掉 mcpServers,执行面搬到
- // 共享 node_repl 宿主),因此官方 CUA server 不出现在任何默认连接集合里。
- // 旧 server 的剔除由 mcp-project-config-startup.test.ts 的启动/状态用例及下方 direct-CLI 用例覆盖;
- // 产品凭据只向可信 node_repl 注入,另有 sanitize 与 captured 优先级用例。
- it("does not trust a project override that copies the official CUA plugin identity", () => {
- const officialPluginConfig: McpServerConfig = {
- type: "stdio",
- command: "/Applications/ZCode Computer Use.app/Contents/MacOS/server",
- env: {
- [ZCODE_PLUGIN_ID_ENV_KEY]: ZCODE_CUA_OFFICIAL_PLUGIN_ID,
- },
- };
- const projectSpoof: McpServerConfig = {
- type: "stdio",
- command: "/tmp/attacker-command",
- env: {
- [ZCODE_PLUGIN_ID_ENV_KEY]: ZCODE_CUA_OFFICIAL_PLUGIN_ID,
- },
- };
- const pluginServers = { "plugin:official-cua:computer-use": officialPluginConfig };
- const configuredServers = {
- ...pluginServers,
- "plugin:official-cua:computer-use": projectSpoof,
- };
- expect(resolveTrustedOfficialCuaServerNames(configuredServers, pluginServers)).toEqual(
- new Set(),
- );
- const injected = injectZCodeCuaBrokerMcpServers(
- configuredServers,
- "/tmp/product-broker.sock",
- "product-token",
- resolveTrustedOfficialCuaServerNames(configuredServers, pluginServers),
- );
- expect(injected["plugin:official-cua:computer-use"]).toBeUndefined();
- });
- it("trusts the unmodified in-memory official CUA plugin config", () => {
- const officialPluginConfig: McpServerConfig = {
- type: "stdio",
- command: "/Applications/ZCode Computer Use.app/Contents/MacOS/server",
- env: {
- [ZCODE_PLUGIN_ID_ENV_KEY]: ZCODE_CUA_OFFICIAL_PLUGIN_ID,
- },
- };
- const pluginServers = { "plugin:official-cua:computer-use": officialPluginConfig };
- const configuredServers = { ...pluginServers };
- expect(resolveTrustedOfficialCuaServerNames(configuredServers, pluginServers)).toEqual(
- new Set(["plugin:official-cua:computer-use"]),
- );
- });
- it("does not inject product broker args into unrelated MCP servers that only mention zcode-cua", () => {
- const servers: Record<string, McpServerConfig> = {
- search: {
- type: "stdio",
- command: "node",
- args: ["/tmp/not-zcode-cua/server.js", "--label", "zcode-cua-notes"],
- },
- };
- const injected = injectZCodeCuaBrokerMcpServers(servers, "/tmp/broker.sock", "tok");
- expect(injected).toBe(servers);
- });
- it("drops explicit zcode-cua package specs instead of injecting broker args", () => {
- const servers: Record<string, McpServerConfig> = {
- custom: {
- type: "stdio",
- command: "uvx",
- args: [
- "--from",
- "zcode-cua[macos]@0.1.8",
- "zcode-cua",
- "--permission-mode",
- "product",
- "--backend=broker",
- "--",
- "--backend",
- "payload-value",
- ],
- },
- };
- const injected = injectZCodeCuaBrokerMcpServers(
- servers,
- "/tmp/broker.sock",
- "tok",
- new Set(["custom"]),
- );
- expect(injected.custom).toBeUndefined();
- });
- it("re-injects the product broker into shared node_repl after CLI env sanitization", () => {
- // 复现 Critical:CLI 入口 sanitize 会先从 process.env 删除 broker socket/token,若定向注入只读
- // process.env,全局 ~/.zcode/cli/config.json 的 zcode-cua 会 fail-open 绕过 broker,让
- // Python/uvx 成为 TCC 主体。修复后 sanitize 前的凭据被捕获到进程内私有存储并仍能定向注入。
- const socketPath = "/tmp/zcode-cua-broker.sock";
- const token = "broker-token-abc";
- const previousSocket = process.env[ZCODE_CUA_BROKER_SOCKET_ENV];
- const previousToken = process.env[LEGACY_BROKER_TOKEN_ENV];
- const previousAuthority = process.env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY];
- const previousRefreshMarker = process.env["ZCODE_CUA_PERMISSION_BROKER_REFRESH_MARKER"];
- const previousUnavailable = process.env[ZCODE_CUA_BROKER_UNAVAILABLE_ENV];
- resetCapturedZCodeCuaBrokerCredentialsForTest();
- try {
- process.env[ZCODE_CUA_BROKER_SOCKET_ENV] = socketPath;
- process.env[LEGACY_BROKER_TOKEN_ENV] = token;
- process.env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY] = "dev.zcode.cua-helper/dev";
- process.env["ZCODE_CUA_PERMISSION_BROKER_REFRESH_MARKER"] =
- `${socketPath}.permission-refresh.json`;
- delete process.env[ZCODE_CUA_BROKER_UNAVAILABLE_ENV];
- // 模拟真实 CLI 入口:sanitize 剔除凭据(剔除前捕获到进程内私有存储)。
- sanitizeZCodeRuntimeEnvInPlace(process.env);
- expect(process.env[ZCODE_CUA_BROKER_SOCKET_ENV]).toBeUndefined();
- expect(process.env[LEGACY_BROKER_TOKEN_ENV]).toBeUndefined();
- const result = omitMcpServers(
- { node_repl: { type: "stdio", command: "node", args: ["server.js"] } },
- new Set(),
- new Set(["node_repl"]),
- );
- const nodeRepl = result.node_repl;
- const env = nodeRepl?.type === "stdio" ? (nodeRepl.env ?? {}) : {};
- expect(nodeRepl?.type === "stdio" ? nodeRepl.args : undefined).toEqual(["server.js"]);
- expect(env[ZCODE_CUA_BROKER_SOCKET_ENV]).toBe(socketPath);
- // identity 模式:凭据只有 socket + authority(+ refresh marker)。遗留 token 即使存在于
- // process.env 也必须止步于 sanitize,不得被重新注入给 node_repl —— 否则等于把一个
- // 旧 Helper 认的口令又发给了子进程。
- expect(env[LEGACY_BROKER_TOKEN_ENV]).toBeUndefined();
- expect(env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY]).toBe("dev.zcode.cua-helper/dev");
- expect(env["ZCODE_CUA_PERMISSION_BROKER_REFRESH_MARKER"]).toBe(
- `${socketPath}.permission-refresh.json`,
- );
- } finally {
- resetCapturedZCodeCuaBrokerCredentialsForTest();
- if (previousSocket === undefined) delete process.env[ZCODE_CUA_BROKER_SOCKET_ENV];
- else process.env[ZCODE_CUA_BROKER_SOCKET_ENV] = previousSocket;
- if (previousToken === undefined) delete process.env[LEGACY_BROKER_TOKEN_ENV];
- else process.env[LEGACY_BROKER_TOKEN_ENV] = previousToken;
- if (previousAuthority === undefined) delete process.env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY];
- else process.env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY] = previousAuthority;
- if (previousRefreshMarker === undefined)
- delete process.env["ZCODE_CUA_PERMISSION_BROKER_REFRESH_MARKER"];
- else process.env["ZCODE_CUA_PERMISSION_BROKER_REFRESH_MARKER"] = previousRefreshMarker;
- if (previousUnavailable === undefined) delete process.env[ZCODE_CUA_BROKER_UNAVAILABLE_ENV];
- else process.env[ZCODE_CUA_BROKER_UNAVAILABLE_ENV] = previousUnavailable;
- }
- });
- it("prefers captured broker credentials over stale/injected process.env values", () => {
- const previousSocket = process.env[ZCODE_CUA_BROKER_SOCKET_ENV];
- const previousToken = process.env[LEGACY_BROKER_TOKEN_ENV];
- const previousAuthority = process.env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY];
- const previousUnavailable = process.env[ZCODE_CUA_BROKER_UNAVAILABLE_ENV];
- resetCapturedZCodeCuaBrokerCredentialsForTest();
- try {
- // 捕获可信凭据(模拟 CLI 入口 sanitize 从 ZCode 下发的 spawn env 捕获)。
- const spawnEnv: Record<string, string | undefined> = {
- [ZCODE_CUA_BROKER_SOCKET_ENV]: "/tmp/captured.sock",
- [LEGACY_BROKER_TOKEN_ENV]: "captured-token",
- [ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY]: "dev.zcode.cua-helper/captured",
- };
- sanitizeZCodeRuntimeEnvInPlace(spawnEnv);
- // 运行时 env 里残留/被注入了不同的 broker 值:绝不能覆盖 captured。
- process.env[ZCODE_CUA_BROKER_SOCKET_ENV] = "/tmp/stale.sock";
- process.env[LEGACY_BROKER_TOKEN_ENV] = "stale-token";
- delete process.env[ZCODE_CUA_BROKER_UNAVAILABLE_ENV];
- const result = omitMcpServers(
- { node_repl: { type: "stdio", command: "node", args: ["server.js"] } },
- new Set(),
- new Set(["node_repl"]),
- );
- const nodeRepl = result.node_repl;
- const env = nodeRepl?.type === "stdio" ? (nodeRepl.env ?? {}) : {};
- expect(nodeRepl?.type === "stdio" ? nodeRepl.args : undefined).toEqual(["server.js"]);
- expect(env[ZCODE_CUA_BROKER_SOCKET_ENV]).toBe("/tmp/captured.sock");
- // captured 凭据也不含 token(identity 模式);spawn env 里的遗留 token 被 sanitize 吃掉。
- expect(env[LEGACY_BROKER_TOKEN_ENV]).toBeUndefined();
- expect(env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY]).toBe("dev.zcode.cua-helper/captured");
- } finally {
- resetCapturedZCodeCuaBrokerCredentialsForTest();
- if (previousSocket === undefined) delete process.env[ZCODE_CUA_BROKER_SOCKET_ENV];
- else process.env[ZCODE_CUA_BROKER_SOCKET_ENV] = previousSocket;
- if (previousToken === undefined) delete process.env[LEGACY_BROKER_TOKEN_ENV];
- else process.env[LEGACY_BROKER_TOKEN_ENV] = previousToken;
- if (previousAuthority === undefined) delete process.env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY];
- else process.env[ZCODE_CUA_PLUGIN_AUTHORITY_ENV_KEY] = previousAuthority;
- if (previousUnavailable === undefined) delete process.env[ZCODE_CUA_BROKER_UNAVAILABLE_ENV];
- else process.env[ZCODE_CUA_BROKER_UNAVAILABLE_ENV] = previousUnavailable;
- }
- });
- describe("retired direct-CLI zcode-cua never starts an MCP server", () => {
- const restore = (snapshot: Record<string, string | undefined>) => {
- for (const [key, value] of Object.entries(snapshot)) {
- if (value === undefined) delete process.env[key];
- else process.env[key] = value;
- }
- };
- const KEYS = [
- ZCODE_CUA_BROKER_SOCKET_ENV,
- LEGACY_BROKER_TOKEN_ENV,
- ZCODE_CUA_BROKER_UNAVAILABLE_ENV,
- "ZCODE_CUA_HELPER_ALLOW_UNAUTHENTICATED_LOCAL",
- ];
- const snapshot = () =>
- Object.fromEntries(KEYS.map((k) => [k, process.env[k]])) as Record<
- string,
- string | undefined
- >;
- const clearAll = () => KEYS.forEach((k) => delete process.env[k]);
- const cuaServer = (): Record<string, McpServerConfig> => ({
- "zcode-cua": {
- type: "stdio",
- command: "uvx",
- args: ["zcode-cua"],
- },
- });
- it("omits a global zcode-cua regardless of broker state", () => {
- const saved = snapshot();
- resetCapturedZCodeCuaBrokerCredentialsForTest();
- try {
- clearAll();
- const result = omitMcpServers(cuaServer(), new Set());
- expect(result["zcode-cua"]).toBeUndefined();
- } finally {
- resetCapturedZCodeCuaBrokerCredentialsForTest();
- restore(saved);
- }
- });
- });
- });
|