fix(core-shared): TypeScript type fixes for Sentry adapter layer — null attr filter, structural event types, non-null assertions, vi.stubEnv
This commit is contained in:
@@ -2,16 +2,22 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { beforeSend, beforeSendTransaction } from "@/instrumentation/sentry/scrub";
|
||||
|
||||
// Use structural types that match what scrub functions accept
|
||||
type ScrubEvent = Parameters<typeof beforeSend>[0];
|
||||
type ScrubHint = Parameters<typeof beforeSend>[1];
|
||||
type TxEvent = Parameters<typeof beforeSendTransaction>[0];
|
||||
type TxHint = Parameters<typeof beforeSendTransaction>[1];
|
||||
|
||||
const hint = {} as ScrubHint;
|
||||
const txHint = {} as TxHint;
|
||||
|
||||
describe("beforeSend", () => {
|
||||
it("redacts top-level keys whose names contain PII substrings", () => {
|
||||
const event = {
|
||||
extra: { email: "a@b.c", username: "alice" },
|
||||
contexts: { custom: { password: "p", note: "ok" } },
|
||||
} as Parameters<typeof beforeSend>[0];
|
||||
const result = beforeSend(event, {} as Parameters<typeof beforeSend>[1]) as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
} as unknown as ScrubEvent;
|
||||
const result = beforeSend(event, hint) as Record<string, unknown>;
|
||||
const extra = result["extra"] as Record<string, unknown>;
|
||||
const contexts = result["contexts"] as { custom: Record<string, unknown> };
|
||||
expect(extra["email"]).toBe("[redacted]");
|
||||
@@ -23,11 +29,8 @@ describe("beforeSend", () => {
|
||||
it("redacts derived key names (substring match): userEmail, accessToken, apiKey", () => {
|
||||
const event = {
|
||||
extra: { userEmail: "a@b.c", accessToken: "t", apiKey: "k", id: "u1" },
|
||||
} as Parameters<typeof beforeSend>[0];
|
||||
const result = beforeSend(event, {} as Parameters<typeof beforeSend>[1]) as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
} as unknown as ScrubEvent;
|
||||
const result = beforeSend(event, hint) as Record<string, unknown>;
|
||||
const extra = result["extra"] as Record<string, unknown>;
|
||||
expect(extra["userEmail"]).toBe("[redacted]");
|
||||
expect(extra["accessToken"]).toBe("[redacted]");
|
||||
@@ -40,11 +43,8 @@ describe("beforeSend", () => {
|
||||
request: {
|
||||
headers: { Authorization: "Bearer x", "Set-Cookie": "session=abc", "User-Agent": "ua" },
|
||||
},
|
||||
} as Parameters<typeof beforeSend>[0];
|
||||
const result = beforeSend(event, {} as Parameters<typeof beforeSend>[1]) as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
} as unknown as ScrubEvent;
|
||||
const result = beforeSend(event, hint) as Record<string, unknown>;
|
||||
const request = result["request"] as { headers: Record<string, unknown> };
|
||||
expect(request.headers["Authorization"]).toBe("[redacted]");
|
||||
expect(request.headers["Set-Cookie"]).toBe("[redacted]");
|
||||
@@ -52,38 +52,30 @@ describe("beforeSend", () => {
|
||||
});
|
||||
|
||||
it("redacts IPv4 addresses found in string values", () => {
|
||||
const event = { extra: { note: "Connection from 192.168.1.10 failed" } } as Parameters<
|
||||
typeof beforeSend
|
||||
>[0];
|
||||
const result = beforeSend(event, {} as Parameters<typeof beforeSend>[1]) as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
const event = {
|
||||
extra: { note: "Connection from 192.168.1.10 failed" },
|
||||
} as unknown as ScrubEvent;
|
||||
const result = beforeSend(event, hint) as Record<string, unknown>;
|
||||
const extra = result["extra"] as Record<string, unknown>;
|
||||
expect(extra["note"]).toBe("Connection from [redacted-ip] failed");
|
||||
});
|
||||
|
||||
it("redacts IPv6 addresses found in string values", () => {
|
||||
const event = { extra: { note: "Tunnel to fe80::1ff:fe23:4567:890a established" } } as Parameters<
|
||||
typeof beforeSend
|
||||
>[0];
|
||||
const result = beforeSend(event, {} as Parameters<typeof beforeSend>[1]) as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
const event = {
|
||||
extra: { note: "Tunnel to fe80::1ff:fe23:4567:890a established" },
|
||||
} as unknown as ScrubEvent;
|
||||
const result = beforeSend(event, hint) as Record<string, unknown>;
|
||||
const extra = result["extra"] as Record<string, unknown>;
|
||||
expect(extra["note"] as string).toContain("[redacted-ip]");
|
||||
});
|
||||
|
||||
it("does not crash on null/undefined branches", () => {
|
||||
expect(beforeSend({ extra: null } as Parameters<typeof beforeSend>[0], {} as Parameters<typeof beforeSend>[1])).toBeTruthy();
|
||||
expect(beforeSend({} as Parameters<typeof beforeSend>[0], {} as Parameters<typeof beforeSend>[1])).toBeTruthy();
|
||||
expect(beforeSend({ extra: null } as unknown as ScrubEvent, hint)).toBeTruthy();
|
||||
expect(beforeSend({} as ScrubEvent, hint)).toBeTruthy();
|
||||
});
|
||||
|
||||
it("returns the event (not null) — keeps Sentry transport flowing", () => {
|
||||
expect(
|
||||
beforeSend({ extra: { ok: true } } as Parameters<typeof beforeSend>[0], {} as Parameters<typeof beforeSend>[1]),
|
||||
).toBeTruthy();
|
||||
expect(beforeSend({ extra: { ok: true } } as unknown as ScrubEvent, hint)).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -91,11 +83,8 @@ describe("beforeSendTransaction", () => {
|
||||
it("strips PII query params from request.url", () => {
|
||||
const event = {
|
||||
request: { url: "https://app/api/foo?token=secret&user=alice&email=a@b.c" },
|
||||
} as Parameters<typeof beforeSendTransaction>[0];
|
||||
const result = beforeSendTransaction(
|
||||
event,
|
||||
{} as Parameters<typeof beforeSendTransaction>[1],
|
||||
) as Record<string, unknown>;
|
||||
} as unknown as TxEvent;
|
||||
const result = beforeSendTransaction(event, txHint) as Record<string, unknown>;
|
||||
const request = result["request"] as { url: string };
|
||||
expect(request.url).toContain("token=%5Bredacted%5D");
|
||||
expect(request.url).toContain("email=%5Bredacted%5D");
|
||||
@@ -103,25 +92,17 @@ describe("beforeSendTransaction", () => {
|
||||
});
|
||||
|
||||
it("strips PII query params from event.transaction", () => {
|
||||
const event = { transaction: "/foo?token=x&id=y" } as Parameters<
|
||||
typeof beforeSendTransaction
|
||||
>[0];
|
||||
const result = beforeSendTransaction(
|
||||
event,
|
||||
{} as Parameters<typeof beforeSendTransaction>[1],
|
||||
) as Record<string, unknown>;
|
||||
const event = { transaction: "/foo?token=x&id=y" } as unknown as TxEvent;
|
||||
const result = beforeSendTransaction(event, txHint) as Record<string, unknown>;
|
||||
expect(result["transaction"] as string).toContain("token=%5Bredacted%5D");
|
||||
expect(result["transaction"] as string).toContain("id=y");
|
||||
});
|
||||
|
||||
it("matches derived param names (accessToken, ApiSecret)", () => {
|
||||
const event = { request: { url: "https://x/y?accessToken=t&ApiSecret=z&safe=1" } } as Parameters<
|
||||
typeof beforeSendTransaction
|
||||
>[0];
|
||||
const result = beforeSendTransaction(
|
||||
event,
|
||||
{} as Parameters<typeof beforeSendTransaction>[1],
|
||||
) as Record<string, unknown>;
|
||||
const event = {
|
||||
request: { url: "https://x/y?accessToken=t&ApiSecret=z&safe=1" },
|
||||
} as unknown as TxEvent;
|
||||
const result = beforeSendTransaction(event, txHint) as Record<string, unknown>;
|
||||
const request = result["request"] as { url: string };
|
||||
expect(request.url).toContain("accessToken=%5Bredacted%5D");
|
||||
expect(request.url).toContain("ApiSecret=%5Bredacted%5D");
|
||||
@@ -129,11 +110,6 @@ describe("beforeSendTransaction", () => {
|
||||
});
|
||||
|
||||
it("returns the event when no URL present", () => {
|
||||
expect(
|
||||
beforeSendTransaction(
|
||||
{} as Parameters<typeof beforeSendTransaction>[0],
|
||||
{} as Parameters<typeof beforeSendTransaction>[1],
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(beforeSendTransaction({} as TxEvent, txHint)).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user