armsEventRedaction.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { describe, expect, it } from "vitest";
  2. import { redactArmsEventBatch } from "../src/main/armsEventRedaction.js";
  3. function clickEvent(overrides: Record<string, unknown> = {}): Record<string, unknown> {
  4. return {
  5. event_type: "click",
  6. type: "click",
  7. name: "click on button: 重构 src/auth 的登录流程...",
  8. target_name: "div[1]/div[3]/button[2]",
  9. duration: 12,
  10. snapshots: JSON.stringify({
  11. id: "task-item-8",
  12. className: "task-list-item selected",
  13. src: "file:///Users/alice/project/preview.png",
  14. }),
  15. ...overrides,
  16. };
  17. }
  18. describe("redactArmsEventBatch - click", () => {
  19. it("截掉 name 里的元素文本,只保留 tag 前缀", () => {
  20. const [event] = redactArmsEventBatch([clickEvent()]);
  21. expect(event!.name).toBe("click on button");
  22. });
  23. it("保留 input 的 type 前缀,便于区分控件", () => {
  24. const [event] = redactArmsEventBatch([
  25. clickEvent({ name: "click on checkbox-input: 启用自动提交..." }),
  26. ]);
  27. expect(event!.name).toBe("click on checkbox-input");
  28. });
  29. it("删除 snapshots,避免 href/src 带出本机路径", () => {
  30. const [event] = redactArmsEventBatch([clickEvent()]);
  31. expect(event!.snapshots).toBeUndefined();
  32. });
  33. it("保留 target_name 与耗时等结构化字段", () => {
  34. const [event] = redactArmsEventBatch([clickEvent()]);
  35. expect(event!.target_name).toBe("div[1]/div[3]/button[2]");
  36. expect(event!.duration).toBe(12);
  37. });
  38. it("没有文本后缀的 name 保持原样", () => {
  39. const [event] = redactArmsEventBatch([clickEvent({ name: "click on button" })]);
  40. expect(event!.name).toBe("click on button");
  41. });
  42. it("name 不是预期形态时整体丢弃为固定桶,不原样透传", () => {
  43. const [event] = redactArmsEventBatch([clickEvent({ name: "用户自定义标题:机密项目" })]);
  44. expect(event!.name).toBe("click");
  45. });
  46. });
  47. describe("redactArmsEventBatch - exception", () => {
  48. it("脱敏 message 里的绝对路径与邮箱", () => {
  49. const [event] = redactArmsEventBatch([
  50. {
  51. event_type: "exception",
  52. type: "error",
  53. message: "ENOENT open '/Users/alice/work/secret-project/src/a.ts' for bob@example.com",
  54. },
  55. ]);
  56. expect(event!.message).not.toContain("alice");
  57. expect(event!.message).not.toContain("secret-project");
  58. expect(event!.message).not.toContain("bob@example.com");
  59. });
  60. it("脱敏 stack 中的 file:// 路径", () => {
  61. const [event] = redactArmsEventBatch([
  62. {
  63. event_type: "exception",
  64. type: "error",
  65. stack: "at render (file:///Users/carol/app/index.js:10:5)",
  66. },
  67. ]);
  68. expect(event!.stack).not.toContain("carol");
  69. });
  70. it("脱敏 file 字段里的 Windows 安装目录,不带出本机用户名", () => {
  71. const [event] = redactArmsEventBatch([
  72. {
  73. event_type: "exception",
  74. type: "error",
  75. source: "uncaughtException",
  76. message: "boom",
  77. file: "file:///C:/Users/erin/AppData/Local/Programs/ZCode/resources/app.asar/dist/renderer/assets/index-9f3a.js",
  78. line: 1,
  79. column: 2345,
  80. },
  81. ]);
  82. expect(String(event!.file)).not.toContain("erin");
  83. expect(event!.line).toBe(1);
  84. expect(event!.column).toBe(2345);
  85. });
  86. it("脱敏 whiteScreen 的 snapshots 文本", () => {
  87. const [event] = redactArmsEventBatch([
  88. {
  89. event_type: "exception",
  90. type: "blank",
  91. snapshots: JSON.stringify({ url: "file:///Users/dave/app/index.html", when: 1 }),
  92. },
  93. ]);
  94. expect(String(event!.snapshots)).not.toContain("dave");
  95. });
  96. it("不改写原生 crashReporter dump 事件的 binary_images 判定字段", () => {
  97. const binaryImages = [{ name: "zcode" }];
  98. const [event] = redactArmsEventBatch([
  99. {
  100. event_type: "exception",
  101. type: "crash",
  102. source: "crashReporter",
  103. message: "EXC_BAD_ACCESS",
  104. binary_images: binaryImages,
  105. },
  106. ]);
  107. expect(event!.binary_images).toBe(binaryImages);
  108. });
  109. it("普通诊断消息不被改写", () => {
  110. const [event] = redactArmsEventBatch([
  111. {
  112. event_type: "exception",
  113. type: "error",
  114. message: "Cannot read properties of undefined (reading 'id')",
  115. },
  116. ]);
  117. expect(event!.message).toBe("Cannot read properties of undefined (reading 'id')");
  118. });
  119. });
  120. describe("redactArmsEventBatch - api / resource", () => {
  121. it("丢弃 url 的 query 与 fragment", () => {
  122. const [event] = redactArmsEventBatch([
  123. {
  124. event_type: "resource",
  125. type: "api",
  126. url: "https://zcode.z.ai/api/v1/event/report?state=abc123#x",
  127. name: "https://zcode.z.ai/api/v1/event/report?state=abc123",
  128. },
  129. ]);
  130. expect(event!.url).toBe("https://zcode.z.ai/api/v1/event/report");
  131. expect(event!.name).toBe("https://zcode.z.ai/api/v1/event/report");
  132. });
  133. it("归一化路由里的 UUID 身份段", () => {
  134. const [event] = redactArmsEventBatch([
  135. {
  136. event_type: "resource",
  137. type: "api",
  138. url: "https://zcode.z.ai/api/v1/tasks/0f8fad5b-d9cb-469f-a165-70867728950e/messages",
  139. },
  140. ]);
  141. expect(event!.url).toBe("https://zcode.z.ai/api/v1/tasks/{segment}/messages");
  142. });
  143. it("脱敏 api 事件的错误 message", () => {
  144. const [event] = redactArmsEventBatch([
  145. {
  146. event_type: "resource",
  147. type: "api",
  148. url: "https://zcode.z.ai/api/v1/ping",
  149. message: "connect ECONNREFUSED for /Users/erin/.zcode/socket",
  150. },
  151. ]);
  152. expect(event!.message).not.toContain("erin");
  153. });
  154. it("本地静态资源归一为 local_file", () => {
  155. const [event] = redactArmsEventBatch([
  156. {
  157. event_type: "resource",
  158. type: "img",
  159. url: "file:///Users/frank/app/logo.png",
  160. },
  161. ]);
  162. expect(event!.url).toBe("local_file");
  163. });
  164. });
  165. describe("redactArmsEventBatch - 不影响其他事件", () => {
  166. it("longTask 的归因摘要 properties 保持不变", () => {
  167. const properties = { loaf_top_invoker_type: "user-callback", loaf_top_share_pct: 62 };
  168. const [event] = redactArmsEventBatch([{ event_type: "longTask", duration: 120, properties }]);
  169. expect(event!.properties).toBe(properties);
  170. });
  171. it("自定义事件(perf_*)不被本批次改写", () => {
  172. const input = {
  173. event_type: "custom",
  174. name: "perf_agent_crash",
  175. properties: { error_message: "<workspace>/src/a.ts failed" },
  176. };
  177. const [event] = redactArmsEventBatch([input]);
  178. expect(event!.properties).toEqual({ error_message: "<workspace>/src/a.ts failed" });
  179. });
  180. it("保持批次顺序与条数,空批次安全", () => {
  181. expect(redactArmsEventBatch([])).toEqual([]);
  182. const events = redactArmsEventBatch([
  183. { event_type: "view", type: "perf" },
  184. clickEvent(),
  185. { event_type: "exception", type: "error", message: "boom" },
  186. ]);
  187. expect(events).toHaveLength(3);
  188. expect(events[0]!.type).toBe("perf");
  189. expect(events[1]!.name).toBe("click on button");
  190. expect(events[2]!.message).toBe("boom");
  191. });
  192. });