Skip to content

fix(debug): fix redaction of logger overwriting values #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions __tests__/utils/debug.test.ts
Original file line number Diff line number Diff line change
@@ -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',
},
});
});
});
26 changes: 20 additions & 6 deletions src/utils/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,52 @@ export const generalRedactor = fastRedact({
'TWILIO_API_SECRET',
]),
],
});
serialize: false,
}) as <T>(x: T) => T;

export const allPropertiesRedactor = fastRedact({
paths: ['*'],
});
serialize: false,
}) as <T>(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);
};

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);
};

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);
};