officialMcpTrust.test.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* 官方 MCP 信任判定(spec §5.1 / §5.2)。
  2. *
  3. * 规则:目标 origin 等于当前 ZCode API origin(https)。
  4. *
  5. * 2026-08 决策变更:移除了「pluginId 必须以 `@zcode-plugins-official` 结尾」这道检查。
  6. * 它会让官方插件在发布到官方 marketplace 之前无法对真实端点自测(本地装的插件 id 是
  7. * `@inline` 或 `@<本地 marketplace>`),而 dev loopback 开关只放开 http loopback、救不了
  8. * https 端点。因此这里的承重项只剩 **origin 相等**(防外泄)。
  9. */
  10. import { describe, expect, it } from "vitest";
  11. import {
  12. createOfficialMcpTrustedOriginRegistry,
  13. isOfficialMcpOriginTrusted,
  14. } from "../src/official-mcp-auth.js";
  15. const OFFICIAL_PLUGIN = "zcode-tools@zcode-plugins-official";
  16. const ZCODE_ORIGIN = "https://zcode.z.ai";
  17. function check(input: {
  18. devTrustedOriginsRaw?: string;
  19. origin: string;
  20. pluginId?: string;
  21. zcodeApiOrigin?: string | undefined;
  22. }) {
  23. return isOfficialMcpOriginTrusted({
  24. devTrustedOriginsRaw: input.devTrustedOriginsRaw,
  25. origin: input.origin,
  26. pluginId: input.pluginId ?? OFFICIAL_PLUGIN,
  27. zcodeApiOrigin: "zcodeApiOrigin" in input ? input.zcodeApiOrigin : ZCODE_ORIGIN,
  28. });
  29. }
  30. describe("isOfficialMcpOriginTrusted", () => {
  31. it("trusts the current ZCode API origin", () => {
  32. expect(check({ origin: ZCODE_ORIGIN })).toEqual({ detail: "ok", trusted: true });
  33. // 带路径的 URL 归一化到同一 origin(真实端点形态:POST /api/v1/mcp/server/:mcp_group)
  34. expect(check({ origin: `${ZCODE_ORIGIN}/api/v1/mcp/server/image_search` })).toMatchObject({
  35. trusted: true,
  36. });
  37. });
  38. it("accepts any plugin id at the correct origin", () => {
  39. // pluginId 不再参与判定:inline、本地 marketplace、第三方一律按 origin 判断。
  40. for (const pluginId of [
  41. "official-tools@zcode-plugins-local",
  42. "dev-official-mcp@inline",
  43. "evil@third-party",
  44. "zcode-tools",
  45. ]) {
  46. expect(check({ origin: ZCODE_ORIGIN, pluginId })).toEqual({ detail: "ok", trusted: true });
  47. }
  48. });
  49. it("still rejects those same plugins at a wrong origin", () => {
  50. // 移除 marketplace 检查后,origin 是唯一屏障——必须确认它没被一起放松。
  51. for (const pluginId of ["evil@third-party", "official-tools@zcode-plugins-local"]) {
  52. expect(check({ origin: "https://attacker.example", pluginId })).toEqual({
  53. detail: "origin_mismatch",
  54. trusted: false,
  55. });
  56. }
  57. });
  58. it("rejects any origin other than the resolved ZCode API origin", () => {
  59. for (const origin of [
  60. "https://attacker.example",
  61. "https://zcode.z.ai.attacker.example",
  62. "https://evil-zcode.z.ai",
  63. `${ZCODE_ORIGIN}:8443`,
  64. ]) {
  65. expect(check({ origin })).toEqual({ detail: "origin_mismatch", trusted: false });
  66. }
  67. });
  68. it("rejects http even when the host matches", () => {
  69. // origin 相等是承重项,但仍必须是 https——明文会让凭证在链路上暴露
  70. expect(check({ origin: "http://zcode.z.ai" })).toMatchObject({ trusted: false });
  71. });
  72. it("rejects credential-bearing URLs on either side", () => {
  73. expect(check({ origin: `https://user:pass@zcode.z.ai` })).toMatchObject({ trusted: false });
  74. expect(
  75. check({ origin: ZCODE_ORIGIN, zcodeApiOrigin: "https://user:pass@zcode.z.ai" }),
  76. ).toMatchObject({ trusted: false });
  77. });
  78. it("fails closed when the ZCode API origin cannot be resolved", () => {
  79. expect(check({ origin: ZCODE_ORIGIN, zcodeApiOrigin: undefined })).toEqual({
  80. detail: "zcode_origin_unresolved",
  81. trusted: false,
  82. });
  83. expect(check({ origin: ZCODE_ORIGIN, zcodeApiOrigin: "not-a-url" })).toEqual({
  84. detail: "zcode_origin_unresolved",
  85. trusted: false,
  86. });
  87. });
  88. it("rejects malformed input without throwing", () => {
  89. expect(check({ origin: "" })).toEqual({ detail: "invalid_input", trusted: false });
  90. expect(check({ origin: "not-a-url" })).toMatchObject({ trusted: false });
  91. // 空 pluginId 不再是拒绝理由,但空 origin 是
  92. expect(check({ origin: ZCODE_ORIGIN, pluginId: "" })).toMatchObject({ trusted: true });
  93. });
  94. describe("dev loopback switch", () => {
  95. it("accepts a loopback origin listed in the switch", () => {
  96. expect(
  97. check({ devTrustedOriginsRaw: "http://127.0.0.1:3999", origin: "http://127.0.0.1:3999" }),
  98. ).toEqual({ detail: "ok", trusted: true });
  99. });
  100. it("only opens loopback, never a remote origin", () => {
  101. // 关键:dev 开关不得成为"一个环境变量把 JWT 导向任意站点"的通道
  102. for (const entry of ["https://attacker.example", "http://evil.example"]) {
  103. expect(check({ devTrustedOriginsRaw: entry, origin: entry })).toMatchObject({
  104. trusted: false,
  105. });
  106. }
  107. });
  108. it("supports several comma-separated entries and ignores blanks", () => {
  109. const raw = " http://127.0.0.1:3999 , ,http://localhost:4010 ";
  110. expect(check({ devTrustedOriginsRaw: raw, origin: "http://localhost:4010" })).toMatchObject({
  111. trusted: true,
  112. });
  113. expect(check({ devTrustedOriginsRaw: raw, origin: "http://127.0.0.1:5000" })).toMatchObject({
  114. trusted: false,
  115. });
  116. });
  117. it("does not require an official plugin for loopback self-testing", () => {
  118. // 本地 plugin 经 dirs 加载时 marketplace 是 inline,自测必须仍可用
  119. expect(
  120. check({
  121. devTrustedOriginsRaw: "http://127.0.0.1:3999",
  122. origin: "http://127.0.0.1:3999",
  123. pluginId: "dev-official-mcp@inline",
  124. }),
  125. ).toMatchObject({ trusted: true });
  126. });
  127. it("changes nothing when the switch is absent", () => {
  128. expect(check({ origin: "http://127.0.0.1:3999" })).toMatchObject({ trusted: false });
  129. });
  130. it("still rejects a loopback origin that is not listed", () => {
  131. // 移除 marketplace 检查后,loopback 也必须逐条登记才放行,不能因 http loopback 就通过
  132. expect(
  133. check({ devTrustedOriginsRaw: "http://127.0.0.1:3999", origin: "http://127.0.0.1:9999" }),
  134. ).toMatchObject({ trusted: false });
  135. });
  136. });
  137. });
  138. describe("createOfficialMcpTrustedOriginRegistry", () => {
  139. it("resolves the ZCode API origin per call, so environment switches are picked up", async () => {
  140. let origin = ZCODE_ORIGIN;
  141. const registry = createOfficialMcpTrustedOriginRegistry({
  142. resolveZCodeApiOrigin: () => origin,
  143. });
  144. await expect(
  145. registry.isTrusted({
  146. mcpKey: "image-search",
  147. origin: ZCODE_ORIGIN,
  148. pluginId: OFFICIAL_PLUGIN,
  149. }),
  150. ).resolves.toMatchObject({ trusted: true });
  151. // 切到 test 环境域名后,旧 origin 立即不再可信
  152. origin = "https://zcode-test.z.ai";
  153. await expect(
  154. registry.isTrusted({
  155. mcpKey: "image-search",
  156. origin: ZCODE_ORIGIN,
  157. pluginId: OFFICIAL_PLUGIN,
  158. }),
  159. ).resolves.toMatchObject({ trusted: false });
  160. await expect(
  161. registry.isTrusted({
  162. mcpKey: "image-search",
  163. origin: "https://zcode-test.z.ai",
  164. pluginId: OFFICIAL_PLUGIN,
  165. }),
  166. ).resolves.toMatchObject({ trusted: true });
  167. });
  168. it("fails closed when the origin resolver throws", async () => {
  169. const registry = createOfficialMcpTrustedOriginRegistry({
  170. resolveZCodeApiOrigin: () => {
  171. throw new Error("settings unavailable");
  172. },
  173. });
  174. await expect(
  175. registry.isTrusted({
  176. mcpKey: "image-search",
  177. origin: ZCODE_ORIGIN,
  178. pluginId: OFFICIAL_PLUGIN,
  179. }),
  180. ).resolves.toEqual({ detail: "zcode_origin_unresolved", trusted: false });
  181. });
  182. it("awaits an async origin resolver", async () => {
  183. const registry = createOfficialMcpTrustedOriginRegistry({
  184. resolveZCodeApiOrigin: async () => ZCODE_ORIGIN,
  185. });
  186. await expect(
  187. registry.isTrusted({
  188. mcpKey: "image-search",
  189. origin: ZCODE_ORIGIN,
  190. pluginId: OFFICIAL_PLUGIN,
  191. }),
  192. ).resolves.toMatchObject({ trusted: true });
  193. });
  194. it("ignores mcpKey entirely (no per-key allowlist any more)", async () => {
  195. const registry = createOfficialMcpTrustedOriginRegistry({
  196. resolveZCodeApiOrigin: () => ZCODE_ORIGIN,
  197. });
  198. for (const mcpKey of ["image-search", "brand-new-mcp", ""]) {
  199. await expect(
  200. registry.isTrusted({ mcpKey, origin: ZCODE_ORIGIN, pluginId: OFFICIAL_PLUGIN }),
  201. ).resolves.toMatchObject({ trusted: true });
  202. }
  203. });
  204. });