2026-06-28 18:57:53 +08:00
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
|
import { sanitizeEventProperties } from "../../../apps/api/src/lib/analytics-allowlist.js";
|
|
|
|
|
|
|
|
|
|
describe("sanitizeEventProperties", () => {
|
2026-07-17 09:51:48 +08:00
|
|
|
it("keeps the enriched allow-listed keys for tool_used and drops free-text/PII", () => {
|
2026-06-28 18:57:53 +08:00
|
|
|
const out = sanitizeEventProperties("tool_used", {
|
|
|
|
|
tool_id: "resize",
|
2026-07-17 09:51:48 +08:00
|
|
|
status: "failed",
|
2026-06-28 18:57:53 +08:00
|
|
|
duration_ms: 12,
|
|
|
|
|
category: "image",
|
|
|
|
|
is_ai_tool: false,
|
2026-07-17 09:51:48 +08:00
|
|
|
is_batch: true,
|
|
|
|
|
input_format: "heic",
|
|
|
|
|
output_format: "png",
|
|
|
|
|
bytes_in: 4096,
|
|
|
|
|
bytes_out: 2048,
|
|
|
|
|
execution_hint: "fast",
|
|
|
|
|
error_kind: "input",
|
|
|
|
|
error_code: "corrupt-header",
|
2026-06-28 18:57:53 +08:00
|
|
|
error_message: "stack with /uploads/secret.docx",
|
|
|
|
|
params: { watermark_text: "CONFIDENTIAL" },
|
|
|
|
|
});
|
|
|
|
|
expect(out).toEqual({
|
|
|
|
|
tool_id: "resize",
|
2026-07-17 09:51:48 +08:00
|
|
|
status: "failed",
|
2026-06-28 18:57:53 +08:00
|
|
|
duration_ms: 12,
|
|
|
|
|
category: "image",
|
|
|
|
|
is_ai_tool: false,
|
2026-07-17 09:51:48 +08:00
|
|
|
is_batch: true,
|
|
|
|
|
input_format: "heic",
|
|
|
|
|
output_format: "png",
|
|
|
|
|
bytes_in: 4096,
|
|
|
|
|
bytes_out: 2048,
|
|
|
|
|
execution_hint: "fast",
|
|
|
|
|
error_kind: "input",
|
|
|
|
|
error_code: "corrupt-header",
|
2026-06-28 18:57:53 +08:00
|
|
|
});
|
|
|
|
|
expect(out).not.toHaveProperty("error_message");
|
|
|
|
|
expect(out).not.toHaveProperty("params");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("drops non-primitive values", () => {
|
|
|
|
|
const out = sanitizeEventProperties("ai_bundle_action", {
|
|
|
|
|
bundle_id: "ocr",
|
|
|
|
|
action: "installed",
|
|
|
|
|
duration_ms: { nested: 1 } as unknown as number,
|
|
|
|
|
});
|
|
|
|
|
expect(out).toEqual({ bundle_id: "ocr", action: "installed" });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("returns no properties for an unknown event", () => {
|
|
|
|
|
const out = sanitizeEventProperties("totally_new_event", { anything: "x" });
|
|
|
|
|
expect(out).toEqual({});
|
|
|
|
|
});
|
2026-07-13 14:23:16 +08:00
|
|
|
|
|
|
|
|
it("keeps only allow-listed keys for instance_started", () => {
|
|
|
|
|
const out = sanitizeEventProperties("instance_started", {
|
|
|
|
|
arch: "arm64",
|
|
|
|
|
os_platform: "linux",
|
|
|
|
|
deploy_mode: "embedded",
|
|
|
|
|
gpu_present: false,
|
2026-07-30 09:40:53 +08:00
|
|
|
app_version: "2.2.0",
|
2026-07-13 14:23:16 +08:00
|
|
|
hostname: "leaked-hostname",
|
|
|
|
|
});
|
|
|
|
|
expect(out).toEqual({
|
|
|
|
|
arch: "arm64",
|
|
|
|
|
os_platform: "linux",
|
|
|
|
|
deploy_mode: "embedded",
|
|
|
|
|
gpu_present: false,
|
2026-07-30 09:40:53 +08:00
|
|
|
app_version: "2.2.0",
|
2026-07-13 14:23:16 +08:00
|
|
|
});
|
|
|
|
|
expect(out).not.toHaveProperty("hostname");
|
|
|
|
|
});
|
2026-06-28 18:57:53 +08:00
|
|
|
});
|