telemetry.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. mobilePluginDiscoveryTelemetrySchema,
  4. resolveSafeTelemetryHostname,
  5. sanitizeTelemetrySkillName,
  6. sanitizeTelemetryEventDetail,
  7. } from "../src/telemetry.js";
  8. describe("resolveSafeTelemetryHostname", () => {
  9. it("只从 HTTP(S) URL 提取小写 hostname", () => {
  10. expect(
  11. resolveSafeTelemetryHostname("https://user:secret@API.Example.COM:8443/private?q=jwt"),
  12. ).toBe("api.example.com");
  13. expect(resolveSafeTelemetryHostname("http://127.0.0.1:64411")).toBe("127.0.0.1");
  14. });
  15. it.each(["", "api.example.com", "file:///private/path", "not a url"])(
  16. "拒绝非 URL 或非 HTTP(S) 值:%s",
  17. (value) => {
  18. expect(resolveSafeTelemetryHostname(value)).toBe("");
  19. },
  20. );
  21. });
  22. describe("Skill 遥测名称隐私边界", () => {
  23. it.each(["pptx", "control-browser", "demo_skill", "a".repeat(64)])("保留合法标识 %s", (name) => {
  24. expect(sanitizeTelemetrySkillName(name)).toBe(name);
  25. });
  26. it.each([
  27. "/Users/FAKE_USER/private",
  28. "C:\\private\\key",
  29. "https://example.com/?token=FAKE",
  30. "demo\nFAKE_SECRET",
  31. " demo",
  32. "demo ",
  33. "document-skills:pptx",
  34. "user@example.com",
  35. "sk-" + "a".repeat(49),
  36. "ghp_" + "a".repeat(36),
  37. "AKIA" + "A".repeat(16),
  38. "a".repeat(65),
  39. "a".repeat(5000),
  40. "",
  41. 123,
  42. null,
  43. ])("拒绝不受控名称 %#", (name) => {
  44. expect(sanitizeTelemetrySkillName(name)).toBe("unknown");
  45. if (typeof name === "string") {
  46. const detail = { tool_name: "Skill", skill_name: name };
  47. const safe = sanitizeTelemetryEventDetail("agent_step", detail);
  48. expect(safe.skill_name).toBe("unknown");
  49. expect(detail.skill_name).toBe(name);
  50. expect(sanitizeTelemetryEventDetail("agent_step", safe)).toEqual(safe);
  51. }
  52. });
  53. });
  54. describe("plugin telemetry privacy", () => {
  55. it("drops arbitrary input and preserves only bounded plugin dimensions", () => {
  56. expect(
  57. sanitizeTelemetryEventDetail("plugin_lifecycle", {
  58. action: "install",
  59. result: "success",
  60. plugin_id: "demo@market",
  61. schema_version: "1",
  62. options: "secret",
  63. token: "secret",
  64. workspacePath: "/private",
  65. operation_id: "extra",
  66. source_type: "https://private",
  67. duration_ms: "12",
  68. }),
  69. ).toEqual({
  70. action: "install",
  71. result: "success",
  72. plugin_id: "demo@market",
  73. schema_version: "1",
  74. duration_ms: "12",
  75. });
  76. });
  77. it("rejects obsolete started results and path-like identifiers", () => {
  78. expect(
  79. sanitizeTelemetryEventDetail("plugin_lifecycle", {
  80. result: "started",
  81. plugin_id: "/private/demo",
  82. marketplace: "https://private",
  83. }),
  84. ).toEqual({});
  85. });
  86. });
  87. describe("mobile plugin discovery schema", () => {
  88. const event = {
  89. elementName: "plugin_discovery",
  90. eventRegion: "app",
  91. eventType: "view",
  92. context: { clientTimezone: "UTC", clientLanguage: "en-US", screenResolution: "1x1" },
  93. eventExtraDetail: {
  94. schema_version: "1",
  95. action: "detail_view",
  96. entry: "store_detail",
  97. client_kind: "mobile",
  98. plugin_id: "a@market",
  99. },
  100. };
  101. it("only admits discovery with bounded fields", () => {
  102. expect(mobilePluginDiscoveryTelemetrySchema.safeParse(event).success).toBe(true);
  103. expect(
  104. mobilePluginDiscoveryTelemetrySchema.safeParse({ ...event, elementName: "agent_step" })
  105. .success,
  106. ).toBe(false);
  107. expect(
  108. mobilePluginDiscoveryTelemetrySchema.safeParse({
  109. ...event,
  110. eventExtraDetail: { ...event.eventExtraDetail, token: "secret" },
  111. }).success,
  112. ).toBe(false);
  113. });
  114. });
  115. it("rejects private plugin attribution on existing agent steps", () => {
  116. expect(
  117. sanitizeTelemetryEventDetail("agent_step", {
  118. plugin_id: "/private/plugin",
  119. plugin_component: "mcp",
  120. status: "success",
  121. }),
  122. ).toEqual({ plugin_component: "mcp", status: "success" });
  123. });