chore: safer way to do redactions

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2024-05-15 15:03:13 +02:00
parent 3e8148e39c
commit 7d30d95dee
6 changed files with 42 additions and 9 deletions
+2
View File
@@ -72,6 +72,7 @@
"fs-extra": "^11.2.0",
"helmet": "^6.0.0",
"jose": "^5.0.0",
"json-stringify-safe": "^5.0.1",
"knex": "^3.0.0",
"lodash": "^4.17.21",
"logform": "^2.3.2",
@@ -93,6 +94,7 @@
"@types/compression": "^1.7.0",
"@types/fs-extra": "^11.0.0",
"@types/http-errors": "^2.0.0",
"@types/json-stringify-safe": "^5.0.3",
"@types/minimist": "^1.2.0",
"@types/morgan": "^1.9.0",
"@types/node-forge": "^1.3.0",
@@ -27,6 +27,7 @@ import {
transports,
transport as Transport,
} from 'winston';
import stringify from 'json-stringify-safe';
import { escapeRegExp } from '../lib/escapeRegExp';
/**
@@ -86,9 +87,13 @@ export class WinstonLogger implements RootLoggerService {
let redactionPattern: RegExp | undefined = undefined;
const replace = (obj: TransformableInfo) => {
const stringifiedFields = JSON.stringify(obj, null);
if (!redactionPattern) {
return obj;
}
const stringifiedFields = stringify(obj);
const redacted = JSON.parse(
stringifiedFields.replace(redactionPattern!, '[REDACTED]'),
stringifiedFields.replace(redactionPattern, '[REDACTED]'),
);
for (const key in redacted) {