telemetryRedaction.test.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. OFFICIAL_GLM_MODEL_IDS,
  4. TELEMETRY_SAFE_BUILTIN_MODEL_IDS,
  5. redactTelemetryText,
  6. redactTelemetryUrl,
  7. resolveTelemetryProviderScope,
  8. resolveTelemetryModelId,
  9. sanitizeTelemetryModelValue,
  10. } from "@zcode/shared";
  11. describe("redactTelemetryText", () => {
  12. it("把 POSIX 绝对路径归一为占位符,不保留用户名", () => {
  13. const redacted = redactTelemetryText(
  14. "ENOENT: no such file or directory, open '/Users/alice/project/src/index.ts'",
  15. );
  16. expect(redacted).not.toContain("alice");
  17. expect(redacted).not.toContain("/Users/");
  18. expect(redacted).toContain("{path}");
  19. });
  20. it("覆盖 /home、/root 与 macOS 临时目录", () => {
  21. const redacted = redactTelemetryText(
  22. "read /home/bob/.zcode failed; tmp /var/folders/xy/zz/T/zcode-123 missing",
  23. );
  24. expect(redacted).not.toContain("bob");
  25. expect(redacted).not.toContain("/var/folders");
  26. });
  27. it("把 Windows 盘符路径归一为占位符", () => {
  28. const redacted = redactTelemetryText(String.raw`EPERM C:\Users\carol\AppData\zcode\db.sqlite`);
  29. expect(redacted).not.toContain("carol");
  30. expect(redacted).toContain("{path}");
  31. });
  32. it("把邮箱归一为占位符", () => {
  33. expect(redactTelemetryText("login failed for dave@example.com")).toContain("{email}");
  34. expect(redactTelemetryText("login failed for dave@example.com")).not.toContain("dave@");
  35. });
  36. it("移除 URL 的 query 与 fragment,只保留 host 与归一化路由", () => {
  37. const redacted = redactTelemetryText(
  38. "GET https://api.example.com/v1/sessions/0f8fad5b-d9cb-469f-a165-70867728950e?token=abc#frag failed",
  39. );
  40. expect(redacted).not.toContain("token=abc");
  41. expect(redacted).not.toContain("0f8fad5b");
  42. expect(redacted).toContain("https://api.example.com/v1/sessions/{segment}");
  43. });
  44. it("遮盖 Authorization、Bearer 与赋值形态的凭据", () => {
  45. const redacted = redactTelemetryText(
  46. `authorization: Bearer abc.def.ghi, api_key="topsecretvalue", password=hunter2`,
  47. );
  48. expect(redacted).not.toContain("abc.def.ghi");
  49. expect(redacted).not.toContain("topsecretvalue");
  50. expect(redacted).not.toContain("hunter2");
  51. });
  52. it("遮盖常见密钥字面量", () => {
  53. const redacted = redactTelemetryText(
  54. "keys: sk-abcdefghijklmnopqrstuvwx ghp_abcdefghijklmnopqrstu AKIAIOSFODNN7EXAMPLE",
  55. );
  56. expect(redacted).not.toContain("sk-abcdefghijklmnopqrstuvwx");
  57. expect(redacted).not.toContain("ghp_abcdefghijklmnopqrstu");
  58. expect(redacted).not.toContain("AKIAIOSFODNN7EXAMPLE");
  59. });
  60. it("按 maxLength 截断并保留头部", () => {
  61. const redacted = redactTelemetryText(`HEAD ${"x".repeat(5_000)}`, { maxLength: 100 });
  62. expect(redacted.length).toBe(100);
  63. expect(redacted.startsWith("HEAD ")).toBe(true);
  64. });
  65. it("空值与非字符串输入返回空串", () => {
  66. expect(redactTelemetryText("")).toBe("");
  67. expect(redactTelemetryText(undefined)).toBe("");
  68. });
  69. it("不改写普通诊断文本", () => {
  70. expect(redactTelemetryText("Cannot read properties of undefined (reading 'id')")).toBe(
  71. "Cannot read properties of undefined (reading 'id')",
  72. );
  73. });
  74. });
  75. describe("redactTelemetryUrl", () => {
  76. it("丢弃 query 与 fragment", () => {
  77. expect(redactTelemetryUrl("https://zcode.z.ai/api/v1/event/report?a=1&b=2#x")).toBe(
  78. "https://zcode.z.ai/api/v1/event/report",
  79. );
  80. });
  81. it("把 UUID、长 hash 与邮箱路由段归一", () => {
  82. expect(
  83. redactTelemetryUrl("https://h.test/u/eve@example.com/sessions/12345678901234567890"),
  84. ).toBe("https://h.test/u/{segment}/sessions/{segment}");
  85. });
  86. it("file:// 与本地路径归一为 local_file,不暴露目录", () => {
  87. expect(redactTelemetryUrl("file:///Users/frank/app/index.html")).toBe("local_file");
  88. expect(redactTelemetryUrl("/Users/frank/app/index.html")).toBe("local_file");
  89. });
  90. it("blob 与 data URL 只保留协议标记", () => {
  91. expect(redactTelemetryUrl("blob:file:///abc-def")).toBe("blob");
  92. expect(redactTelemetryUrl("data:image/png;base64,AAAA")).toBe("data");
  93. });
  94. it("无法解析时返回 unknown,不回退到原值", () => {
  95. expect(redactTelemetryUrl("!!!")).toBe("unknown");
  96. expect(redactTelemetryUrl("")).toBe("unknown");
  97. });
  98. });
  99. describe("resolveTelemetryProviderScope", () => {
  100. it("内置 provider 保留稳定 ID", () => {
  101. expect(resolveTelemetryProviderScope("account:zai-individual-coding-plan")).toEqual({
  102. providerId: "account:zai-individual-coding-plan",
  103. providerScope: "builtin",
  104. });
  105. });
  106. it("自定义 provider 归一为 custom,不泄露用户命名", () => {
  107. const resolved = resolveTelemetryProviderScope("我的内网代理");
  108. expect(resolved).toEqual({ providerId: "custom", providerScope: "custom" });
  109. });
  110. it("缺失 provider 时 scope 为 unknown", () => {
  111. expect(resolveTelemetryProviderScope(undefined)).toEqual({
  112. providerId: "",
  113. providerScope: "unknown",
  114. });
  115. });
  116. it("旧报表身份 builtin:* 是 ZCode 固定 ID,判为内置并原样保留", () => {
  117. // supervisor 投影 /report detail 时会把 account:* 映射成这些旧身份,plan_ttft / perf_ui_* 复用同一份 detail。
  118. for (const legacyId of [
  119. "builtin:zai",
  120. "builtin:bigmodel",
  121. "builtin:zai-coding-plan",
  122. "builtin:bigmodel-coding-plan",
  123. "builtin:zai-start-plan",
  124. "builtin:bigmodel-start-plan",
  125. ]) {
  126. expect(resolveTelemetryProviderScope(legacyId)).toEqual({
  127. providerId: legacyId,
  128. providerScope: "builtin",
  129. });
  130. }
  131. });
  132. it("未知的 builtin: 前缀不能借前缀混入内置", () => {
  133. expect(resolveTelemetryProviderScope("builtin:my-proxy")).toEqual({
  134. providerId: "custom",
  135. providerScope: "custom",
  136. });
  137. });
  138. });
  139. describe("TELEMETRY_SAFE_BUILTIN_MODEL_IDS", () => {
  140. it("覆盖官方 GLM 模型名单全部条目,旗舰模型不会被降级为 custom", () => {
  141. for (const id of OFFICIAL_GLM_MODEL_IDS) {
  142. expect(TELEMETRY_SAFE_BUILTIN_MODEL_IDS.has(id.toLowerCase())).toBe(true);
  143. }
  144. expect(TELEMETRY_SAFE_BUILTIN_MODEL_IDS.has("glm-5.3")).toBe(true);
  145. expect(TELEMETRY_SAFE_BUILTIN_MODEL_IDS.has("glm-5.3-flash")).toBe(true);
  146. });
  147. });
  148. describe("resolveTelemetryModelId", () => {
  149. it("内置 provider 下命中白名单的模型原样保留(小写)", () => {
  150. expect(resolveTelemetryModelId("builtin", "GLM-4.6")).toBe("glm-4.6");
  151. });
  152. it("内置 provider 下未命中白名单的模型降级为 custom", () => {
  153. expect(resolveTelemetryModelId("builtin", "glm-未来型号")).toBe("custom");
  154. });
  155. it("自定义 provider 的模型固定写 custom", () => {
  156. expect(resolveTelemetryModelId("custom", "internal-llm-v3")).toBe("custom");
  157. });
  158. it("scope 未知或模型缺失时留空", () => {
  159. expect(resolveTelemetryModelId("unknown", "glm-4.6")).toBe("");
  160. expect(resolveTelemetryModelId("builtin", undefined)).toBe("");
  161. });
  162. it("内置 provider 下的复合值与 custom: 编码值先剥出裸模型 ID 再查白名单", () => {
  163. expect(resolveTelemetryModelId("builtin", "builtin:zai-start-plan/GLM-5.3")).toBe("glm-5.3");
  164. expect(resolveTelemetryModelId("builtin", "custom:builtin%3Azai-start-plan:glm-5.3")).toBe(
  165. "glm-5.3",
  166. );
  167. expect(resolveTelemetryModelId("builtin", "builtin:zai-start-plan/private-model")).toBe(
  168. "custom",
  169. );
  170. });
  171. });
  172. describe("sanitizeTelemetryModelValue", () => {
  173. it("白名单内置模型原样保留", () => {
  174. expect(sanitizeTelemetryModelValue("glm-4.6")).toBe("glm-4.6");
  175. expect(sanitizeTelemetryModelValue("GLM-4.6")).toBe("glm-4.6");
  176. });
  177. it("未命中白名单的裸模型 ID 降级为 custom", () => {
  178. expect(sanitizeTelemetryModelValue("some-internal-model")).toBe("custom");
  179. });
  180. it("custom: 编码值不泄露 provider 名与模型名", () => {
  181. const value = sanitizeTelemetryModelValue("custom:%E5%86%85%E7%BD%91:secret-model");
  182. expect(value).toBe("custom");
  183. });
  184. it("provider/model 复合值按 provider 判定", () => {
  185. expect(sanitizeTelemetryModelValue("account:zai-start-plan/glm-4.6")).toBe("glm-4.6");
  186. expect(sanitizeTelemetryModelValue("my-proxy/secret-model")).toBe("custom");
  187. });
  188. it("supervisor 投影出的旧身份复合值与 custom: 编码值都保留白名单模型", () => {
  189. expect(sanitizeTelemetryModelValue("builtin:zai-start-plan/GLM-5.3")).toBe("glm-5.3");
  190. expect(sanitizeTelemetryModelValue("custom:builtin%3Azai-start-plan:glm-5.3")).toBe("glm-5.3");
  191. });
  192. it("空值保持空,便于与既有留空口径一致", () => {
  193. expect(sanitizeTelemetryModelValue("")).toBe("");
  194. expect(sanitizeTelemetryModelValue(undefined)).toBe("");
  195. expect(sanitizeTelemetryModelValue(" ")).toBe("");
  196. });
  197. });