From 004cc1eb0543b4dab5715b77b17da4ae4adb488b Mon Sep 17 00:00:00 2001 From: Benjamin Calef Date: Sun, 24 Nov 2024 22:21:49 +0100 Subject: [PATCH] [v2.0.3] add sensitive processor for logs --- Logger/SensitiveDataProcessor.php | 93 +++++++++++++++++++++++++++++++ composer.json | 2 +- etc/di.xml | 4 +- 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 Logger/SensitiveDataProcessor.php diff --git a/Logger/SensitiveDataProcessor.php b/Logger/SensitiveDataProcessor.php new file mode 100644 index 0000000..7623b7d --- /dev/null +++ b/Logger/SensitiveDataProcessor.php @@ -0,0 +1,93 @@ + + * @copyright 2024 Zepgram Copyright (c) (https://github.com/zepgram) + * @license MIT License + **/ + +declare(strict_types=1); + +namespace Zepgram\Rest\Logger; + +use Monolog\Processor\ProcessorInterface; + +class SensitiveDataProcessor implements ProcessorInterface +{ + private $sensitiveKeyPattern; + + public function __construct( + private array $sensitiveKeys = [], + private array $overrideSensitiveKeys = [], + private string $redactionPlaceholder = '***REDACTED***', + private ?bool $isEnabled = null, + ) { + $defaultSensitiveKeys = [ + 'password', + 'username', + 'user', + 'token', + 'key', + 'secret', + 'hash', + 'hmac', + 'sha', + 'sign', + 'authorization', + 'jwt', + 'access', + 'auth', + 'sso', + 'passphrase', + 'ssh', + 'pin', + 'cvv', + 'ccv', + 'cvc', + 'card' + ]; + $this->isEnabled = $isEnabled ?? (getenv('MAGE_MODE') === 'production'); + $this->sensitiveKeys = array_unique(array_merge($defaultSensitiveKeys, $sensitiveKeys)); + $this->sensitiveKeys = $this->overrideSensitiveKeys ?: $this->sensitiveKeys; + $this->sensitiveKeyPattern = '/' . implode('|', array_map('preg_quote', $this->sensitiveKeys)) . '/i'; + } + + public function __invoke(array $record): array + { + if (!$this->isEnabled) { + return $record; + } + + foreach ($record as &$line) { + $line = $this->redactSensitiveData($line); + } + + return $record; + } + + private function redactSensitiveData(mixed $data): mixed + { + if (is_array($data)) { + foreach ($data as $key => &$value) { + if (is_array($value)) { + $value = $this->redactSensitiveData($value); + } elseif ($key && is_string($key) && $this->isSensitiveKey($key)) { + $value = $this->redactionPlaceholder; + } + } + return $data; + } + + return $data; + } + + private function isSensitiveKey(string $key): bool + { + return preg_match($this->sensitiveKeyPattern, $key) === 1; + } +} diff --git a/composer.json b/composer.json index 2e31462..f68f1e0 100755 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "zepgram/module-rest", "description": "Technical module to industrialize API REST call with dependency injection pattern using Guzzle library", "type": "magento2-module", - "version": "2.0.2", + "version": "2.0.3", "authors": [ { "name": "Benjamin Calef", diff --git a/etc/di.xml b/etc/di.xml index afd76a5..90b8015 100755 --- a/etc/di.xml +++ b/etc/di.xml @@ -24,6 +24,9 @@ Zepgram\Rest\Logger\Handler + + Zepgram\Rest\Logger\SensitiveDataProcessor + @@ -33,7 +36,6 @@ -