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
@@ -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