From ac078ccde7864d313daec95ce133565ba9d0ddb8 Mon Sep 17 00:00:00 2001 From: dkundel Date: Tue, 27 Aug 2019 15:56:09 -0700 Subject: [PATCH] fix(debug): fix redaction of logger overwriting values fix #78 --- __tests__/utils/debug.test.ts | 51 +++++++++++++++++++++++++++++++++++ src/utils/debug.ts | 26 +++++++++++++----- 2 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 __tests__/utils/debug.test.ts diff --git a/__tests__/utils/debug.test.ts b/__tests__/utils/debug.test.ts new file mode 100644 index 00000000..94d7d9b3 --- /dev/null +++ b/__tests__/utils/debug.test.ts @@ -0,0 +1,51 @@ +import { createRedactedObject, generalRedactor } from '../../src/utils/debug'; + +describe('generalRedactor', () => { + test('redacts the correct properties', () => { + const context = { + ACCOUNT_SID: 'ACxxxxxxx', + AUTH_TOKEN: 'auth-token', + SOME_URL: 'https://twilio.com/labs', + }; + + const redacted = generalRedactor(context); + expect(redacted.ACCOUNT_SID).toBe('ACxxxxxxx'); + expect(redacted.AUTH_TOKEN).toBe('[REDACTED]'); + expect(redacted.SOME_URL).toBe('https://twilio.com/labs'); + }); +}); + +describe('createRedactedObject', () => { + test('does not modify the original object', () => { + const context = { + ACCOUNT_SID: 'ACxxxxxxx', + AUTH_TOKEN: 'auth-token', + SOME_URL: 'https://twilio.com/labs', + env: { + ACCOUNT_SID: 'ACyyyyyyy', + AUTH_TOKEN: 'another-auth-token', + }, + }; + + const redacted = createRedactedObject(context, generalRedactor); + + expect(redacted).toEqual({ + ACCOUNT_SID: 'ACxxxxxxx', + AUTH_TOKEN: '[REDACTED]', + SOME_URL: 'https://twilio.com/labs', + env: { + ACCOUNT_SID: '[REDACTED]', + AUTH_TOKEN: '[REDACTED]', + }, + }); + expect(context).toEqual({ + ACCOUNT_SID: 'ACxxxxxxx', + AUTH_TOKEN: 'auth-token', + SOME_URL: 'https://twilio.com/labs', + env: { + ACCOUNT_SID: 'ACyyyyyyy', + AUTH_TOKEN: 'another-auth-token', + }, + }); + }); +}); diff --git a/src/utils/debug.ts b/src/utils/debug.ts index e91c8c5c..66928e71 100644 --- a/src/utils/debug.ts +++ b/src/utils/debug.ts @@ -28,15 +28,29 @@ export const generalRedactor = fastRedact({ 'TWILIO_API_SECRET', ]), ], -}); + serialize: false, +}) as (x: T) => T; export const allPropertiesRedactor = fastRedact({ paths: ['*'], -}); + serialize: false, +}) as (x: T) => T; + +export function copyObject(obj: object) { + return JSON.parse(JSON.stringify(obj)); +} + +export function createRedactedObject( + obj: object, + redactor: typeof generalRedactor +) { + const copiedObject = copyObject(obj); + return redactor(copiedObject); +} debug.formatters.P = function protectedFormatterMultiline(v: any): string { if (typeof v === 'object') { - v = JSON.parse(generalRedactor(v)); + v = createRedactedObject(v, generalRedactor); } return debug.formatters.O.bind(debug)(v); @@ -44,7 +58,7 @@ debug.formatters.P = function protectedFormatterMultiline(v: any): string { debug.formatters.p = function protectedFormatterSameline(v: any): string { if (typeof v === 'object') { - v = JSON.parse(generalRedactor(v)); + v = createRedactedObject(v, generalRedactor); } return debug.formatters.o.bind(debug)(v); @@ -52,14 +66,14 @@ debug.formatters.p = function protectedFormatterSameline(v: any): string { debug.formatters.R = function redactedFormatterMultiline(v: any): string { if (typeof v === 'object') { - v = JSON.parse(allPropertiesRedactor(v)); + v = createRedactedObject(v, allPropertiesRedactor); } return debug.formatters.O.bind(debug)(v); }; debug.formatters.r = function redactedFormatterSameline(v: any): string { if (typeof v === 'object') { - v = JSON.parse(allPropertiesRedactor(v)); + v = createRedactedObject(v, allPropertiesRedactor); } return debug.formatters.o.bind(debug)(v); };