safety
This commit is contained in:
parent
879e9b4228
commit
c3cb36c8d6
51 changed files with 2290 additions and 1467 deletions
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
import pino from 'pino';
|
||||
import pretty from 'pino-pretty';
|
||||
import { redactLogValue } from './redact';
|
||||
import type { LogContext, LoggerConfig, LogLevel, LogMetadata } from './types';
|
||||
|
||||
// Simple cache for logger instances
|
||||
|
|
@ -183,12 +184,12 @@ export class Logger {
|
|||
}
|
||||
|
||||
if (typeof message === 'string') {
|
||||
this.pino[level](data, message);
|
||||
this.pino[level](redactLogValue(data), redactLogValue(message) as string);
|
||||
} else {
|
||||
if (globalConfig.hideObject && level !== 'error' && level !== 'fatal') {
|
||||
this.pino[level]({}, `Object logged (hidden)`);
|
||||
} else {
|
||||
this.pino[level]({ ...data, data: message }, 'Object logged');
|
||||
this.pino[level](redactLogValue({ ...data, data: message }), 'Object logged');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
52
libs/core/logger/src/redact.ts
Normal file
52
libs/core/logger/src/redact.ts
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
const sensitiveKey =
|
||||
/password|passwd|secret|token|api[_-]?key|authorization|cookie|credential|private[_-]?key|session[_-]?id/i;
|
||||
const hidden = '[REDACTED]';
|
||||
|
||||
function redactText(value: string): string {
|
||||
return value
|
||||
.replace(/([a-z][a-z0-9+.-]*:\/\/)[^\s/@]+(?::[^\s/@]*)?@/gi, `$1${hidden}@`)
|
||||
.replace(
|
||||
/([?&](?:api[_-]?(?:key|token)|token|key|password|secret|credential|auth)=)[^&#\s]*/gi,
|
||||
`$1${hidden}`
|
||||
)
|
||||
.replace(/\bBearer\s+[a-z0-9._~+/=-]+/gi, `Bearer ${hidden}`);
|
||||
}
|
||||
|
||||
/** Redact without mutating callers, including error objects and circular metadata. */
|
||||
export function redactLogValue(value: unknown, seen = new WeakSet<object>()): unknown {
|
||||
if (typeof value === 'string') {
|
||||
return redactText(value);
|
||||
}
|
||||
if (!value || typeof value !== 'object') {
|
||||
return value;
|
||||
}
|
||||
if (seen.has(value)) {
|
||||
return '[Circular]';
|
||||
}
|
||||
seen.add(value);
|
||||
if (value instanceof Date) {
|
||||
return value.toISOString();
|
||||
}
|
||||
if (Buffer.isBuffer(value)) {
|
||||
return '[Binary]';
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(item => redactLogValue(item, seen));
|
||||
}
|
||||
const source =
|
||||
value instanceof Error
|
||||
? {
|
||||
...value,
|
||||
name: value.name,
|
||||
message: value.message,
|
||||
stack: value.stack,
|
||||
cause: value.cause,
|
||||
}
|
||||
: value;
|
||||
return Object.fromEntries(
|
||||
Object.entries(source).map(([key, item]) => [
|
||||
key,
|
||||
sensitiveKey.test(key) ? hidden : redactLogValue(item, seen),
|
||||
])
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue