Skip to content

Commit

Permalink
feat: mask sensitive data in logs (#4630)
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent authored Dec 10, 2024
1 parent 9f830b9 commit 29e8136
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
3 changes: 3 additions & 0 deletions lib/codecept.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ class Codecept {

// debug mode
global.debugMode = false;

// mask sensitive data
global.maskSensitiveData = this.config.maskSensitiveData || false;
}
}

Expand Down
16 changes: 12 additions & 4 deletions lib/output.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const colors = require('chalk');
const figures = require('figures');
const { maskSensitiveData } = require('invisi-data')

const styles = {
error: colors.bgRed.white.bold,
Expand Down Expand Up @@ -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}`));
}
},

Expand All @@ -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)));
}
},

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -167,7 +171,7 @@ module.exports = {
scenario: {
/**
* @param {Mocha.Test} test
*/
*/

started(test) {},

Expand Down Expand Up @@ -254,3 +258,7 @@ function truncate(msg, gap = 0) {
}
return msg;
}

function isMaskedData() {
return global.maskSensitiveData === true || false
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 29e8136

Please sign in to comment.