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

feat: mask sensitive data in logs #4630

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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
3 changes: 3 additions & 0 deletions lib/codecept.js
Original file line number Diff line number Diff line change
@@ -92,6 +92,9 @@ class Codecept {

// debug mode
global.debugMode = false;

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

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,
@@ -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
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
8 changes: 8 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -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.