From 611464c6669038d5130ee0f21a8fc64f876a6d5a Mon Sep 17 00:00:00 2001 From: Grzegorz Rola <grola@seostudio.pl> Date: Mon, 18 Mar 2024 18:16:26 +0100 Subject: [PATCH] feature(processor): sensitive data processor --- CHANGELOG.md | 4 ++ composer.json | 9 ++-- src/Processor/SensitiveDataProcessor.php | 51 ++++++++++++++++++ .../Processor/TestSensitiveDataProcessor.php | 54 +++++++++++++++++++ tests/unit/bootstrap.php | 17 ++++++ 5 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 src/Processor/SensitiveDataProcessor.php create mode 100644 tests/unit/Processor/TestSensitiveDataProcessor.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 838f8bd..a295bc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [1.12.0] - 2023-07-10 +### Added +- sensitive data replacer + ## [1.11.0] - 2023-07-10 ### Updated - monolog to 2.9.1 diff --git a/composer.json b/composer.json index f94603a..cdfb1a7 100644 --- a/composer.json +++ b/composer.json @@ -8,22 +8,23 @@ ], "config": { "platform": { - "php": "7.2" + "php": "7.4" }, "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true } }, "require": { - "php": ">=7.2|^8", + "php": ">=7.4|^8", "psr/log": "^1", "monolog/monolog": "^2.9.1", "wpdesk/wp-notice": "^3.0" }, "require-dev": { - "phpunit/phpunit": "^5", + "phpunit/phpunit": "^5|^6|^7|^8|^9", "squizlabs/php_codesniffer": "^3.0.2", - "wpdesk/wp-code-sniffer": "^1.2.3" + "wpdesk/wp-code-sniffer": "^1.2.3", + "10up/wp_mock": "^1.0" }, "autoload": { "psr-4": {"WPDesk\\Logger\\": "src/"} diff --git a/src/Processor/SensitiveDataProcessor.php b/src/Processor/SensitiveDataProcessor.php new file mode 100644 index 0000000..f0b6b82 --- /dev/null +++ b/src/Processor/SensitiveDataProcessor.php @@ -0,0 +1,51 @@ +<?php + +namespace WPDesk\Logger\Processor; + +use Monolog\Processor\ProcessorInterface; + +/** + * Can replace data in log. + * Ie. sensitive data. + * + * @package WPDesk\Logger\Processor + */ +class SensitiveDataProcessor implements ProcessorInterface { + + /** + * Replace array. + * + * @var array + */ + private array $replace; + + public function __construct( array $replace ) { + $this->replace = $replace; + } + + public function __invoke( array $record ): array { + return $this->replace_array( $record ); + } + + private function replace_array( array $value ): array { + foreach ( $value as $key => $item ) { + if ( is_array( $item ) ) { + $value[ $key ] = $this->replace_array( $item ); + } + if ( is_string( $item ) ) { + $value[ $key ] = $this->replace( $item ); + } + } + + return $value; + } + + private function replace( string $value ): string { + foreach ( $this->replace as $search => $replace ) { + $value = str_replace( $search, $replace, $value ); + } + + return $value; + } + +} diff --git a/tests/unit/Processor/TestSensitiveDataProcessor.php b/tests/unit/Processor/TestSensitiveDataProcessor.php new file mode 100644 index 0000000..8bc6444 --- /dev/null +++ b/tests/unit/Processor/TestSensitiveDataProcessor.php @@ -0,0 +1,54 @@ +<?php + +namespace unit\Processor; + +use WP_Mock; +use WP_Mock\Tools\TestCase; +use WPDesk\Logger\Processor\SensitiveDataProcessor; + +class TestSensitiveDataProcessor extends TestCase +{ + + public function setUp(): void { + WP_Mock::setUp(); + } + + public function tearDown(): void { + WP_Mock::tearDown(); + } + + public function testShouldReplaceWhenMatched() { + $processor = new SensitiveDataProcessor( array( 'password' => '***' ) ); + $record = array( + 'password' => 'password', + 'array' => array( + 'password' => 'password', + ), + ); + $expected = array( + 'password' => '***', + 'array' => array( + 'password' => '***', + ), + ); + $this->assertEquals( $expected, $processor->__invoke( $record ) ); + } + + public function testShouldNotReplaceWhenNotMatched() { + $processor = new SensitiveDataProcessor( array( 'password' => '***' ) ); + $record = array( + 'text' => 'text', + 'array' => array( + 'text' => 'text', + ), + ); + $expected = array( + 'text' => 'text', + 'array' => array( + 'text' => 'text', + ), + ); + $this->assertEquals( $expected, $processor->__invoke( $record ) ); + } + +} diff --git a/tests/unit/bootstrap.php b/tests/unit/bootstrap.php index 575b33b..cb59777 100644 --- a/tests/unit/bootstrap.php +++ b/tests/unit/bootstrap.php @@ -4,3 +4,20 @@ */ require_once __DIR__ . '/../../vendor/autoload.php'; + +error_reporting( E_ALL & ~E_DEPRECATED ); + +if ( getenv( 'PLUGIN_PATH' ) !== false ) { + define( 'PLUGIN_PATH', getenv( 'PLUGIN_PATH' ) ); +} else { + define( 'PLUGIN_PATH', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR ); +} + +if ( getenv( 'ABSPATH' ) !== false ) { + define( 'ABSPATH', getenv( 'ABSPATH' ) ); +} else { + define( 'ABSPATH', PLUGIN_PATH . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR ); +} + +WP_Mock::setUsePatchwork( true ); +WP_Mock::bootstrap(); -- GitLab