official-auth-routing-proxy.mjs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { spawn } from "node:child_process";
  2. import { appendFile } from "node:fs/promises";
  3. import { createInterface } from "node:readline";
  4. // 运行真实 CLI,仅在反向授权边界注入恶意协议请求;不伪造 MCP 连接或 Host 响应。
  5. const [entry, evidenceFile, inject] = process.argv.slice(2);
  6. const child = spawn(process.execPath, [entry, "app-server", "--stdio"], {
  7. env: process.env,
  8. stdio: ["pipe", "pipe", "inherit"],
  9. });
  10. let injected = false;
  11. let writes = Promise.resolve();
  12. const send = (message) => process.stdout.write(`${JSON.stringify(message)}\n`);
  13. createInterface({ input: child.stdout }).on("line", (line) => {
  14. const message = JSON.parse(line);
  15. if (
  16. inject === "probe" &&
  17. !injected &&
  18. message.method === "interaction/requestOfficialMcpAuthHeaders"
  19. ) {
  20. injected = true;
  21. const original = message.params;
  22. const variants = {
  23. unregistered: {
  24. workspace: {
  25. workspacePath: `${original.workspace.workspacePath}-unregistered`,
  26. workspaceKey: `${original.workspace.workspacePath}-unregistered`,
  27. },
  28. },
  29. identity: {
  30. workspace: {
  31. ...original.workspace,
  32. workspaceIdentity: "e2e-unregistered-identity",
  33. workspaceKey: "e2e-unregistered-identity",
  34. },
  35. },
  36. consumer: { consumer: "plugin-command" },
  37. malformed: { targetOrigin: 42 },
  38. origin: { targetOrigin: "https://untrusted.example" },
  39. };
  40. for (const [kind, patch] of Object.entries(variants)) {
  41. send({
  42. id: `e2e-auth-probe-${kind}`,
  43. method: message.method,
  44. params: { ...original, mcpKey: `probe-${kind}`, ...patch },
  45. });
  46. }
  47. }
  48. send(message);
  49. });
  50. createInterface({ input: process.stdin }).on("line", (line) => {
  51. const message = JSON.parse(line);
  52. if (String(message.id).startsWith("e2e-auth-probe-")) {
  53. const evidence = {
  54. kind: message.id.slice("e2e-auth-probe-".length),
  55. code: message.error?.code,
  56. ok: message.result?.ok,
  57. reason: message.result?.reason,
  58. };
  59. writes = writes.then(() => appendFile(evidenceFile, `${JSON.stringify(evidence)}\n`));
  60. return;
  61. }
  62. child.stdin.write(`${line}\n`);
  63. });
  64. process.stdin.on("end", () => child.stdin.end());
  65. for (const signal of ["SIGTERM", "SIGINT"]) {
  66. process.on(signal, () => child.kill(signal));
  67. }
  68. child.on("error", (error) => {
  69. process.stderr.write(`${error.name}: CLI proxy spawn failed\n`);
  70. process.exitCode = 1;
  71. });
  72. child.on("exit", async (code) => {
  73. await writes;
  74. process.exit(code ?? 1);
  75. });