2026-07-10 21:41:49 +08:00
|
|
|
import { describe, expect, it } from "vitest";
|
2026-08-03 13:13:38 +08:00
|
|
|
import { buildWebBeforeSend, DENY_URLS, IGNORE_ERRORS } from "@/lib/sentry-scrub";
|
2026-07-10 21:41:49 +08:00
|
|
|
|
|
|
|
|
describe("static filter lists", () => {
|
|
|
|
|
it("deny extension frames and ignore noisy network errors", () => {
|
|
|
|
|
expect(DENY_URLS.some((re) => re.test("chrome-extension://abcdef/content.js"))).toBe(true);
|
|
|
|
|
expect(DENY_URLS.some((re) => re.test("moz-extension://abcdef/content.js"))).toBe(true);
|
|
|
|
|
expect(IGNORE_ERRORS).toContain("Failed to fetch");
|
|
|
|
|
expect(IGNORE_ERRORS).toContain("Load failed");
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("buildWebBeforeSend", () => {
|
2026-07-13 14:23:16 +08:00
|
|
|
const baseEvent = (over: Record<string, any> = {}): Record<string, any> => ({
|
2026-07-10 21:41:49 +08:00
|
|
|
request: { url: "http://192.168.0.4:1349/image/resize" },
|
|
|
|
|
breadcrumbs: [{}],
|
|
|
|
|
user: { id: "x" },
|
|
|
|
|
contexts: { react: { componentStack: "at ToolPage" }, device: { name: "leak" } },
|
|
|
|
|
exception: {
|
|
|
|
|
values: [
|
|
|
|
|
{
|
|
|
|
|
type: "TypeError",
|
|
|
|
|
value: "secret /Users/a/b",
|
|
|
|
|
stacktrace: { frames: [{ filename: "http://192.168.0.4:1349/assets/app.js" }] },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
tags: { tool_id: "resize", drop_me: "x" },
|
2026-07-13 14:23:16 +08:00
|
|
|
...over,
|
2026-07-10 21:41:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("gates, strips, keeps react componentStack, redacts values", () => {
|
|
|
|
|
const send = buildWebBeforeSend(() => true);
|
|
|
|
|
const out = send(baseEvent(), { originalException: new TypeError("boom /Users/a/b") })!;
|
|
|
|
|
expect(out.request).toBeUndefined();
|
|
|
|
|
expect(out.user).toBeUndefined();
|
|
|
|
|
expect(out.contexts.react.componentStack).toBe("at ToolPage");
|
|
|
|
|
expect(out.contexts.device).toBeUndefined();
|
|
|
|
|
expect(out.exception.values[0].value).toBe("boom <path>");
|
|
|
|
|
expect(out.exception.values[0].stacktrace.frames[0].filename).toBe("/assets/app.js");
|
|
|
|
|
expect(out.tags.tool_id).toBe("resize");
|
|
|
|
|
expect(out.tags.drop_me).toBeUndefined();
|
|
|
|
|
expect(buildWebBeforeSend(() => false)(baseEvent(), {})).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-17 09:51:48 +08:00
|
|
|
it("keeps the breadcrumb trail, redacting urls but keeping safe fetch status/method", () => {
|
2026-07-13 14:23:16 +08:00
|
|
|
const send = buildWebBeforeSend(() => true);
|
|
|
|
|
const out = send(
|
|
|
|
|
baseEvent({
|
|
|
|
|
breadcrumbs: [
|
2026-07-17 09:51:48 +08:00
|
|
|
{
|
|
|
|
|
message: "fetch https://host/user.png",
|
|
|
|
|
category: "fetch",
|
|
|
|
|
data: { url: "https://host/user.png", status_code: 500, method: "POST" },
|
|
|
|
|
},
|
2026-07-13 14:23:16 +08:00
|
|
|
{ message: "open /Users/a/secret.pdf", category: "console", level: "warning" },
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
{},
|
|
|
|
|
)!;
|
|
|
|
|
expect(out.breadcrumbs).toEqual([
|
2026-07-17 09:51:48 +08:00
|
|
|
{ message: "fetch <url>", category: "fetch", data: { status_code: 500, method: "POST" } },
|
2026-07-13 14:23:16 +08:00
|
|
|
{ message: "open <path>", category: "console", level: "warning" },
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-08-03 13:13:38 +08:00
|
|
|
it("keeps a redacted message for a non-native error", () => {
|
2026-07-10 21:41:49 +08:00
|
|
|
const send = buildWebBeforeSend(() => true);
|
|
|
|
|
const custom = Object.assign(new Error("user secret"), { name: "WeirdLibError" });
|
|
|
|
|
const out = send(baseEvent(), { originalException: custom })!;
|
2026-08-03 13:13:38 +08:00
|
|
|
expect(out.exception.values[0].value).toBe("user secret");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("keeps a redacted message for a non-native app error", () => {
|
|
|
|
|
const send = buildWebBeforeSend(() => true);
|
|
|
|
|
const out = send(baseEvent(), {
|
|
|
|
|
originalException: new Error("upload failed for report.pdf"),
|
|
|
|
|
})!;
|
|
|
|
|
expect(out.exception.values[0].value).toBe("upload failed for <file>");
|
2026-07-10 21:41:49 +08:00
|
|
|
});
|
|
|
|
|
|
2026-07-13 14:23:16 +08:00
|
|
|
it("enforces the 500-per-hour ceiling", () => {
|
2026-07-10 21:41:49 +08:00
|
|
|
const send = buildWebBeforeSend(() => true);
|
2026-07-13 14:23:16 +08:00
|
|
|
for (let i = 0; i < 500; i++) expect(send(baseEvent(), {})).not.toBeNull();
|
2026-07-10 21:41:49 +08:00
|
|
|
expect(send(baseEvent(), {})).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("never throws on malformed events", () => {
|
|
|
|
|
const send = buildWebBeforeSend(() => true);
|
|
|
|
|
expect(() => send({} as Record<string, any>, {})).not.toThrow();
|
|
|
|
|
expect(() => send({ exception: { values: null } } as any, {})).not.toThrow();
|
|
|
|
|
});
|
|
|
|
|
});
|