Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,229 @@
|
||||
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",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
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",
|
||||
"referer",
|
||||
"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("redacts known query-string secret keys without dropping other params", () => {
|
||||
const result = sanitizeSentryEvent({
|
||||
request: {
|
||||
url: "https://api.example.com/x",
|
||||
query_string: "id=42&token=abc&apiKey=xyz&page=2",
|
||||
headers: {},
|
||||
},
|
||||
});
|
||||
|
||||
const q = (result?.request as { query_string: string }).query_string;
|
||||
expect(q).toContain("id=42");
|
||||
expect(q).toContain("page=2");
|
||||
expect(q).toContain("token=[Redacted]");
|
||||
expect(q).toContain("apiKey=[Redacted]");
|
||||
expect(q).not.toContain("abc");
|
||||
expect(q).not.toContain("xyz");
|
||||
});
|
||||
|
||||
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", "x-forwarded-for", "referer"]) {
|
||||
expect(SENTRY_HEADER_ALLOWLIST.has(h)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
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]");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user