From ef59b43edfd4d141f771bf2e11b55451b540c914 Mon Sep 17 00:00:00 2001 From: Christoffer Rehn <97894666+BN-CRehn@users.noreply.github.com> Date: Wed, 17 Aug 2022 13:59:19 +0200 Subject: [PATCH] Dont log apikey in default logger - Bon-61533 (#31) * Dont log apikey - Bon-61533 --- lib/splat-entry.js | 4 +++- test/feature/logging-feature.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/splat-entry.js b/lib/splat-entry.js index 9f72e3c..c8776a5 100644 --- a/lib/splat-entry.js +++ b/lib/splat-entry.js @@ -15,7 +15,9 @@ function splatEntry(info) { info.metaData = message.pop(); } } - info.message = util.format(info.message, ...message); + const noapikey = + /(["]?(x-)?api-key["]?[:=]["]?)(([\s]?["]?[0-9a-fA-F]){8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}["]?)/gi; + info.message = util.format(info.message, ...message).replace(noapikey, /\1SECRET/); if (!config.pretty) { info.message = info.message.replace(/\n\s*/gm, " "); diff --git a/test/feature/logging-feature.js b/test/feature/logging-feature.js index 35207ce..9b6d711 100644 --- a/test/feature/logging-feature.js +++ b/test/feature/logging-feature.js @@ -68,6 +68,34 @@ Feature("Logging", () => { }); }); + Scenario("Logging an api-key", () => { + const message = "Message"; + const data = "x-api-key:8a1ba457-24bc-4941-b136-d401a717c223"; + + When("logging a huge message", () => { + logger.debug(message, data); + }); + + Then("log output should be trimmed", () => { + const logContent = transport.logs.shift(); + logContent.message.should.equal("Message /\\1SECRET/"); + }); + }); + + Scenario("Logging an api-key as message", () => { + const message = "x-api-key:8a1ba457-24bc-4941-b136-d401a717c223"; + const data = "some-data"; + + When("logging a huge message", () => { + logger.debug(message, data); + }); + + Then("log output should be trimmed", () => { + const logContent = transport.logs.shift(); + logContent.message.should.equal("/\\1SECRET/ some-data"); + }); + }); + Scenario("Should support prefixed package names", () => { let newLogger;