runtimeEnv.test.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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("ZCODE_CUA_DEV_MODE is the one-knob dev bundle and implies the internal feature flag", () => {
  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. // DEV_MODE implies the internal feature flag without needing ZCODE_CUA_PRODUCT_HELPER,
  36. // collapsing the historical multi-var dev incantation to one knob.
  37. expect(isZCodeCuaInternalFeatureEnabled({ ZCODE_CUA_DEV_MODE: "1" })).toBe(true);
  38. expect(isZCodeCuaInternalFeatureEnabled({ ZCODE_CUA_PRODUCT_HELPER: "1" })).toBe(true);
  39. expect(isZCodeCuaInternalFeatureEnabled({})).toBe(true);
  40. });
  41. it("sanitizes runtime env while sealing user network env for tools", () => {
  42. const sourceEnv = {
  43. HTTP_PROXY: "http://proxy.example:8080",
  44. NODE_ENV: "development",
  45. NODE_EXTRA_CA_CERTS: "/tmp/root-ca.pem",
  46. PATH: "/usr/bin:/bin",
  47. npm_config_proxy: "http://npm-proxy.example:8080",
  48. };
  49. const sanitized = sanitizeZCodeRuntimeEnv(sourceEnv);
  50. const passthroughEnv = buildZCodeToolEnvPassthroughEnv(sourceEnv);
  51. const captured = readZCodeToolEnvPassthroughEnv(passthroughEnv);
  52. expect(sanitized).toEqual({ PATH: "/usr/bin:/bin" });
  53. expect(passthroughEnv[ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY]).toBeDefined();
  54. expect(captured).toEqual({
  55. HTTP_PROXY: "http://proxy.example:8080",
  56. NODE_EXTRA_CA_CERTS: "/tmp/root-ca.pem",
  57. npm_config_proxy: "http://npm-proxy.example:8080",
  58. });
  59. });
  60. it("ignores invalid passthrough JSON and non-network keys", () => {
  61. const captured = readZCodeToolEnvPassthroughEnv({
  62. [ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY]: JSON.stringify({
  63. HTTP_PROXY: "http://proxy.example:8080",
  64. NODE_ENV: "production",
  65. PATH: "/tmp/bin",
  66. }),
  67. });
  68. expect(captured).toEqual({
  69. HTTP_PROXY: "http://proxy.example:8080",
  70. });
  71. expect(
  72. readZCodeToolEnvPassthroughEnv({
  73. [ZCODE_TOOL_ENV_PASSTHROUGH_ENV_KEY]: "not-json",
  74. }),
  75. ).toEqual({});
  76. });
  77. it("strips the CUA broker socket/authority/refresh marker from child env and tool passthrough", () => {
  78. // Confused-deputy 防护:broker bearer 凭据只该定向注入目标 zcode-cua server,绝不能随全局 env
  79. // 泄漏给其它 MCP / Bash / tool 子进程,也不能经 tool-env-passthrough 恢复。
  80. const sourceEnv = {
  81. PATH: "/usr/bin:/bin",
  82. ZCODE_CUA_PERMISSION_BROKER_SOCKET: "/tmp/zcode-cua-me/broker-abc.sock",
  83. ZCODE_CUA_PERMISSION_BROKER_REFRESH_MARKER:
  84. "/tmp/zcode-cua-me/broker-abc.sock.permission-refresh.json",
  85. };
  86. const sanitized = sanitizeZCodeRuntimeEnv(sourceEnv);
  87. expect(sanitized).toEqual({ PATH: "/usr/bin:/bin" });
  88. const captured = readZCodeToolEnvPassthroughEnv(buildZCodeToolEnvPassthroughEnv(sourceEnv));
  89. expect(captured).not.toHaveProperty("ZCODE_CUA_PERMISSION_BROKER_SOCKET");
  90. expect(captured).not.toHaveProperty("ZCODE_CUA_PERMISSION_BROKER_SOCKET");
  91. expect(captured).not.toHaveProperty("ZCODE_CUA_PERMISSION_BROKER_REFRESH_MARKER");
  92. });
  93. it("strips desktop-to-remote network handoff values after stdio bootstrap", () => {
  94. const sourceEnv = {
  95. PATH: "/usr/bin:/bin",
  96. [ZCODE_REMOTE_RUNTIME_NETWORK_AUTHORITY_ENV_KEY]: "1",
  97. [ZCODE_REMOTE_HTTP_PROXY_ENV_KEY]: "http://172.21.240.1:7890",
  98. [ZCODE_REMOTE_NO_PROXY_ENV_KEY]: "localhost,127.0.0.1",
  99. };
  100. expect(sanitizeZCodeRuntimeEnv(sourceEnv)).toEqual({ PATH: "/usr/bin:/bin" });
  101. expect(readZCodeToolEnvPassthroughEnv(buildZCodeToolEnvPassthroughEnv(sourceEnv))).toEqual({});
  102. });
  103. it("captures broker credentials as one atomic pair and clears stale state on partial rotation", () => {
  104. resetCapturedZCodeCuaBrokerCredentialsForTest();
  105. try {
  106. sanitizeZCodeRuntimeEnv({
  107. ZCODE_CUA_PERMISSION_BROKER_SOCKET: "/tmp/broker-a.sock",
  108. ZCODE_CUA_PLUGIN_AUTHORITY: "authority-a",
  109. });
  110. expect(getCapturedZCodeCuaBrokerCredentials()).toEqual({
  111. socket: "/tmp/broker-a.sock",
  112. pluginAuthority: "authority-a",
  113. });
  114. sanitizeZCodeRuntimeEnv({
  115. ZCODE_CUA_PERMISSION_BROKER_SOCKET: "/tmp/broker-b.sock",
  116. });
  117. expect(getCapturedZCodeCuaBrokerCredentials()).toEqual({
  118. socket: undefined,
  119. pluginAuthority: undefined,
  120. });
  121. sanitizeZCodeRuntimeEnv({
  122. ZCODE_CUA_PERMISSION_BROKER_SOCKET: "/tmp/broker-b.sock",
  123. ZCODE_CUA_PLUGIN_AUTHORITY: "authority-b",
  124. });
  125. expect(getCapturedZCodeCuaBrokerCredentials()).toEqual({
  126. socket: "/tmp/broker-b.sock",
  127. pluginAuthority: "authority-b",
  128. });
  129. } finally {
  130. resetCapturedZCodeCuaBrokerCredentialsForTest();
  131. }
  132. });
  133. it("captures Agent OTLP config privately and never passes auth or identity through to tools", () => {
  134. resetCapturedZCodeAgentTelemetryEnvForTest();
  135. const sourceEnv = {
  136. OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://arms.example.com/v1/traces",
  137. OTEL_EXPORTER_OTLP_TRACES_HEADERS: "Authorization=secret",
  138. OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: "https://arms.example.com/v1/metrics",
  139. OTEL_EXPORTER_OTLP_METRICS_HEADERS: "Authorization=metric-secret",
  140. PATH: "/usr/bin:/bin",
  141. ZCODE_TELEMETRY_USER_SUBJECT_ID: "opaque-user",
  142. };
  143. expect(sanitizeZCodeRuntimeEnv(sourceEnv)).toEqual({ PATH: "/usr/bin:/bin" });
  144. expect(getCapturedZCodeAgentTelemetryEnv()).toMatchObject({
  145. OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://arms.example.com/v1/traces",
  146. OTEL_EXPORTER_OTLP_TRACES_HEADERS: "Authorization=secret",
  147. OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: "https://arms.example.com/v1/metrics",
  148. OTEL_EXPORTER_OTLP_METRICS_HEADERS: "Authorization=metric-secret",
  149. ZCODE_TELEMETRY_USER_SUBJECT_ID: "opaque-user",
  150. });
  151. const toolEnv = readZCodeToolEnvPassthroughEnv(buildZCodeToolEnvPassthroughEnv(sourceEnv));
  152. expect(toolEnv).not.toHaveProperty("OTEL_EXPORTER_OTLP_TRACES_HEADERS");
  153. expect(toolEnv).not.toHaveProperty("OTEL_EXPORTER_OTLP_METRICS_HEADERS");
  154. expect(toolEnv).not.toHaveProperty("ZCODE_TELEMETRY_USER_SUBJECT_ID");
  155. });
  156. it("extracts only the allowlisted Agent telemetry env", () => {
  157. expect(
  158. readZCodeAgentTelemetryEnv({
  159. OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: " https://arms.example.com/v1/traces ",
  160. OTEL_UNKNOWN_SECRET: "must-not-pass",
  161. PATH: "/bin",
  162. ZCODE_MODEL_TELEMETRY_ENABLED: "1",
  163. ZCODE_TELEMETRY_RUNTIME_DISTRIBUTION: " packaged ",
  164. }),
  165. ).toEqual({
  166. OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://arms.example.com/v1/traces",
  167. ZCODE_MODEL_TELEMETRY_ENABLED: "1",
  168. ZCODE_TELEMETRY_RUNTIME_DISTRIBUTION: "packaged",
  169. });
  170. });
  171. });