plugin-official-mcp-auth.test.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* OMCP-001/002/007/018/020 —— Plugin loader 对 zcode_official 的严格解析(spec §4.2 / §5.2)。
  2. 这里只验证"配置进入 runtime 的形状",信任判定与身份头注入分别在 bootstrap 与 mcp adapter 测试。 */
  3. import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
  4. import { tmpdir } from "node:os";
  5. import { join } from "node:path";
  6. import { describe, expect, it } from "vitest";
  7. import { createNodePluginAdapter } from "../src/plugins/index.js";
  8. const PLUGIN_NAME = "official-mcp-plugin";
  9. const MCP_KEY = "fake-official-search";
  10. const RUNTIME_NAME = `plugin:${PLUGIN_NAME}:${MCP_KEY}`;
  11. interface DiscoverOutcome {
  12. diagnostics: Array<{ code: string; message: string }>;
  13. mcpServers: Record<string, Record<string, unknown>>;
  14. }
  15. async function discoverWithMcpServer(server: Record<string, unknown>): Promise<{
  16. cleanup: () => Promise<void>;
  17. outcome: DiscoverOutcome;
  18. }> {
  19. const dir = await mkdtemp(join(tmpdir(), "zcode-official-mcp-plugin-"));
  20. const pluginRoot = join(dir, PLUGIN_NAME);
  21. await mkdir(join(pluginRoot, ".zcode-plugin"), { recursive: true });
  22. await writeFile(
  23. join(pluginRoot, ".zcode-plugin", "plugin.json"),
  24. JSON.stringify({ name: PLUGIN_NAME }),
  25. );
  26. await writeFile(
  27. join(pluginRoot, ".mcp.json"),
  28. JSON.stringify({ mcpServers: { [MCP_KEY]: server } }),
  29. );
  30. const outcome = (await createNodePluginAdapter({
  31. storageRoot: join(dir, "plugins"),
  32. }).discoverPlugins({
  33. config: { dirs: [pluginRoot], enabled: true, enabledPlugins: {}, options: {} },
  34. env: {},
  35. storageRoot: join(dir, "plugins"),
  36. workingDirectory: dir,
  37. })) as unknown as DiscoverOutcome;
  38. return { cleanup: () => rm(dir, { force: true, recursive: true }), outcome };
  39. }
  40. function expectDisabled(outcome: DiscoverOutcome): void {
  41. expect(outcome.mcpServers[RUNTIME_NAME]).toBeUndefined();
  42. expect(outcome.diagnostics.map((diagnostic) => diagnostic.code)).toContain(
  43. "plugin_mcp_server_disabled",
  44. );
  45. }
  46. describe("plugin mcp zcode_official auth parsing", () => {
  47. it("OMCP-001: keeps auth and host-generated provenance for a valid http declaration", async () => {
  48. const { cleanup, outcome } = await discoverWithMcpServer({
  49. auth: { provider: "jwt_token", type: "zcode_official" },
  50. timeoutMs: 30_000,
  51. type: "http",
  52. url: "https://mcp.zcode.example/mcp",
  53. });
  54. try {
  55. expect(outcome.mcpServers[RUNTIME_NAME]).toMatchObject({
  56. auth: { provider: "jwt_token", type: "zcode_official" },
  57. official: { mcpKey: MCP_KEY, source: "plugin" },
  58. type: "http",
  59. url: "https://mcp.zcode.example/mcp",
  60. });
  61. // provenance 的 pluginId 必须来自宿主装载结果,不是 .mcp.json 能写的值
  62. const official = outcome.mcpServers[RUNTIME_NAME]?.official as { pluginId: string };
  63. expect(official.pluginId).toContain(PLUGIN_NAME);
  64. } finally {
  65. await cleanup();
  66. }
  67. });
  68. it("OMCP-002: ignores an official field forged in .mcp.json", async () => {
  69. const { cleanup, outcome } = await discoverWithMcpServer({
  70. auth: { provider: "jwt_token", type: "zcode_official" },
  71. official: { mcpKey: "spoofed", pluginId: "zcode-tools@official", source: "plugin" },
  72. type: "http",
  73. url: "https://mcp.zcode.example/mcp",
  74. });
  75. try {
  76. const official = outcome.mcpServers[RUNTIME_NAME]?.official as {
  77. mcpKey: string;
  78. pluginId: string;
  79. };
  80. expect(official.mcpKey).toBe(MCP_KEY);
  81. expect(official.pluginId).not.toBe("zcode-tools@official");
  82. } finally {
  83. await cleanup();
  84. }
  85. });
  86. it("rejects alias and casing variants of the auth type", async () => {
  87. for (const type of ["zcode-official_auth", "zcode-official", "ZCODE_OFFICIAL", "Zcode_Official"]) {
  88. const { cleanup, outcome } = await discoverWithMcpServer({
  89. auth: { provider: "jwt_token", type },
  90. type: "http",
  91. url: "https://mcp.zcode.example/mcp",
  92. });
  93. try {
  94. expectDisabled(outcome);
  95. } finally {
  96. await cleanup();
  97. }
  98. }
  99. });
  100. it("rejects an unsupported auth provider", async () => {
  101. const { cleanup, outcome } = await discoverWithMcpServer({
  102. auth: { provider: "short_lived_token", type: "zcode_official" },
  103. type: "http",
  104. url: "https://mcp.zcode.example/mcp",
  105. });
  106. try {
  107. expectDisabled(outcome);
  108. } finally {
  109. await cleanup();
  110. }
  111. });
  112. // phase-2:stdio 放开(身份头改经 tools/call 的 `_meta` 下发),sse 仍拒。
  113. it("OMCP-S07: keeps auth and host-generated provenance for a valid stdio declaration", async () => {
  114. const { cleanup, outcome } = await discoverWithMcpServer({
  115. auth: { provider: "jwt_token", type: "zcode_official" },
  116. command: "python3",
  117. type: "stdio",
  118. });
  119. try {
  120. expect(outcome.mcpServers[RUNTIME_NAME]).toMatchObject({
  121. auth: { provider: "jwt_token", type: "zcode_official" },
  122. official: { mcpKey: MCP_KEY, source: "plugin" },
  123. type: "stdio",
  124. });
  125. const official = outcome.mcpServers[RUNTIME_NAME]?.official as { pluginId: string };
  126. expect(official.pluginId).toContain(PLUGIN_NAME);
  127. } finally {
  128. await cleanup();
  129. }
  130. });
  131. it("OMCP-S08: ignores an official field forged in a stdio declaration", async () => {
  132. const { cleanup, outcome } = await discoverWithMcpServer({
  133. auth: { provider: "jwt_token", type: "zcode_official" },
  134. command: "python3",
  135. official: { mcpKey: "spoofed", pluginId: "zcode-tools@official", source: "plugin" },
  136. type: "stdio",
  137. });
  138. try {
  139. const official = outcome.mcpServers[RUNTIME_NAME]?.official as {
  140. mcpKey: string;
  141. pluginId: string;
  142. };
  143. expect(official.mcpKey).toBe(MCP_KEY);
  144. expect(official.pluginId).not.toBe("zcode-tools@official");
  145. } finally {
  146. await cleanup();
  147. }
  148. });
  149. it("OMCP-S09: rejects zcode_official combined with oauth on stdio", async () => {
  150. const { cleanup, outcome } = await discoverWithMcpServer({
  151. auth: { provider: "jwt_token", type: "zcode_official" },
  152. command: "python3",
  153. oauth: { type: "authorization_code" },
  154. type: "stdio",
  155. });
  156. try {
  157. expectDisabled(outcome);
  158. } finally {
  159. await cleanup();
  160. }
  161. });
  162. it("rejects zcode_official on the sse transport", async () => {
  163. const sse = await discoverWithMcpServer({
  164. auth: { provider: "jwt_token", type: "zcode_official" },
  165. type: "sse",
  166. url: "https://mcp.zcode.example/sse",
  167. });
  168. try {
  169. expectDisabled(sse.outcome);
  170. } finally {
  171. await sse.cleanup();
  172. }
  173. });
  174. it("OMCP-018: rejects zcode_official combined with oauth", async () => {
  175. const { cleanup, outcome } = await discoverWithMcpServer({
  176. auth: { provider: "jwt_token", type: "zcode_official" },
  177. oauth: { type: "authorization_code" },
  178. type: "http",
  179. url: "https://mcp.zcode.example/mcp",
  180. });
  181. try {
  182. expectDisabled(outcome);
  183. } finally {
  184. await cleanup();
  185. }
  186. });
  187. it("OMCP-007 / OMCP-020: rejects reserved identity and protocol headers, case-insensitively", async () => {
  188. const reserved = [
  189. "authorization",
  190. "AUTHORIZATION",
  191. "x-coding-plan-api-key",
  192. "X-Coding-Plan-Api-Key",
  193. "x-bigmodel-authorization",
  194. "bigmodel-target-type",
  195. "Bigmodel-Organization",
  196. "bigmodel-project",
  197. "mcp-session-id",
  198. "MCP-Protocol-Version",
  199. ];
  200. for (const header of reserved) {
  201. const { cleanup, outcome } = await discoverWithMcpServer({
  202. auth: { provider: "jwt_token", type: "zcode_official" },
  203. headers: { [header]: "attacker-supplied" },
  204. type: "http",
  205. url: "https://mcp.zcode.example/mcp",
  206. });
  207. try {
  208. expectDisabled(outcome);
  209. } finally {
  210. await cleanup();
  211. }
  212. }
  213. });
  214. it("keeps non-reserved static headers alongside official auth", async () => {
  215. const { cleanup, outcome } = await discoverWithMcpServer({
  216. auth: { provider: "jwt_token", type: "zcode_official" },
  217. headers: { "x-trace-hint": "keep-me" },
  218. type: "http",
  219. url: "https://mcp.zcode.example/mcp",
  220. });
  221. try {
  222. expect(outcome.mcpServers[RUNTIME_NAME]?.headers).toEqual({ "x-trace-hint": "keep-me" });
  223. } finally {
  224. await cleanup();
  225. }
  226. });
  227. it("OMCP-019: leaves ordinary http MCP untouched, including a static authorization header", async () => {
  228. const { cleanup, outcome } = await discoverWithMcpServer({
  229. headers: { authorization: "Bearer third-party-token" },
  230. type: "http",
  231. url: "https://third-party.example/mcp",
  232. });
  233. try {
  234. // 保留头黑名单只在官方鉴权路径生效;全局拦截会让既有第三方 MCP 回归。
  235. expect(outcome.mcpServers[RUNTIME_NAME]).toMatchObject({
  236. headers: { authorization: "Bearer third-party-token" },
  237. type: "http",
  238. });
  239. expect(outcome.mcpServers[RUNTIME_NAME]?.auth).toBeUndefined();
  240. expect(outcome.mcpServers[RUNTIME_NAME]?.official).toBeUndefined();
  241. } finally {
  242. await cleanup();
  243. }
  244. });
  245. });