From 572a8c7a87708340085c662087be861c4b1e239d Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Tue, 10 Dec 2024 13:14:50 +0100 Subject: [PATCH] feat: mask sensitive data in logs --- lib/codecept.js | 3 +++ lib/output.js | 16 ++++++++++++---- package.json | 1 + typings/index.d.ts | 8 ++++++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/codecept.js b/lib/codecept.js index fe749fd6e..47e0bbe5d 100644 --- a/lib/codecept.js +++ b/lib/codecept.js @@ -92,6 +92,9 @@ class Codecept { // debug mode global.debugMode = false; + + // mask sensitive data + global.maskSensitiveData = this.config.maskSensitiveData || false; } } diff --git a/lib/output.js b/lib/output.js index c0974be58..833046446 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1,5 +1,6 @@ const colors = require('chalk'); const figures = require('figures'); +const { maskSensitiveData } = require('invisi-data') const styles = { error: colors.bgRed.white.bold, @@ -57,8 +58,9 @@ module.exports = { * @param {string} msg */ debug(msg) { + const _msg = isMaskedData() ? maskSensitiveData(msg) : msg if (outputLevel >= 2) { - print(' '.repeat(this.stepShift), styles.debug(`${figures.pointerSmall} ${msg}`)); + print(' '.repeat(this.stepShift), styles.debug(`${figures.pointerSmall} ${_msg}`)); } }, @@ -67,8 +69,9 @@ module.exports = { * @param {string} msg */ log(msg) { + const _msg = isMaskedData() ? maskSensitiveData(msg) : msg if (outputLevel >= 3) { - print(' '.repeat(this.stepShift), styles.log(truncate(` ${msg}`, this.spaceShift))); + print(' '.repeat(this.stepShift), styles.log(truncate(` ${_msg}`, this.spaceShift))); } }, @@ -120,7 +123,8 @@ module.exports = { stepLine += colors.grey(step.comment.split('\n').join('\n' + ' '.repeat(4))); } - print(' '.repeat(this.stepShift), truncate(stepLine, this.spaceShift)); + const _stepLine = isMaskedData() ? maskSensitiveData(stepLine) : stepLine + print(' '.repeat(this.stepShift), truncate(_stepLine, this.spaceShift)); }, /** @namespace */ @@ -167,7 +171,7 @@ module.exports = { scenario: { /** * @param {Mocha.Test} test - */ + */ started(test) {}, @@ -254,3 +258,7 @@ function truncate(msg, gap = 0) { } return msg; } + +function isMaskedData() { + return global.maskSensitiveData === true || false +} diff --git a/package.json b/package.json index dc7e8df02..5304c95c4 100644 --- a/package.json +++ b/package.json @@ -147,6 +147,7 @@ "graphql": "16.9.0", "husky": "9.1.7", "inquirer-test": "2.0.1", + "invisi-data": "^1.0.0", "jsdoc": "4.0.4", "jsdoc-typeof-plugin": "1.0.0", "json-server": "0.17.4", diff --git a/typings/index.d.ts b/typings/index.d.ts index 1a576c883..9b8acc91c 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -114,6 +114,14 @@ declare namespace CodeceptJS { * ``` */ emptyOutputFolder?: boolean; + /** + * mask sensitive data in output logs + * + * ```js + * maskSensitiveData: true + * ``` + */ + maskSensitiveData?: boolean; /** * Pattern to filter tests by name. * This option is useful if you plan to use multiple configs for different environments.