runtimeEnv.test.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY,
  4. ZCODE_REMOTE_HTTP_PROXY_ENV_KEY,
  5. ZCODE_REMOTE_NO_PROXY_ENV_KEY,
  6. ZCODE_REMOTE_RUNTIME_NETWORK_AUTHORITY_ENV_KEY,
  7. buildZCodeToolEnvPassthroughEnv,
  8. getCapturedZCodeCuaBrokerCredentials,
  9. isCuaDevModeRequested,
  10. isZCodeCuaInternalFeatureEnabled,
  11. readZCodeAgentTelemetryEnv,
  12. readZCodeToolEnvPassthroughEnv,
  13. resetCapturedZCodeCuaBrokerCredentialsForTest,
  14. sanitizeZCodeRuntimeEnv,
  15. getCapturedZCodeAgentTelemetryEnv,
  16. resetCapturedZCodeAgentTelemetryEnvForTest,
  17. } from "../src/runtimeEnv.js";
  18. describe("runtimeEnv", () => {
  19. it("enables internal CUA by default while honoring explicit false values", () => {
  20. expect(isZCodeCuaInternalFeatureEnabled({})).toBe(true);
  21. expect(isZCodeCuaInternalFeatureEnabled({ ZCODE_CUA_PRODUCT_HELPER: "0" })).toBe(false);
  22. expect(isZCodeCuaInternalFeatureEnabled({ ZCODE_CUA_PRODUCT_HELPER: "false" })).toBe(false);
  23. expect(isZCodeCuaInternalFeatureEnabled({ ZCODE_CUA_PRODUCT_HELPER: "1" })).toBe(true);
  24. expect(isZCodeCuaInternalFeatureEnabled({ ZCODE_CUA_PRODUCT_HELPER: " TRUE " })).toBe(true);
  25. expect(isZCodeCuaInternalFeatureEnabled({ ZCODE_CUA_PRODUCT_HELPER: "on" })).toBe(true);
  26. });
  27. it("parses explicit truthy values for ZCODE_CUA_DEV_MODE", () => {
  28. // isCuaDevModeRequested: only explicit truthy values (case-insensitive, trimmed).
  29. expect(isCuaDevModeRequested({})).toBe(false);
  30. expect(isCuaDevModeRequested({ ZCODE_CUA_DEV_MODE: "0" })).toBe(false);
  31. expect(isCuaDevModeRequested({ ZCODE_CUA_DEV_MODE: "false" })).toBe(false);
  32. expect(isCuaDevModeRequested({ ZCODE_CUA_DEV_MODE: "1" })).toBe(true);
  33. expect(isCuaDevModeRequested({ ZCODE_CUA_DEV_MODE: " ON " })).toBe(true);
  34. expect(isCuaDevModeRequested({ ZCODE_CUA_DEV_MODE: "true" })).toBe(true);
  35. });
  36. it("sanitizes runtime env while sealing user network env for tools", () => {
  37. const sourceEnv = {
  38. HTTP_PROXY: "http://proxy.example:8080",
  39. NODE_ENV: "development",
  40. NODE_EXTRA_CA_CERTS: "/tmp/root-ca.pem",
  41. PATH: "/usr/bin:/bin",
  42. npm_config_proxy: "http://npm-proxy.example:8080",
  43. };
  44. const sanitized = sanitizeZCodeRuntimeEnv(sourceEnv);
  45. const passthroughEnv = buildZCodeToolEnvPassthroughEnv(sourceEnv);
  46. const captured = readZCodeToolEnvPassthroughEnv(passthroughEnv);
  47. expect(sanitized).toEqual({ PATH: "/usr/bin:/bin" });
  48. expect(captured).toEqual({
  49. HTTP_PROXY: "http://proxy.example:8080",
  50. NODE_EXTRA_CA_CERTS: "/tmp/root-ca.pem",
  51. npm_config_proxy: "http://npm-proxy.example:8080",
  52. });
  53. });
  54. it("ignores invalid passthrough JSON and non-network keys", () => {
  55. const captured = readZCodeToolEnvPassthroughEnv({
  56. [ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY]: JSON.stringify({
  57. HTTP_PROXY: "http://proxy.example:8080",
  58. NODE_ENV: "production",
  59. PATH: "/tmp/bin",
  60. }),
  61. });
  62. expect(captured).toEqual({
  63. HTTP_PROXY: "http://proxy.example:8080",
  64. });
  65. expect(
  66. readZCodeToolEnvPassthroughEnv({
  67. [ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY]: "not-json",
  68. }),
  69. ).toEqual({});
  70. });
  71. it("strips the CUA broker socket/authority/refresh marker from child env and tool passthrough", () => {
  72. // Confused-deputy 防护:broker bearer 凭据只该定向注入目标 zcode-cua server,绝不能随全局 env
  73. // 泄漏给其它 MCP / Bash / tool 子进程,也不能经 tool-env-passthrough 恢复。
  74. const sourceEnv = {
  75. PATH: "/usr/bin:/bin",
  76. ZCODE_CUA_PERMISSION_BROKER_SOCKET: "/tmp/zcode-cua-me/broker-abc.sock",
  77. ZCODE_CUA_PERMISSION_BROKER_REFRESH_MARKER:
  78. "/tmp/zcode-cua-me/broker-abc.sock.permission-refresh.json",
  79. };
  80. const sanitized = sanitizeZCodeRuntimeEnv(sourceEnv);
  81. expect(sanitized).toEqual({ PATH: "/usr/bin:/bin" });
  82. const captured = readZCodeToolEnvPassthroughEnv(buildZCodeToolEnvPassthroughEnv(sourceEnv));
  83. expect(captured).not.toHaveProperty("ZCODE_CUA_PERMISSION_BROKER_SOCKET");
  84. expect(captured).not.toHaveProperty("ZCODE_CUA_PERMISSION_BROKER_REFRESH_MARKER");
  85. });
  86. it("strips desktop-to-remote network handoff values after stdio bootstrap", () => {
  87. const sourceEnv = {
  88. PATH: "/usr/bin:/bin",
  89. [ZCODE_REMOTE_RUNTIME_NETWORK_AUTHORITY_ENV_KEY]: "1",
  90. [ZCODE_REMOTE_HTTP_PROXY_ENV_KEY]: "http://172.21.240.1:7890",
  91. [ZCODE_REMOTE_NO_PROXY_ENV_KEY]: "localhost,127.0.0.1",
  92. };
  93. expect(sanitizeZCodeRuntimeEnv(sourceEnv)).toEqual({ PATH: "/usr/bin:/bin" });
  94. expect(readZCodeToolEnvPassthroughEnv(buildZCodeToolEnvPassthroughEnv(sourceEnv))).toEqual({});
  95. });
  96. it("captures broker credentials as one atomic pair and clears stale state on partial rotation", () => {
  97. resetCapturedZCodeCuaBrokerCredentialsForTest();
  98. try {
  99. sanitizeZCodeRuntimeEnv({
  100. ZCODE_CUA_PERMISSION_BROKER_SOCKET: "/tmp/broker-a.sock",
  101. ZCODE_CUA_PLUGIN_AUTHORITY: "authority-a",
  102. });
  103. expect(getCapturedZCodeCuaBrokerCredentials()).toEqual({
  104. socket: "/tmp/broker-a.sock",
  105. pluginAuthority: "authority-a",
  106. });
  107. sanitizeZCodeRuntimeEnv({
  108. ZCODE_CUA_PERMISSION_BROKER_SOCKET: "/tmp/broker-b.sock",
  109. });
  110. expect(getCapturedZCodeCuaBrokerCredentials()).toEqual({
  111. socket: undefined,
  112. pluginAuthority: undefined,
  113. });
  114. sanitizeZCodeRuntimeEnv({
  115. ZCODE_CUA_PERMISSION_BROKER_SOCKET: "/tmp/broker-b.sock",
  116. ZCODE_CUA_PLUGIN_AUTHORITY: "authority-b",
  117. });
  118. expect(getCapturedZCodeCuaBrokerCredentials()).toEqual({
  119. socket: "/tmp/broker-b.sock",
  120. pluginAuthority: "authority-b",
  121. });
  122. } finally {
  123. resetCapturedZCodeCuaBrokerCredentialsForTest();
  124. }
  125. });
  126. it("captures Agent OTLP config privately and never passes auth or identity through to tools", () => {
  127. resetCapturedZCodeAgentTelemetryEnvForTest();
  128. const sourceEnv = {
  129. OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://arms.example.com/v1/traces",
  130. OTEL_EXPORTER_OTLP_TRACES_HEADERS: "Authorization=secret",
  131. OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: "https://arms.example.com/v1/metrics",
  132. OTEL_EXPORTER_OTLP_METRICS_HEADERS: "Authorization=metric-secret",
  133. PATH: "/usr/bin:/bin",
  134. ZCODE_TELEMETRY_USER_SUBJECT_ID: "opaque-user",
  135. };
  136. expect(sanitizeZCodeRuntimeEnv(sourceEnv)).toEqual({ PATH: "/usr/bin:/bin" });
  137. expect(getCapturedZCodeAgentTelemetryEnv()).toMatchObject({
  138. OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://arms.example.com/v1/traces",
  139. OTEL_EXPORTER_OTLP_TRACES_HEADERS: "Authorization=secret",
  140. OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: "https://arms.example.com/v1/metrics",
  141. OTEL_EXPORTER_OTLP_METRICS_HEADERS: "Authorization=metric-secret",
  142. ZCODE_TELEMETRY_USER_SUBJECT_ID: "opaque-user",
  143. });
  144. const toolEnv = readZCodeToolEnvPassthroughEnv(buildZCodeToolEnvPassthroughEnv(sourceEnv));
  145. expect(toolEnv).not.toHaveProperty("OTEL_EXPORTER_OTLP_TRACES_HEADERS");
  146. expect(toolEnv).not.toHaveProperty("OTEL_EXPORTER_OTLP_METRICS_HEADERS");
  147. expect(toolEnv).not.toHaveProperty("ZCODE_TELEMETRY_USER_SUBJECT_ID");
  148. });
  149. it("extracts only the allowlisted Agent telemetry env", () => {
  150. expect(
  151. readZCodeAgentTelemetryEnv({
  152. OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: " https://arms.example.com/v1/traces ",
  153. OTEL_UNKNOWN_SECRET: "must-not-pass",
  154. PATH: "/bin",
  155. ZCODE_MODEL_TELEMETRY_ENABLED: "1",
  156. ZCODE_TELEMETRY_RUNTIME_DISTRIBUTION: " packaged ",
  157. }),
  158. ).toEqual({
  159. OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://arms.example.com/v1/traces",
  160. ZCODE_MODEL_TELEMETRY_ENABLED: "1",
  161. ZCODE_TELEMETRY_RUNTIME_DISTRIBUTION: "packaged",
  162. });
  163. });
  164. });