Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -49,6 +49,27 @@ describe("@hivegrid/logger", () => {
|
||||
expect(sink.records[0]).not.toHaveProperty("hostname");
|
||||
});
|
||||
|
||||
it("passes sanitized arguments through a caller log hook", () => {
|
||||
const sink = createLogSink();
|
||||
let hookCalls = 0;
|
||||
const logger = createLogger({
|
||||
level: "info",
|
||||
stream: sink.stream,
|
||||
hooks: {
|
||||
logMethod(args, method) {
|
||||
hookCalls += 1;
|
||||
method.apply(this, args);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
logger.info({ requestId: "req-hook", token: "raw-hook-token" }, "hooked");
|
||||
|
||||
expect(hookCalls).toBe(1);
|
||||
expect(sink.records[0]).toMatchObject({ requestId: "req-hook", token: "[Redacted]", msg: "hooked" });
|
||||
expect(JSON.stringify(sink.records[0])).not.toContain("raw-hook-token");
|
||||
});
|
||||
|
||||
it("creates child loggers with runtime bindings", () => {
|
||||
const sink = createLogSink();
|
||||
const logger = createLogger({ level: "info", stream: sink.stream });
|
||||
@@ -93,13 +114,13 @@ describe("@hivegrid/logger", () => {
|
||||
connectionString: rawSecrets[7],
|
||||
},
|
||||
},
|
||||
"secret payload",
|
||||
"secret payload"
|
||||
);
|
||||
|
||||
expect(sink.records).toHaveLength(1);
|
||||
expect(sink.records[0]).toMatchObject({
|
||||
authorization: "[Redacted]",
|
||||
headers: { cookie: "[Redacted]" },
|
||||
headers: "[Redacted]",
|
||||
password: "[Redacted]",
|
||||
nested: {
|
||||
token: "[Redacted]",
|
||||
@@ -131,7 +152,7 @@ describe("@hivegrid/logger", () => {
|
||||
requestId: "req_correlation_1",
|
||||
nested: { secret: "nested-secret", requestId: "req_nested_1" },
|
||||
},
|
||||
"correlated event",
|
||||
"correlated event"
|
||||
);
|
||||
|
||||
expect(sink.records).toHaveLength(1);
|
||||
@@ -151,4 +172,96 @@ describe("@hivegrid/logger", () => {
|
||||
expect(serializedRecord).not.toContain("Bearer request-token");
|
||||
expect(serializedRecord).not.toContain("nested-secret");
|
||||
});
|
||||
|
||||
it("recursively redacts PII, nested payloads, URL queries, and secret text", () => {
|
||||
const sink = createLogSink();
|
||||
const logger = createLogger({ level: "info", stream: sink.stream });
|
||||
|
||||
logger.error(
|
||||
{
|
||||
clientIp: "203.0.113.42",
|
||||
email: "alice@example.test",
|
||||
nested: {
|
||||
deeper: {
|
||||
payload: { safe: false, password: "raw-password" },
|
||||
url: "https://example.test/reset?token=raw-token#private",
|
||||
},
|
||||
},
|
||||
err: new Error("request failed with Bearer raw-bearer and redis://user:secret@cache.example/0"),
|
||||
},
|
||||
"provider token=raw-message-token failed at https://example.test/path?email=alice@example.test"
|
||||
);
|
||||
|
||||
const serialized = JSON.stringify(sink.records[0]);
|
||||
for (const sensitive of [
|
||||
"203.0.113.42",
|
||||
"alice@example.test",
|
||||
"raw-password",
|
||||
"raw-token",
|
||||
"raw-bearer",
|
||||
"redis://user:secret@cache.example/0",
|
||||
"raw-message-token",
|
||||
]) {
|
||||
expect(serialized).not.toContain(sensitive);
|
||||
}
|
||||
expect(sink.records[0]?.nested).toEqual({
|
||||
deeper: {
|
||||
payload: "[Redacted]",
|
||||
url: "https://example.test/reset",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("bounds circular, deeply nested, oversized, and wide log values", () => {
|
||||
const sink = createLogSink();
|
||||
const logger = createLogger({ level: "info", stream: sink.stream });
|
||||
const circular: Record<string, unknown> = {};
|
||||
circular.self = circular;
|
||||
const wide = Object.fromEntries(Array.from({ length: 120 }, (_, index) => [`key${index}`, index]));
|
||||
|
||||
logger.info({ circular, huge: "x".repeat(9 * 1024), wide }, "bounded");
|
||||
|
||||
const record = sink.records[0] as Record<string, unknown>;
|
||||
expect(record.circular).toEqual({ self: "[Circular]" });
|
||||
expect(record.huge).toMatch(/^<string truncated/);
|
||||
expect((record.wide as Record<string, unknown>).__omittedEntries).toBe(20);
|
||||
});
|
||||
|
||||
it("sanitizes recursive edge types and bounds arrays and depth", () => {
|
||||
const sink = createLogSink();
|
||||
const logger = createLogger({ level: "info", stream: sink.stream });
|
||||
const deep: Record<string, unknown> = {};
|
||||
let cursor = deep;
|
||||
for (let index = 0; index < 10; index += 1) {
|
||||
const next: Record<string, unknown> = {};
|
||||
cursor.next = next;
|
||||
cursor = next;
|
||||
}
|
||||
const errorWithoutStack = new Error("token=raw-error-token");
|
||||
errorWithoutStack.stack = undefined;
|
||||
|
||||
logger.info(
|
||||
{
|
||||
binary: Buffer.from("private bytes"),
|
||||
createdAt: new Date("2026-07-12T10:00:00.000Z"),
|
||||
deep,
|
||||
errorWithoutStack,
|
||||
malformed: { url: "http://[::1?token=raw-url-token" },
|
||||
relative: { url: "/private/path?token=raw-relative-token#fragment" },
|
||||
values: Array.from({ length: 102 }, (_, index) => index),
|
||||
},
|
||||
"edge values"
|
||||
);
|
||||
|
||||
const record = sink.records[0] as Record<string, unknown>;
|
||||
expect(record.binary).toBe("[Buffer 13 bytes]");
|
||||
expect(record.createdAt).toBe("2026-07-12T10:00:00.000Z");
|
||||
expect(JSON.stringify(record.deep)).toContain("[MaxDepth]");
|
||||
expect(record.errorWithoutStack).toEqual({ name: "Error", message: "[Redacted]" });
|
||||
expect(record.relative).toEqual({ url: "/private/path" });
|
||||
expect(JSON.stringify(record.malformed)).not.toContain("raw-url-token");
|
||||
const values = record.values as unknown[];
|
||||
expect(values).toHaveLength(101);
|
||||
expect(values.at(-1)).toBe("[2 entries omitted]");
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
import pino, { type DestinationStream, type Logger, type LoggerOptions } from "pino";
|
||||
import pino, { type DestinationStream, type Logger, type LoggerOptions, type LogFn } from "pino";
|
||||
|
||||
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal" | "silent";
|
||||
|
||||
@@ -27,6 +27,49 @@ const REDACT_PATHS = [
|
||||
"*.connectionString",
|
||||
] as const;
|
||||
|
||||
const REDACTED = "[Redacted]";
|
||||
const CIRCULAR = "[Circular]";
|
||||
const MAX_LOG_DEPTH = 8;
|
||||
const MAX_LOG_ENTRIES = 100;
|
||||
const MAX_LOG_STRING_BYTES = 8 * 1024;
|
||||
const SENSITIVE_KEYS = new Set([
|
||||
"authorization",
|
||||
"body",
|
||||
"connectionstring",
|
||||
"cookie",
|
||||
"email",
|
||||
"emailaddress",
|
||||
"headers",
|
||||
"ip",
|
||||
"ipaddress",
|
||||
"clientip",
|
||||
"password",
|
||||
"passwd",
|
||||
"payload",
|
||||
"phone",
|
||||
"phonenumber",
|
||||
"recipient",
|
||||
"redisurl",
|
||||
"secret",
|
||||
"session",
|
||||
"sessionid",
|
||||
"setcookie",
|
||||
"templatedata",
|
||||
"to",
|
||||
"token",
|
||||
"accesstoken",
|
||||
"refreshtoken",
|
||||
"idtoken",
|
||||
"apikey",
|
||||
"xapikey",
|
||||
"xcsrftoken",
|
||||
]);
|
||||
const URL_KEYS = new Set(["href", "httpurl", "referer", "referrer", "url", "urlfull"]);
|
||||
const BEARER_PATTERN = /bearer\s+[^\s,;]+/giu;
|
||||
const CONNECTION_URL_PATTERN = /(?:postgres(?:ql)?|redis):\/\/[^\s,;]+/giu;
|
||||
const SECRET_ASSIGNMENT_PATTERN = /(?:token|secret|password|api[_-]?key|connectionstring|redisurl)=([^\s,;]+)/giu;
|
||||
const ABSOLUTE_URL_QUERY_PATTERN = /(https?:\/\/[^\s?#]+)(?:\?[^\s#]*)?(?:#[^\s]*)?/giu;
|
||||
|
||||
export interface CreateLoggerOptions extends Omit<LoggerOptions, "base" | "level" | "redact"> {
|
||||
level?: LogLevel;
|
||||
stream?: DestinationStream;
|
||||
@@ -37,7 +80,7 @@ export function resolveLogLevel(env: NodeJS.ProcessEnv = process.env): LogLevel
|
||||
}
|
||||
|
||||
export function createLogger(options: CreateLoggerOptions = {}): HiveGridLogger {
|
||||
const { level = resolveLogLevel(), stream, ...pinoOptions } = options;
|
||||
const { level = resolveLogLevel(), stream, hooks: callerHooks, ...pinoOptions } = options;
|
||||
|
||||
return pino(
|
||||
{
|
||||
@@ -46,15 +89,103 @@ export function createLogger(options: CreateLoggerOptions = {}): HiveGridLogger
|
||||
level,
|
||||
redact: {
|
||||
paths: [...REDACT_PATHS],
|
||||
censor: "[Redacted]",
|
||||
censor: REDACTED,
|
||||
},
|
||||
hooks: {
|
||||
...callerHooks,
|
||||
logMethod(args, method, numericLevel) {
|
||||
const sanitizedArgs = args.map((argument) => sanitizeLogValue(argument)) as Parameters<LogFn>;
|
||||
if (callerHooks?.logMethod) {
|
||||
callerHooks.logMethod.call(this, sanitizedArgs, method, numericLevel);
|
||||
return;
|
||||
}
|
||||
method.apply(this, sanitizedArgs);
|
||||
},
|
||||
},
|
||||
},
|
||||
stream,
|
||||
stream
|
||||
);
|
||||
}
|
||||
|
||||
export function createChildLogger(logger: HiveGridLogger, bindings: Record<string, unknown>): HiveGridLogger {
|
||||
return logger.child(bindings);
|
||||
return logger.child(sanitizeLogValue(bindings) as Record<string, unknown>);
|
||||
}
|
||||
|
||||
export const defaultLogger = createLogger();
|
||||
|
||||
function sanitizeLogValue(value: unknown, key?: string, depth = 0, seen = new WeakSet<object>()): unknown {
|
||||
const normalizedKey = key?.trim().toLowerCase().replace(/[-_.]/gu, "");
|
||||
if (normalizedKey && SENSITIVE_KEYS.has(normalizedKey)) {
|
||||
return REDACTED;
|
||||
}
|
||||
if (depth > MAX_LOG_DEPTH) {
|
||||
return "[MaxDepth]";
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
const sanitized = normalizedKey && URL_KEYS.has(normalizedKey) ? stripUrlDetails(value) : redactUnsafeText(value);
|
||||
return truncateString(sanitized);
|
||||
}
|
||||
if (value instanceof Error) {
|
||||
return {
|
||||
name: value.name,
|
||||
message: truncateString(redactUnsafeText(value.message)),
|
||||
...(value.stack ? { stack: truncateString(redactUnsafeText(value.stack)) } : {}),
|
||||
};
|
||||
}
|
||||
if (value instanceof Date) {
|
||||
return value.toISOString();
|
||||
}
|
||||
if (Buffer.isBuffer(value)) {
|
||||
return `[Buffer ${value.byteLength} bytes]`;
|
||||
}
|
||||
if (!value || typeof value !== "object") {
|
||||
return value;
|
||||
}
|
||||
if (seen.has(value)) {
|
||||
return CIRCULAR;
|
||||
}
|
||||
seen.add(value);
|
||||
try {
|
||||
if (Array.isArray(value)) {
|
||||
const entries = value.slice(0, MAX_LOG_ENTRIES).map((entry) => sanitizeLogValue(entry, undefined, depth + 1, seen));
|
||||
if (value.length > MAX_LOG_ENTRIES) entries.push(`[${value.length - MAX_LOG_ENTRIES} entries omitted]`);
|
||||
return entries;
|
||||
}
|
||||
|
||||
const entries = Object.entries(value).slice(0, MAX_LOG_ENTRIES);
|
||||
const sanitized = Object.fromEntries(entries.map(([entryKey, entryValue]) => [entryKey, sanitizeLogValue(entryValue, entryKey, depth + 1, seen)]));
|
||||
if (Object.keys(value).length > MAX_LOG_ENTRIES) {
|
||||
sanitized.__omittedEntries = Object.keys(value).length - MAX_LOG_ENTRIES;
|
||||
}
|
||||
return sanitized;
|
||||
} finally {
|
||||
seen.delete(value);
|
||||
}
|
||||
}
|
||||
|
||||
function redactUnsafeText(value: string): string {
|
||||
return value
|
||||
.replace(BEARER_PATTERN, "Bearer [Redacted]")
|
||||
.replace(CONNECTION_URL_PATTERN, REDACTED)
|
||||
.replace(SECRET_ASSIGNMENT_PATTERN, REDACTED)
|
||||
.replace(ABSOLUTE_URL_QUERY_PATTERN, "$1");
|
||||
}
|
||||
|
||||
function stripUrlDetails(value: string): string {
|
||||
try {
|
||||
const absolute = /^[A-Za-z][A-Za-z0-9+.-]*:/u.test(value);
|
||||
const parsed = new URL(value, "https://redaction.invalid");
|
||||
parsed.username = "";
|
||||
parsed.password = "";
|
||||
parsed.search = "";
|
||||
parsed.hash = "";
|
||||
return absolute ? parsed.toString() : parsed.pathname;
|
||||
} catch {
|
||||
return redactUnsafeText(value);
|
||||
}
|
||||
}
|
||||
|
||||
function truncateString(value: string): string {
|
||||
const byteLength = Buffer.byteLength(value, "utf8");
|
||||
return byteLength <= MAX_LOG_STRING_BYTES ? value : `<string truncated, ${byteLength} bytes>`;
|
||||
}
|
||||
Reference in new issue
Block a user