Skip to content
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

Implement support for error obfuscation #17

Merged
merged 7 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
118 changes: 69 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@luckbox/logger-factory",
"version": "3.1.0",
"version": "3.2.0",
"description": "Easy to use logger with several levels of logging as well as different adapters that can be used separately or in combinations",
"author": "Luckbox",
"license": "ISC",
Expand All @@ -20,7 +20,7 @@
},
"scripts": {
"build": "rm -rf dist/ && tsc -d",
"lint": "eslint \"**/*.ts\" --cache",
"lint": "eslint \"**/*.ts\"",
"lint:fix": "npm run lint -- --fix",
"test": "jest --collectCoverage",
"test:watch": "jest --watchAll",
Expand All @@ -41,11 +41,13 @@
"devDependencies": {
"@luckbox/eslint-rules": "^4.0.5",
"@types/jest": "^26.0.3",
"@types/lodash": "^4.14.168",
"@types/node": "^14.0.14",
"@typescript-eslint/eslint-plugin": "^4.12.0",
"@typescript-eslint/parser": "^4.12.0",
"coveralls": "^3.1.0",
"eslint": "^7.2.0",
"eslint-plugin-no-only-tests": "^2.4.0",
"eslint-plugin-node": "^11.1.0",
"husky": "^4.2.5",
"jest": "^26.1.0",
Expand Down
41 changes: 34 additions & 7 deletions src/Obfuscator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@ enum Tag {
SECRET = 'SECRET'
}

type PlainObject = Record<string, unknown>;

class Obfuscator {
public obfuscateObject(object: Record<string, unknown>, obfuscateSettings: Array<[string, Tag]>): Record<string, unknown> {
const clonedObj = lodash.cloneDeep(object);
const allPaths: Array<string> = this.collectPaths(object);
public obfuscateString(value: string, tag: Tag): string {
return `[${tag}]${value}[/${tag}]`;
}

public obfuscateObject<T extends PlainObject | Error>(object: T, obfuscateSettings: Array<[string, Tag]>): T {
if (this.isPlainObject(object)) {
return this.obfuscatePlainObject(object, obfuscateSettings);
}

return this.obfuscateError(object as Error, obfuscateSettings) as T;
nikksan marked this conversation as resolved.
Show resolved Hide resolved
}

private obfuscatePlainObject<T extends PlainObject>(plainObject: T, obfuscateSettings: Array<[string, Tag]>): T {
const clonedObj = lodash.cloneDeep(plainObject);
const allPaths: Array<string> = this.collectPaths(plainObject);

const pathToTagMap = new Map();
const allPathsToObfuscate = [];
Expand All @@ -27,16 +41,25 @@ class Obfuscator {
if (!tag) {
continue;
}
lodash.set(clonedObj, path, this.obfuscateString(rawValue, tag));
lodash.set(clonedObj, path, this.obfuscateString(rawValue as string, tag));
}
}

return clonedObj;
}

public obfuscateString(value: string, tag: Tag): string {
const upperCasedTag = tag.toUpperCase();
return `[${upperCasedTag}]${value}[/${upperCasedTag}]`;
private obfuscateError<T extends Error>(err: T, obfuscateSettings: Array<[string, Tag]>): T {
const clonedErr = new Error(err.message);
Object.setPrototypeOf(clonedErr, Object.getPrototypeOf(err));

const dataToAssign = {};
for (const prop of Object.getOwnPropertyNames(err)) {
dataToAssign[prop] = lodash.cloneDeep(err[prop]);
}

Object.assign(clonedErr, this.obfuscatePlainObject(dataToAssign, obfuscateSettings));

return clonedErr as T;
}

private collectPaths(input: any, currentPath?: string) {
Expand All @@ -62,6 +85,10 @@ class Obfuscator {
const tag = pathToTagMap.get(path);
return tag;
}

private isPlainObject(value: unknown): value is PlainObject {
return lodash.isPlainObject(value);
}
}

export {
Expand Down
2 changes: 1 addition & 1 deletion src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ describe('LoggerFactory', () => {

it('should not throw when stringifying big int', () => {
expect(() => {
consoleLogger.info({ id: BigInt(Number.MAX_SAFE_INTEGER + 1) })
consoleLogger.info({ id: BigInt(Number.MAX_SAFE_INTEGER + 1) });
}).not.toThrow();
});
});
Expand Down
36 changes: 35 additions & 1 deletion src/tests/unit-tests/Obfuscator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Obfuscator, Tag } from '../../Obfuscator';

describe('Obfuscator', () => {
const obfuscator = new Obfuscator();

describe('obfuscateString', () => {
it('should wrap the provided tag around the string that needs to be obfuscated', () => {
expect(obfuscator.obfuscateString('string', Tag.PII)).toEqual('[PII]string[/PII]');
Expand All @@ -26,5 +26,39 @@ describe('Obfuscator', () => {
const originalObject = { favouriteColor: 'red', nested: { field: 'value' } };
expect(obfuscator.obfuscateObject(originalObject, [['name', Tag.PII]])).toEqual(originalObject);
});

it('should return a copy of the error and not modify the original', () => {
const originalError = new Error();
const obfuscatedError = obfuscator.obfuscateObject(originalError, [['bar', Tag.PII]]);
expect(obfuscatedError).not.toBe(originalError);
});

it('should preserve the prototype, name, message and stack of the error', () => {
class CustomError extends Error {}
const originalError = new CustomError();
const obfuscatedError = obfuscator.obfuscateObject(originalError, [['bar', Tag.PII]]);

expect(obfuscatedError).toBeInstanceOf(CustomError);
expect(obfuscatedError.name).toEqual(originalError.name);
expect(obfuscatedError.message).toEqual(originalError.message);
expect(obfuscatedError.stack).toEqual(originalError.stack);
});

it('should obfuscate error specific props', () => {
class CustomError extends Error {
bar = 'foo'
foo = {
test: 'test',
}
}
const originalError = new CustomError();
const obfuscatedError = obfuscator.obfuscateObject(originalError, [
['bar', Tag.PII],
['foo.test', Tag.PII],
]);

expect(obfuscatedError.bar).toEqual('[PII]foo[/PII]');
expect(obfuscatedError.foo.test).toEqual('[PII]test[/PII]');
});
});
});