From 54ea9a2bbd3cf374069e9aaf5f7ddda36cc67d45 Mon Sep 17 00:00:00 2001 From: Belle Aerni Date: Thu, 1 Aug 2024 14:19:12 -0700 Subject: [PATCH] Added a method to mask debug log parameters (#2377) * Added a method to mask debug log parameters Co-Authored-By: Christoph Schlaepfer <1939311+Anuril@users.noreply.github.com> * Slightly clean up Co-Authored-By: Christoph Schlaepfer <1939311+Anuril@users.noreply.github.com> --------- Co-authored-by: Christoph Schlaepfer <1939311+Anuril@users.noreply.github.com> --- src/library/Box/Log.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/library/Box/Log.php b/src/library/Box/Log.php index 487579059..d81a07088 100644 --- a/src/library/Box/Log.php +++ b/src/library/Box/Log.php @@ -47,6 +47,8 @@ class Box_Log implements FOSSBilling\InjectionAwareInterface protected array $_extras = []; protected string $_channel = 'application'; + private array $_maskedKeys = ['password', 'pass', 'token', 'key', 'apisecret', 'secret', 'api_token']; + public function setDi(Pimple\Container $di): void { $this->di = $di; @@ -57,19 +59,40 @@ class Box_Log implements FOSSBilling\InjectionAwareInterface return $this->di; } + private function maskParams(array|string $params, int $depthLimit = 15): array|string + { + if (is_string($params)) { + return $params; + } + + if ($depthLimit <= 0) { + return 'Recursion limit reached while masking event log parameters'; + } + + foreach ($params as $key => $value) { + if (is_array($value)) { + $params[$key] = $this->maskParams($value, $depthLimit - 1); + } elseif (in_array(strtolower($key), $this->_maskedKeys)) { + $params[$key] = '********'; + } + } + + return $params; + } + /** * @throws FOSSBilling\Exception */ public function __call($method, $params): void { $priority = strtoupper($method); + $params = $this->maskParams($params); if (($priority = array_search($priority, $this->_priorities)) !== false) { switch (is_countable($params) ? count($params) : 0) { case 0: throw new FOSSBilling\Exception('Missing log message'); case 1: $message = array_shift($params); - $extras = null; break; default: