Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/error-reporter/src/sanitize.test.ts
T

112 lines
4.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { sanitizeErrorReportValue, __internal } from "./sanitize.js";
describe("sanitizeErrorReportValue (recursive)", () => {
it("redacts nested sensitive keys regardless of casing and separators", () => {
const sanitized = sanitizeErrorReportValue({
Authorization: "Bearer raw-token",
passWord: "raw",
X_API_Key: "x-key-value",
X_CSRF_Token: "csrf-value",
"Set-Cookie": "session=abc; Path=/",
access_token: "access-raw",
refreshToken: "refresh-raw",
session_id: "sess-id",
phone: "+49-30-12345",
Email: "person@example.com",
payload: {
nested: {
password: "deep-pw",
extras: [{ apiKey: "k1" }, { setCookie: "session=def" }],
},
},
}) as Record<string, unknown>;
expect(sanitized.Authorization).toBe("[Redacted]");
expect(sanitized.passWord).toBe("[Redacted]");
expect(sanitized.X_API_Key).toBe("[Redacted]");
expect(sanitized.X_CSRF_Token).toBe("[Redacted]");
expect(sanitized["Set-Cookie"]).toBe("[Redacted]");
expect(sanitized.access_token).toBe("[Redacted]");
expect(sanitized.refreshToken).toBe("[Redacted]");
expect(sanitized.session_id).toBe("[Redacted]");
expect(sanitized.phone).toBe("[Redacted]");
expect(sanitized.Email).toBe("[Redacted]");
const payload = sanitized.payload as { nested: { password: string; extras: Array<Record<string, string>> } };
expect(payload.nested.password).toBe("[Redacted]");
expect(payload.nested.extras[0]?.apiKey).toBe("[Redacted]");
expect(payload.nested.extras[1]?.setCookie).toBe("[Redacted]");
});
it("redacts scalar secret patterns embedded in free-form strings", () => {
const result = sanitizeErrorReportValue({
message: "Failed Authorization: Bearer eyJabc.def at postgres://user:password@db/host",
detail: "connection redis://:secret@redis:6379 fell over",
params: "token=abc123 went bad",
}) as Record<string, string>;
expect(result.message).toContain("Bearer [Redacted]");
expect(result.message).toContain("[Redacted]");
expect(result.message).not.toContain("eyJabc.def");
expect(result.message).not.toContain("user:password");
expect(result.detail).toContain("[Redacted]");
expect(result.detail).not.toContain(":secret@redis:6379");
expect(result.params).toContain("[Redacted]");
expect(result.params).not.toContain("abc123");
});
it("truncates strings larger than 8 KB with a length marker", () => {
const huge = "a".repeat(__internal.MAX_STRING_BYTES + 5);
const result = sanitizeErrorReportValue({ blob: huge }) as { blob: string };
expect(result.blob).toMatch(/^<string truncated, \d+ bytes>$/);
expect(result.blob).not.toContain("aaa");
});
it("does not truncate strings at or below the 8 KB boundary", () => {
const justFits = "a".repeat(__internal.MAX_STRING_BYTES);
const result = sanitizeErrorReportValue({ blob: justFits }) as { blob: string };
expect(result.blob).toBe(justFits);
});
it("handles circular references without overflowing", () => {
const circular: Record<string, unknown> = { a: 1 };
circular.self = circular;
const list: unknown[] = [];
list.push(list);
const result = sanitizeErrorReportValue({ root: circular, listy: list }) as { root: { a: number; self: string }; listy: string[] };
expect(result.root.a).toBe(1);
expect(result.root.self).toBe("[Circular]");
expect(result.listy[0]).toBe("[Circular]");
});
it("preserves Error metadata while redacting enumerable secret fields", () => {
const err = new Error("connection failed token=abc123");
Object.assign(err, { token: "raw", safe: "kept" });
const result = sanitizeErrorReportValue({ error: err }) as { error: Record<string, unknown> };
expect(result.error.errorName).toBe("Error");
expect(result.error.errorMessage).toContain("[Redacted]");
expect(result.error.errorMessage).not.toContain("abc123");
expect(result.error.token).toBe("[Redacted]");
expect(result.error.safe).toBe("kept");
});
it("redacts setCookie variants (Set-Cookie, set_cookie, setCookie)", () => {
const result = sanitizeErrorReportValue({
"Set-Cookie": "x",
set_cookie: "y",
setCookie: "z",
"X-Api-Key": "k",
"x-csrf-token": "c",
}) as Record<string, string>;
expect(result["Set-Cookie"]).toBe("[Redacted]");
expect(result.set_cookie).toBe("[Redacted]");
expect(result.setCookie).toBe("[Redacted]");
expect(result["X-Api-Key"]).toBe("[Redacted]");
expect(result["x-csrf-token"]).toBe("[Redacted]");
});
});