telemetryRedaction.test.ts 8.2 KB

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