Skip to content

Commit

Permalink
feat: redact secrets and keys from logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jakowenko committed Aug 7, 2021
1 parent 9bcf8fa commit 0f3ef02
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions api/package-lock.json

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

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"multer": "^1.4.2",
"node-schedule": "^2.0.0",
"sharp": "^0.28.3",
"traverse": "^0.6.6",
"uuid": "^8.3.2",
"winston": "^3.3.3"
}
Expand Down
3 changes: 2 additions & 1 deletion api/src/util/logger.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ const { createLogger, format, transports } = require('winston');
const util = require('util');
const { core: SYSTEM_CORE } = require('../constants/system');
const mqtt = require('./mqtt.util');
const redact = require('./redact-secrets.util');

const combineMessageAndSplat = () => {
return {
transform: (info /* , opts */) => {
info.message = util.format(info.message, ...(info[Symbol.for('splat')] || []));
info.message = util.format(redact(info.message), ...redact(info[Symbol.for('splat')] || []));
return info;
},
};
Expand Down
27 changes: 27 additions & 0 deletions api/src/util/redact-secrets.util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const traverse = require('traverse');

const KEYS = [
// generic
/passw(or)?d/i,
/key/,
/^pw$/,
/^pass$/i,
/secret/i,
/token/i,
/api[-._]?key/i,
/session[-._]?id/i,
// specific
/^connect\.sid$/, // https://github.com/expressjs/session
];

const key = (str) =>
KEYS.some((regex) => {
return regex.test(str);
});

module.exports = (obj, value = '********') => {
// eslint-disable-next-line array-callback-return
return traverse(obj).map(function redact(/* val */) {
if (key(this.key)) this.update(value);
});
};

0 comments on commit 0f3ef02

Please sign in to comment.