Skip to content
Snippets Groups Projects
Unverified Commit fb8715c9 authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

refactor(logger): Simplify sanitizeValue function (#12859)

* refactor(logger): Simplify sanitizeValue function

* Return older test

* One more test for functions
parent 79da9bc0
No related branches found
No related tags found
No related merge requests found
...@@ -152,6 +152,10 @@ describe('logger/index', () => { ...@@ -152,6 +152,10 @@ describe('logger/index', () => {
}); });
add({ password: 'secret"password' }); add({ password: 'secret"password' });
class SomeClass {
constructor(public field: string) {}
}
logger.error({ logger.error({
foo: 'secret"password', foo: 'secret"password',
bar: ['somethingelse', 'secret"password'], bar: ['somethingelse', 'secret"password'],
...@@ -162,6 +166,8 @@ describe('logger/index', () => { ...@@ -162,6 +166,8 @@ describe('logger/index', () => {
secrets: { secrets: {
foo: 'barsecret', foo: 'barsecret',
}, },
someFn: () => 'secret"password',
someObject: new SomeClass('secret"password'),
}); });
expect(logged.foo).not.toBe('secret"password'); expect(logged.foo).not.toBe('secret"password');
...@@ -173,5 +179,7 @@ describe('logger/index', () => { ...@@ -173,5 +179,7 @@ describe('logger/index', () => {
expect(logged.content).toBe('[content]'); expect(logged.content).toBe('[content]');
expect(logged.prBody).toBe('[Template]'); expect(logged.prBody).toBe('[Template]');
expect(logged.secrets.foo).toBe('***********'); expect(logged.secrets.foo).toBe('***********');
expect(logged.someFn).toBe('[function]');
expect(logged.someObject.field).toBe('**redacted**');
}); });
}); });
import { Stream } from 'stream'; import { Stream } from 'stream';
import is from '@sindresorhus/is';
import bunyan from 'bunyan'; import bunyan from 'bunyan';
import fs from 'fs-extra'; import fs from 'fs-extra';
import { clone } from '../util/clone'; import { clone } from '../util/clone';
...@@ -90,38 +91,54 @@ export default function prepareError(err: Error): Record<string, unknown> { ...@@ -90,38 +91,54 @@ export default function prepareError(err: Error): Record<string, unknown> {
return response; return response;
} }
export function sanitizeValue(_value: unknown, seen = new WeakMap()): any { type NestedValue = unknown[] | object;
let value = _value;
if (Array.isArray(value)) { function isNested(value: unknown): value is NestedValue {
const length = value.length; return is.array(value) || is.object(value);
const arrayResult = Array(length); }
seen.set(value, arrayResult);
for (let idx = 0; idx < length; idx += 1) { export function sanitizeValue(
const val = value[idx]; value: unknown,
arrayResult[idx] = seen.has(val) seen = new WeakMap<NestedValue, unknown>()
? seen.get(val) ): any {
: sanitizeValue(val, seen); if (is.string(value)) {
} return sanitize(value);
return arrayResult;
} }
if (value instanceof Buffer) { if (is.date(value)) {
return '[content]'; return value;
} }
if (value instanceof Error) { if (is.function_(value)) {
value = prepareError(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 (is.array(value)) {
if (value instanceof Date) { const length = value.length;
return value; 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> = {}; const objectResult: Record<string, any> = {};
seen.set(value as any, objectResult); seen.set(value, objectResult);
for (const [key, val] of Object.entries<any>(value)) { for (const [key, val] of Object.entries<any>(value)) {
let curValue: any; let curValue: any;
if (!val) { if (!val) {
...@@ -143,10 +160,11 @@ export function sanitizeValue(_value: unknown, seen = new WeakMap()): any { ...@@ -143,10 +160,11 @@ export function sanitizeValue(_value: unknown, seen = new WeakMap()): any {
objectResult[key] = curValue; objectResult[key] = curValue;
} }
return objectResult; return objectResult;
} }
return valueType === 'string' ? sanitize(value as string) : value; return value;
} }
export function withSanitizer(streamConfig: bunyan.Stream): bunyan.Stream { export function withSanitizer(streamConfig: bunyan.Stream): bunyan.Stream {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment