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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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(
  34. options: { resolveWorkspace?: ReturnType<typeof entrypointResolveWorkspace> } = {},
  35. ) {
  36. const requestClient = vi.fn(async () => OK);
  37. const context = { requestClient } as unknown as OfficialMcpAuthRequestContext;
  38. const port = createOfficialMcpAuthHeadersPort({
  39. resolveContext: () => context,
  40. resolveWorkspace: options.resolveWorkspace ?? entrypointResolveWorkspace("/fallback"),
  41. });
  42. const sentWorkspace = (call: number) =>
  43. (requestClient.mock.calls[call]?.[1] as { workspace: Record<string, string> }).workspace;
  44. return { port, requestClient, sentWorkspace };
  45. }
  46. const BASE_REQUEST = {
  47. mcpKey: "image-search",
  48. pluginId: "zcode-tools@zcode-plugins-official",
  49. targetOrigin: "https://mcp.zcode.example",
  50. };
  51. describe("official mcp auth headers port", () => {
  52. it("CR-01: keeps same-path workspaces distinguishable by workspaceIdentity", async () => {
  53. const { port, sentWorkspace } = createHarness();
  54. await port.resolveHeaders({
  55. ...BASE_REQUEST,
  56. workspaceIdentity: "remote:wsl:ubuntu:/workspace/app",
  57. workspacePath: "/workspace/app",
  58. });
  59. await port.resolveHeaders({ ...BASE_REQUEST, workspacePath: "/workspace/app" });
  60. // 同 workspacePath,但 identity 不同 -> 协议参数必须可区分
  61. expect(sentWorkspace(0)).toEqual({
  62. workspaceIdentity: "remote:wsl:ubuntu:/workspace/app",
  63. workspaceKey: "remote:wsl:ubuntu:/workspace/app",
  64. workspacePath: "/workspace/app",
  65. });
  66. expect(sentWorkspace(1)).toEqual({
  67. workspaceKey: "/workspace/app",
  68. workspacePath: "/workspace/app",
  69. });
  70. });
  71. it("sends the declared plugin identity and target origin unchanged", async () => {
  72. const { port, requestClient } = createHarness();
  73. await port.resolveHeaders({ ...BASE_REQUEST, workspacePath: "/repo" });
  74. expect(requestClient).toHaveBeenCalledWith(
  75. zcodeProtocolMethods.interactionRequestOfficialMcpAuthHeaders,
  76. expect.objectContaining({
  77. mcpKey: "image-search",
  78. pluginId: "zcode-tools@zcode-plugins-official",
  79. targetOrigin: "https://mcp.zcode.example",
  80. }),
  81. expect.anything(),
  82. expect.anything(),
  83. );
  84. });
  85. it("uses a distinct requestId per call", async () => {
  86. const { port, requestClient } = createHarness();
  87. await port.resolveHeaders({ ...BASE_REQUEST, workspacePath: "/repo" });
  88. await port.resolveHeaders({ ...BASE_REQUEST, workspacePath: "/repo" });
  89. const ids = requestClient.mock.calls.map(
  90. (call) => (call[1] as { requestId: string }).requestId,
  91. );
  92. expect(new Set(ids).size).toBe(2);
  93. });
  94. it("fails closed when the server context is not ready yet", async () => {
  95. const requestClient = vi.fn(async () => OK);
  96. const port = createOfficialMcpAuthHeadersPort({
  97. resolveContext: () => undefined,
  98. resolveWorkspace: entrypointResolveWorkspace("/fallback"),
  99. });
  100. await expect(port.resolveHeaders({ ...BASE_REQUEST, workspacePath: "/repo" })).resolves.toEqual(
  101. { ok: false, reason: "official_auth_unavailable" },
  102. );
  103. expect(requestClient).not.toHaveBeenCalled();
  104. });
  105. it("fails closed when no workspace can be resolved", async () => {
  106. const { port, requestClient } = createHarness({
  107. resolveWorkspace: entrypointResolveWorkspace(""),
  108. });
  109. await expect(port.resolveHeaders({ ...BASE_REQUEST })).resolves.toEqual({
  110. ok: false,
  111. reason: "official_auth_unavailable",
  112. });
  113. expect(requestClient).not.toHaveBeenCalled();
  114. });
  115. it("maps a transport failure to official_auth_unavailable without leaking details", async () => {
  116. const context = {
  117. requestClient: vi.fn(async () => {
  118. throw new Error("stdio closed: /secret/path context");
  119. }),
  120. } as unknown as OfficialMcpAuthRequestContext;
  121. const port = createOfficialMcpAuthHeadersPort({
  122. resolveContext: () => context,
  123. resolveWorkspace: entrypointResolveWorkspace("/fallback"),
  124. });
  125. const result = await port.resolveHeaders({ ...BASE_REQUEST, workspacePath: "/repo" });
  126. // 绝不降级为匿名,且不把可能含上下文的错误详情带出
  127. expect(result).toEqual({ ok: false, reason: "official_auth_unavailable" });
  128. });
  129. });