official-mcp-auth-port.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* CR-01 —— 官方 MCP 身份头端口构造协议参数的定向测试(spec §7.1 / §7.2)。
  2. *
  3. * 覆盖缺口说明:officialMcpAuthRemoteRouting.test.ts 验证的是 service 层**收到** workspace
  4. * 之后的响应路由,不覆盖本端口把 OfficialMcpAuthHeadersPort 入参转成协议参数这一段。
  5. * 同路径、不同 identity 的远端 workspace 在这里如果丢掉 identity,审计上下文就无法区分。
  6. */
  7. import { describe, expect, it, vi } from "vitest";
  8. import { zcodeProtocolMethods } from "@zcode/shared";
  9. import {
  10. createOfficialMcpAuthHeadersPort,
  11. type OfficialMcpAuthRequestContext,
  12. } from "../src/zcode-protocol/official-mcp-auth-port.js";
  13. const OK = { headers: { Authorization: "Bearer jwt" }, ok: true as const };
  14. /** 与 zcode-protocol-entrypoint.ts 相同的构造口径:workspaceKey = identity?.trim() || path。 */
  15. function entrypointResolveWorkspace(fallbackCwd: string) {
  16. return ({
  17. workspaceIdentity,
  18. workspacePath,
  19. }: {
  20. workspaceIdentity?: string;
  21. workspacePath?: string;
  22. }) => {
  23. const path = workspacePath ?? fallbackCwd;
  24. if (!path) return undefined;
  25. const identity = workspaceIdentity?.trim();
  26. return {
  27. ...(identity ? { workspaceIdentity: identity } : {}),
  28. workspaceKey: identity || path,
  29. workspacePath: path,
  30. };
  31. };
  32. }
  33. function createHarness(options: { resolveWorkspace?: ReturnType<typeof entrypointResolveWorkspace> } = {}) {
  34. const requestClient = vi.fn(async () => OK);
  35. const context = { requestClient } as unknown as OfficialMcpAuthRequestContext;
  36. const port = createOfficialMcpAuthHeadersPort({
  37. resolveContext: () => context,
  38. resolveWorkspace: options.resolveWorkspace ?? entrypointResolveWorkspace("/fallback"),
  39. });
  40. const sentWorkspace = (call: number) =>
  41. (requestClient.mock.calls[call]?.[1] as { workspace: Record<string, string> }).workspace;
  42. return { port, requestClient, sentWorkspace };
  43. }
  44. const BASE_REQUEST = {
  45. mcpKey: "image-search",
  46. pluginId: "zcode-tools@zcode-plugins-official",
  47. targetOrigin: "https://mcp.zcode.example",
  48. };
  49. describe("official mcp auth headers port", () => {
  50. it("CR-01: keeps same-path workspaces distinguishable by workspaceIdentity", async () => {
  51. const { port, sentWorkspace } = createHarness();
  52. await port.resolveHeaders({
  53. ...BASE_REQUEST,
  54. workspaceIdentity: "remote:wsl:ubuntu:/workspace/app",
  55. workspacePath: "/workspace/app",
  56. });
  57. await port.resolveHeaders({ ...BASE_REQUEST, workspacePath: "/workspace/app" });
  58. // 同 workspacePath,但 identity 不同 -> 协议参数必须可区分
  59. expect(sentWorkspace(0)).toEqual({
  60. workspaceIdentity: "remote:wsl:ubuntu:/workspace/app",
  61. workspaceKey: "remote:wsl:ubuntu:/workspace/app",
  62. workspacePath: "/workspace/app",
  63. });
  64. expect(sentWorkspace(1)).toEqual({
  65. workspaceKey: "/workspace/app",
  66. workspacePath: "/workspace/app",
  67. });
  68. expect(sentWorkspace(0).workspaceKey).not.toBe(sentWorkspace(1).workspaceKey);
  69. });
  70. it("forwards workspaceIdentity through to resolveWorkspace", async () => {
  71. const resolveWorkspace = vi.fn(entrypointResolveWorkspace("/fallback"));
  72. const { port } = createHarness({ resolveWorkspace });
  73. await port.resolveHeaders({
  74. ...BASE_REQUEST,
  75. workspaceIdentity: "remote:ssh:host:/repo",
  76. workspacePath: "/repo",
  77. });
  78. expect(resolveWorkspace).toHaveBeenCalledWith({
  79. workspaceIdentity: "remote:ssh:host:/repo",
  80. workspacePath: "/repo",
  81. });
  82. });
  83. it("treats a blank identity as absent instead of producing an empty workspaceKey", async () => {
  84. const { port, sentWorkspace } = createHarness();
  85. await port.resolveHeaders({ ...BASE_REQUEST, workspaceIdentity: " ", workspacePath: "/repo" });
  86. expect(sentWorkspace(0)).toEqual({ workspaceKey: "/repo", workspacePath: "/repo" });
  87. });
  88. it("sends the declared plugin identity and target origin unchanged", async () => {
  89. const { port, requestClient } = createHarness();
  90. await port.resolveHeaders({ ...BASE_REQUEST, workspacePath: "/repo" });
  91. expect(requestClient).toHaveBeenCalledWith(
  92. zcodeProtocolMethods.interactionRequestOfficialMcpAuthHeaders,
  93. expect.objectContaining({
  94. mcpKey: "image-search",
  95. pluginId: "zcode-tools@zcode-plugins-official",
  96. targetOrigin: "https://mcp.zcode.example",
  97. }),
  98. expect.anything(),
  99. expect.anything(),
  100. );
  101. });
  102. it("uses a distinct requestId per call", async () => {
  103. const { port, requestClient } = createHarness();
  104. await port.resolveHeaders({ ...BASE_REQUEST, workspacePath: "/repo" });
  105. await port.resolveHeaders({ ...BASE_REQUEST, workspacePath: "/repo" });
  106. const ids = requestClient.mock.calls.map(
  107. (call) => (call[1] as { requestId: string }).requestId,
  108. );
  109. expect(new Set(ids).size).toBe(2);
  110. });
  111. it("fails closed when the server context is not ready yet", async () => {
  112. const requestClient = vi.fn(async () => OK);
  113. const port = createOfficialMcpAuthHeadersPort({
  114. resolveContext: () => undefined,
  115. resolveWorkspace: entrypointResolveWorkspace("/fallback"),
  116. });
  117. await expect(
  118. port.resolveHeaders({ ...BASE_REQUEST, workspacePath: "/repo" }),
  119. ).resolves.toEqual({ ok: false, reason: "official_auth_unavailable" });
  120. expect(requestClient).not.toHaveBeenCalled();
  121. });
  122. it("fails closed when no workspace can be resolved", async () => {
  123. const { port, requestClient } = createHarness({
  124. resolveWorkspace: entrypointResolveWorkspace(""),
  125. });
  126. await expect(port.resolveHeaders({ ...BASE_REQUEST })).resolves.toEqual({
  127. ok: false,
  128. reason: "official_auth_unavailable",
  129. });
  130. expect(requestClient).not.toHaveBeenCalled();
  131. });
  132. it("maps a transport failure to official_auth_unavailable without leaking details", async () => {
  133. const context = {
  134. requestClient: vi.fn(async () => {
  135. throw new Error("stdio closed: /secret/path context");
  136. }),
  137. } as unknown as OfficialMcpAuthRequestContext;
  138. const port = createOfficialMcpAuthHeadersPort({
  139. resolveContext: () => context,
  140. resolveWorkspace: entrypointResolveWorkspace("/fallback"),
  141. });
  142. const result = await port.resolveHeaders({ ...BASE_REQUEST, workspacePath: "/repo" });
  143. // 绝不降级为匿名,且不把可能含上下文的错误详情带出
  144. expect(result).toEqual({ ok: false, reason: "official_auth_unavailable" });
  145. expect(JSON.stringify(result)).not.toContain("/secret/path");
  146. });
  147. });