From 19380066a80efe2cdf86f68e9978c0e3235c3119 Mon Sep 17 00:00:00 2001 From: Yuchen Liu Date: Fri, 13 Dec 2024 17:41:32 +0800 Subject: [PATCH] redact log output --- util/logger.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/util/logger.js b/util/logger.js index c3ec2f36..47d27d1d 100644 --- a/util/logger.js +++ b/util/logger.js @@ -21,6 +21,7 @@ const config = winston.config; const loggerManager = require('./log_mgr.js'); const path = require('path'); const fs = require('fs'); +const _ = require('lodash'); const moment = require('moment') @@ -40,6 +41,24 @@ if (process.env.FWDEBUG) { console.log("LOGGER SET TO", globalLogLevel); } +const keysToRedact = new Set(["password", "passwd", "psk", "key", "psks"]); +// pass in function arguments object and returns string with whitespaces +function redactLog(obj, redactRequired = false) { + if (!obj) + return obj; + // obj should be either object or array + try { + for (const key of Object.keys(obj)) { + if (_.isObject(obj[key]) || _.isArray(obj[key])) + redactLog(obj[key], redactRequired || keysToRedact.has(key)); + else { + if (redactRequired || keysToRedact.has(key)) + obj[key] = "*** redacted ***"; + } + } + } catch (err) {} +} + // pass in function arguments object and returns string with whitespaces function argumentsToString(v) { // convert arguments object to real array @@ -47,6 +66,8 @@ function argumentsToString(v) { for (var k in args) { if (typeof args[k] === "object") { // args[k] = JSON.stringify(args[k]); + args[k] = JSON.parse(JSON.stringify(args[k])); + redactLog(args[k]); args[k] = require('util').inspect(args[k], false, null, true); } }