353 lines
13 KiB
TypeScript
353 lines
13 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
SENTRY_DROP_EXCEPTION_TYPES,
|
|
SENTRY_HEADER_ALLOWLIST,
|
|
createSentryErrorReporter,
|
|
sanitizeSentryEvent,
|
|
type SentryInitOptions,
|
|
type SentryLikeModule,
|
|
} from "./sentry.js";
|
|
|
|
describe("sanitizeSentryEvent", () => {
|
|
it("strips disallowed request headers and keeps the allowlist", () => {
|
|
const result = sanitizeSentryEvent({
|
|
request: {
|
|
url: "https://api.example.com/v1/widgets",
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"User-Agent": "vitest/1.0",
|
|
Authorization: "Bearer leaked",
|
|
"X-API-Key": "leaked",
|
|
"X-Csrf-Token": "csrf",
|
|
"Set-Cookie": "session=abc",
|
|
Cookie: "session=abc",
|
|
Referer: "https://example.com/private?token=secret",
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
const headers = (result?.request as { headers: Record<string, unknown> }).headers;
|
|
expect(Object.keys(headers).map((k) => k.toLowerCase()).sort()).toEqual(["content-type", "user-agent"]);
|
|
expect(headers).not.toHaveProperty("Authorization");
|
|
expect(headers).not.toHaveProperty("X-API-Key");
|
|
expect(headers).not.toHaveProperty("Cookie");
|
|
});
|
|
|
|
it("recursively sanitizes user, extra, contexts, tags, breadcrumbs", () => {
|
|
const result = sanitizeSentryEvent({
|
|
user: { id: "u1", email: "alice@example.com", phone: "+1-555" },
|
|
extra: { token: "raw", payload: { password: "deep" } },
|
|
contexts: { custom: { secret: "leak" } },
|
|
tags: { apiKey: "x" },
|
|
breadcrumbs: [
|
|
{ category: "http", data: { body: { password: "hunter2" } } },
|
|
{ category: "auth", data: { token: "abc" } },
|
|
],
|
|
});
|
|
|
|
expect(result).not.toBeNull();
|
|
expect((result?.user as Record<string, unknown>).email).toBe("[Redacted]");
|
|
expect((result?.user as Record<string, unknown>).phone).toBe("[Redacted]");
|
|
expect((result?.user as Record<string, unknown>).id).toBe("u1");
|
|
|
|
expect((result?.extra as Record<string, unknown>).token).toBe("[Redacted]");
|
|
expect(((result?.extra as { payload: Record<string, unknown> }).payload).password).toBe("[Redacted]");
|
|
|
|
expect(((result?.contexts as { custom: Record<string, unknown> }).custom).secret).toBe("[Redacted]");
|
|
expect((result?.tags as Record<string, unknown>).apiKey).toBe("[Redacted]");
|
|
|
|
const breadcrumbs = result?.breadcrumbs as Array<Record<string, unknown>>;
|
|
expect((breadcrumbs[0]?.data as Record<string, unknown>).body).toBe("[Redacted]");
|
|
expect((breadcrumbs[1]?.data as Record<string, unknown>).token).toBe("[Redacted]");
|
|
});
|
|
|
|
it("drops AbortError and similar non-actionable exceptions", () => {
|
|
expect(
|
|
sanitizeSentryEvent({
|
|
exception: { values: [{ type: "AbortError", value: "client gone" }] },
|
|
})
|
|
).toBeNull();
|
|
|
|
expect(
|
|
sanitizeSentryEvent({
|
|
exception: { values: [{ type: "RequestAbortedError" }] },
|
|
})
|
|
).toBeNull();
|
|
|
|
expect(
|
|
sanitizeSentryEvent({
|
|
exception: { values: [{ type: "TypeError", value: "x.y is undefined" }] },
|
|
})
|
|
).not.toBeNull();
|
|
});
|
|
|
|
it("drops request query strings and URL fragments wholesale", () => {
|
|
const result = sanitizeSentryEvent({
|
|
request: {
|
|
url: "https://api.example.com/x?id=42&token=abc#private",
|
|
query_string: "id=42&token=abc&apiKey=xyz&page=2",
|
|
headers: {},
|
|
},
|
|
});
|
|
|
|
const request = result?.request as { query_string: string; url: string };
|
|
expect(request.query_string).toBe("[Redacted]");
|
|
expect(request.url).toBe("https://api.example.com/x");
|
|
});
|
|
|
|
it("redacts bearer tokens embedded in sensitive route paths", () => {
|
|
const result = sanitizeSentryEvent({
|
|
request: {
|
|
url: "https://app.example.com/invitation/raw-owner-token?source=mail#claim",
|
|
headers: {},
|
|
},
|
|
breadcrumbs: [
|
|
{
|
|
category: "navigation",
|
|
data: {
|
|
from: "/verify/raw-registration-token?request=secret",
|
|
to: "/app/acme/settings/privacy/delete/confirm/raw-delete-token",
|
|
},
|
|
},
|
|
],
|
|
spans: [{ description: "GET /sign/raw-signature-token?utm_source=mail" }],
|
|
});
|
|
|
|
expect((result?.request as { url: string }).url).toBe("https://app.example.com/invitation/redacted");
|
|
expect(result?.breadcrumbs).toEqual([
|
|
{
|
|
category: "navigation",
|
|
data: {
|
|
from: "/verify/redacted",
|
|
to: "[Redacted]",
|
|
},
|
|
},
|
|
]);
|
|
expect(result?.spans).toEqual([{ description: "GET /sign/redacted" }]);
|
|
expect(JSON.stringify(result)).not.toContain("raw-owner-token");
|
|
expect(JSON.stringify(result)).not.toContain("raw-registration-token");
|
|
expect(JSON.stringify(result)).not.toContain("raw-delete-token");
|
|
expect(JSON.stringify(result)).not.toContain("raw-signature-token");
|
|
});
|
|
|
|
it("redacts public and internal customer-portal capability paths everywhere in an event", () => {
|
|
const magicToken = "m".repeat(43);
|
|
const setupToken = "s".repeat(43);
|
|
const verifyToken = "v".repeat(43);
|
|
const resetToken = "r".repeat(43);
|
|
const result = sanitizeSentryEvent({
|
|
transaction: `pageload /__portal/acme/reset-password/${resetToken}?source=mail`,
|
|
request: {
|
|
url: `https://portal.example.test/acme/login/magic/${magicToken}?source=mail#open`,
|
|
headers: {},
|
|
},
|
|
breadcrumbs: [
|
|
{
|
|
category: "navigation",
|
|
message: `opened /acme/setup/${setupToken}`,
|
|
data: { from: `/__portal/acme/verify-email/${verifyToken}?source=mail` },
|
|
},
|
|
],
|
|
spans: [
|
|
{
|
|
name: `navigation /acme/reset-password/${resetToken}`,
|
|
description: `GET /__portal/acme/verify-email/${verifyToken}?source=mail`,
|
|
data: { "url.full": `https://portal.example.test/acme/setup/${setupToken}?source=mail` },
|
|
},
|
|
],
|
|
});
|
|
|
|
expect(result).toMatchObject({
|
|
transaction: "pageload /__portal/acme/reset-password/redacted",
|
|
request: { url: "https://portal.example.test/acme/login/magic/redacted" },
|
|
breadcrumbs: [
|
|
{
|
|
message: "opened /acme/setup/redacted",
|
|
data: { from: "/__portal/acme/verify-email/redacted" },
|
|
},
|
|
],
|
|
spans: [
|
|
{
|
|
name: "navigation /acme/reset-password/redacted",
|
|
description: "GET /__portal/acme/verify-email/redacted",
|
|
data: { "url.full": "https://portal.example.test/acme/setup/redacted" },
|
|
},
|
|
],
|
|
});
|
|
for (const token of [magicToken, setupToken, verifyToken, resetToken]) {
|
|
expect(JSON.stringify(result)).not.toContain(token);
|
|
}
|
|
});
|
|
|
|
it("redacts request.cookies wholesale", () => {
|
|
const result = sanitizeSentryEvent({
|
|
request: { cookies: { session: "raw", csrf: "raw" }, headers: {} },
|
|
});
|
|
|
|
expect((result?.request as { cookies: unknown }).cookies).toBe("[Redacted]");
|
|
});
|
|
|
|
it("redacts request.data via the recursive sanitizer", () => {
|
|
const result = sanitizeSentryEvent({
|
|
request: {
|
|
data: { username: "alice", password: "hunter2", profile: { token: "deep" } },
|
|
headers: {},
|
|
},
|
|
});
|
|
|
|
const data = (result?.request as { data: Record<string, unknown> }).data;
|
|
expect(data.username).toBe("alice");
|
|
expect(data.password).toBe("[Redacted]");
|
|
expect((data.profile as Record<string, unknown>).token).toBe("[Redacted]");
|
|
});
|
|
|
|
it("exposes the canonical header allowlist", () => {
|
|
for (const h of ["content-type", "user-agent", "accept-language", "accept-encoding", "x-request-id"]) {
|
|
expect(SENTRY_HEADER_ALLOWLIST.has(h)).toBe(true);
|
|
}
|
|
expect(SENTRY_HEADER_ALLOWLIST.has("x-forwarded-for")).toBe(false);
|
|
expect(SENTRY_HEADER_ALLOWLIST.has("referer")).toBe(false);
|
|
});
|
|
|
|
it("redacts exception and message secret text", () => {
|
|
const result = sanitizeSentryEvent({
|
|
exception: { values: [{ type: "Error", value: "provider token=raw-secret failed" }] },
|
|
message: "Bearer raw-bearer-token failed",
|
|
});
|
|
expect(JSON.stringify(result)).not.toContain("raw-secret");
|
|
expect(JSON.stringify(result)).not.toContain("raw-bearer-token");
|
|
});
|
|
|
|
it("scrubs URL fields and descriptions throughout transaction spans", () => {
|
|
const result = sanitizeSentryEvent({
|
|
spans: [
|
|
{
|
|
description: "GET https://api.example.test/customers?email=alice@example.test#private",
|
|
data: {
|
|
"http.url": "https://api.example.test/customers?email=alice@example.test#private",
|
|
"http.target": "/customers?email=alice@example.test",
|
|
nested: { "url.full": "https://example.test/reset?token=raw" },
|
|
},
|
|
},
|
|
],
|
|
measurements: { payload: { password: "raw" } },
|
|
});
|
|
|
|
expect(result?.spans).toEqual([
|
|
{
|
|
description: "GET https://api.example.test/customers",
|
|
data: {
|
|
"http.url": "https://api.example.test/customers",
|
|
"http.target": "/customers",
|
|
nested: { "url.full": "https://example.test/reset" },
|
|
},
|
|
},
|
|
]);
|
|
expect(result?.measurements).toEqual({ payload: { password: "[Redacted]" } });
|
|
});
|
|
|
|
it("scrubs URL fields inside breadcrumbs", () => {
|
|
const result = sanitizeSentryEvent({
|
|
breadcrumbs: [{ category: "navigation", data: { from: "/", url: "/account?invite=secret#member" } }],
|
|
});
|
|
|
|
expect(result?.breadcrumbs).toEqual([{ category: "navigation", data: { from: "/", url: "/account" } }]);
|
|
});
|
|
|
|
it("exposes the canonical exception drop list", () => {
|
|
expect(SENTRY_DROP_EXCEPTION_TYPES.has("AbortError")).toBe(true);
|
|
expect(SENTRY_DROP_EXCEPTION_TYPES.has("RequestAbortedError")).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("createSentryErrorReporter init plumbing", () => {
|
|
it("forwards tracesSampleRate / profilesSampleRate / maxBreadcrumbs / integrations", async () => {
|
|
let captured: SentryInitOptions | undefined;
|
|
const fakeSentry: SentryLikeModule = {
|
|
init: (opts) => {
|
|
captured = opts;
|
|
},
|
|
captureException: vi.fn(),
|
|
captureMessage: vi.fn(),
|
|
};
|
|
|
|
const integrations = [{ name: "fake" }];
|
|
const reporter = createSentryErrorReporter({
|
|
dsn: "https://example@sentry.io/1",
|
|
environment: "staging",
|
|
release: "abc123",
|
|
tracesSampleRate: 0.25,
|
|
profilesSampleRate: 0.1,
|
|
maxBreadcrumbs: 75,
|
|
integrations,
|
|
loadSentry: async () => fakeSentry,
|
|
});
|
|
|
|
await reporter.report({ component: "api", operation: "request", errorMessage: "boom" });
|
|
|
|
expect(captured?.dsn).toBe("https://example@sentry.io/1");
|
|
expect(captured?.environment).toBe("staging");
|
|
expect(captured?.release).toBe("abc123");
|
|
expect(captured?.tracesSampleRate).toBe(0.25);
|
|
expect(captured?.profilesSampleRate).toBe(0.1);
|
|
expect(captured?.maxBreadcrumbs).toBe(75);
|
|
expect(captured?.integrations).toBe(integrations);
|
|
expect(typeof captured?.beforeSend).toBe("function");
|
|
});
|
|
|
|
it("defaults maxBreadcrumbs to 50 when not provided", async () => {
|
|
let captured: SentryInitOptions | undefined;
|
|
const fakeSentry: SentryLikeModule = {
|
|
init: (opts) => {
|
|
captured = opts;
|
|
},
|
|
captureException: vi.fn(),
|
|
captureMessage: vi.fn(),
|
|
};
|
|
|
|
const reporter = createSentryErrorReporter({
|
|
dsn: "https://example@sentry.io/1",
|
|
loadSentry: async () => fakeSentry,
|
|
});
|
|
|
|
await reporter.report({ component: "api", operation: "request", errorMessage: "boom" });
|
|
expect(captured?.maxBreadcrumbs).toBe(50);
|
|
});
|
|
|
|
it("init.beforeSend runs the recursive sanitizer end-to-end", async () => {
|
|
let captured: SentryInitOptions | undefined;
|
|
const fakeSentry: SentryLikeModule = {
|
|
init: (opts) => {
|
|
captured = opts;
|
|
},
|
|
captureException: vi.fn(),
|
|
captureMessage: vi.fn(),
|
|
};
|
|
|
|
const reporter = createSentryErrorReporter({
|
|
dsn: "https://example@sentry.io/1",
|
|
loadSentry: async () => fakeSentry,
|
|
});
|
|
|
|
await reporter.report({ component: "api", operation: "request", errorMessage: "boom" });
|
|
|
|
expect(captured?.beforeSend).toBeDefined();
|
|
const transformed = captured?.beforeSend?.(
|
|
{
|
|
request: { headers: { Authorization: "Bearer x", "Content-Type": "application/json" } },
|
|
user: { email: "bob@example.com" },
|
|
},
|
|
undefined
|
|
);
|
|
|
|
expect(transformed).not.toBeNull();
|
|
const headers = ((transformed as { request: { headers: Record<string, unknown> } }).request).headers;
|
|
expect(headers).not.toHaveProperty("Authorization");
|
|
expect(headers["Content-Type"]).toBe("application/json");
|
|
expect(((transformed as { user: Record<string, unknown> }).user).email).toBe("[Redacted]");
|
|
});
|
|
});
|