Skip to content

Commit

Permalink
refactor(logger): Simplify sanitizeValue function (#12859)
Browse files Browse the repository at this point in the history
* refactor(logger): Simplify sanitizeValue function

* Return older test

* One more test for functions
  • Loading branch information
zharinov committed Nov 27, 2021
1 parent 79da9bc commit fb8715c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
8 changes: 8 additions & 0 deletions lib/logger/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ describe('logger/index', () => {
});
add({ password: 'secret"password' });

class SomeClass {
constructor(public field: string) {}
}

logger.error({
foo: 'secret"password',
bar: ['somethingelse', 'secret"password'],
Expand All @@ -162,6 +166,8 @@ describe('logger/index', () => {
secrets: {
foo: 'barsecret',
},
someFn: () => 'secret"password',
someObject: new SomeClass('secret"password'),
});

expect(logged.foo).not.toBe('secret"password');
Expand All @@ -173,5 +179,7 @@ describe('logger/index', () => {
expect(logged.content).toBe('[content]');
expect(logged.prBody).toBe('[Template]');
expect(logged.secrets.foo).toBe('***********');
expect(logged.someFn).toBe('[function]');
expect(logged.someObject.field).toBe('**redacted**');
});
});
64 changes: 41 additions & 23 deletions lib/logger/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Stream } from 'stream';
import is from '@sindresorhus/is';
import bunyan from 'bunyan';
import fs from 'fs-extra';
import { clone } from '../util/clone';
Expand Down Expand Up @@ -90,38 +91,54 @@ export default function prepareError(err: Error): Record<string, unknown> {
return response;
}

export function sanitizeValue(_value: unknown, seen = new WeakMap()): any {
let value = _value;
if (Array.isArray(value)) {
const length = value.length;
const arrayResult = Array(length);
seen.set(value, arrayResult);
for (let idx = 0; idx < length; idx += 1) {
const val = value[idx];
arrayResult[idx] = seen.has(val)
? seen.get(val)
: sanitizeValue(val, seen);
}
return arrayResult;
type NestedValue = unknown[] | object;

function isNested(value: unknown): value is NestedValue {
return is.array(value) || is.object(value);
}

export function sanitizeValue(
value: unknown,
seen = new WeakMap<NestedValue, unknown>()
): any {
if (is.string(value)) {
return sanitize(value);
}

if (value instanceof Buffer) {
return '[content]';
if (is.date(value)) {
return value;
}

if (value instanceof Error) {
value = prepareError(value);
if (is.function_(value)) {
return '[function]';
}

const valueType = typeof value;
if (is.buffer(value)) {
return '[content]';
}

if (is.error(value)) {
const err = prepareError(value);
return sanitizeValue(err, seen);
}

if (value && valueType !== 'function' && valueType === 'object') {
if (value instanceof Date) {
return value;
if (is.array(value)) {
const length = value.length;
const arrayResult = Array(length);
seen.set(value, arrayResult);
for (let idx = 0; idx < length; idx += 1) {
const val = value[idx];
arrayResult[idx] =
isNested(val) && seen.has(val)
? seen.get(val)
: sanitizeValue(val, seen);
}
return arrayResult;
}

if (is.object(value)) {
const objectResult: Record<string, any> = {};
seen.set(value as any, objectResult);
seen.set(value, objectResult);
for (const [key, val] of Object.entries<any>(value)) {
let curValue: any;
if (!val) {
Expand All @@ -143,10 +160,11 @@ export function sanitizeValue(_value: unknown, seen = new WeakMap()): any {

objectResult[key] = curValue;
}

return objectResult;
}

return valueType === 'string' ? sanitize(value as string) : value;
return value;
}

export function withSanitizer(streamConfig: bunyan.Stream): bunyan.Stream {
Expand Down

0 comments on commit fb8715c

Please sign in to comment.