telemetryPrivacy.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { describe, expect, it } from "vitest";
  2. import { sanitizeTelemetryEventDetail } from "@zcode/shared";
  3. describe("telemetry privacy boundary", () => {
  4. it.each([
  5. [
  6. "https://user:secret@LOGIN.Example.com:8443/authorize?token=secret#state",
  7. "login.example.com",
  8. ],
  9. ["http://127.0.0.1:3000/login?state=secret", "127.0.0.1"],
  10. ["https://[::1]:3000/login?state=secret", "[::1]"],
  11. ["login.example.com", "login.example.com"],
  12. ["file:///Users/private/key", ""],
  13. ["javascript:secret", ""],
  14. ["not a url", ""],
  15. ["", ""],
  16. ])("login_url 只保留 hostname:%s", (value, expected) => {
  17. const detail = { login_url: value };
  18. const safe = sanitizeTelemetryEventDetail("app_login_ck", detail);
  19. expect(safe).toEqual({ login_url: expected });
  20. expect(sanitizeTelemetryEventDetail("app_login_ck", safe)).toEqual(safe);
  21. expect(detail.login_url).toBe(value);
  22. });
  23. it.each([
  24. "Authorization: Bearer FAKE_SECRET",
  25. '{"Authorization":"Basic FAKE_SECRET","api_key":"FAKE_SECRET"}',
  26. "https://user:secret@example.com/private?token=FAKE_SECRET#secret",
  27. "C:\\Users\\private person\\secret.txt",
  28. "/Users/private person/secret.txt",
  29. "/home/private/secret.txt",
  30. "\\\\host\\private\\secret.txt",
  31. "~/.ssh/id_rsa",
  32. "-----BEGIN PRIVATE KEY-----\nFAKE_SECRET\n-----END PRIVATE KEY-----",
  33. "unknown-format-secret-without-any-keyword",
  34. "ordinary error",
  35. " ",
  36. ])("所有非空错误原文都丢弃:%s", (error) => {
  37. expect(sanitizeTelemetryEventDetail("future_event", { error_msg: error })).toEqual({
  38. error_msg: "[redacted]",
  39. });
  40. });
  41. it("保留空值、缺失、分类字段及调用方原对象", () => {
  42. const detail = Object.freeze({
  43. error_msg: "secret",
  44. error_type: "TOOL_EXEC_ERROR",
  45. status: "fail",
  46. });
  47. const safe = sanitizeTelemetryEventDetail("agent_step", detail);
  48. expect(safe).toEqual({ ...detail, error_msg: "[redacted]" });
  49. expect(detail.error_msg).toBe("secret");
  50. expect(sanitizeTelemetryEventDetail("agent_step", safe)).toEqual(safe);
  51. expect(sanitizeTelemetryEventDetail("agent_step", { error_msg: "" })).toEqual({
  52. error_msg: "",
  53. });
  54. expect(sanitizeTelemetryEventDetail("session_create", { create_source: "session" })).toEqual({
  55. create_source: "session",
  56. });
  57. });
  58. });