From 3b841b96354ac2d92687e520b4ee4f24e07cce92 Mon Sep 17 00:00:00 2001 From: Nicky De Maeyer Date: Tue, 4 Apr 2023 09:50:27 +0200 Subject: [PATCH 1/2] [CLEARFACTS-8478] support int in param cleaning --- composer.json | 4 +- phpunit.xml.dist | 1 - src/Processor/SessionRequestProcessor.php | 35 +++++++-------- .../Processor/SessionRequestProcessorTest.php | 45 +++++++++++++++++++ tests/TestCase.php | 16 +++++++ 5 files changed, 79 insertions(+), 22 deletions(-) create mode 100644 tests/Processor/SessionRequestProcessorTest.php create mode 100644 tests/TestCase.php diff --git a/composer.json b/composer.json index 7825215..87318b5 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ { "type": "vcs", "url": "https://github.com/Clearfacts/cf-codestyle" } ], "require": { - "php" : "^7 || ^8", + "php" : "^7.4 || ^8", "ext-json" : "*", "monolog/monolog": "^1 || ^2 || ^3", "symfony/http-foundation": "^3 || ^4 || ^5 || ^6", @@ -41,7 +41,7 @@ }, "autoload-dev": { "psr-4": { - "Tests\\DataLog\\" : "tests/" + "Tests\\Datalog\\" : "tests/" } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 4fab4cf..73d897a 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -10,7 +10,6 @@ convertWarningsToExceptions = "true" processIsolation = "false" stopOnFailure = "false" - syntaxCheck = "false" bootstrap = "vendor/autoload.php"> diff --git a/src/Processor/SessionRequestProcessor.php b/src/Processor/SessionRequestProcessor.php index c7f59af..11de87b 100644 --- a/src/Processor/SessionRequestProcessor.php +++ b/src/Processor/SessionRequestProcessor.php @@ -4,28 +4,28 @@ namespace Datalog\Processor; -use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\SessionInterface; class SessionRequestProcessor { - private $session; + private SessionInterface $session; private $sessionId; private $requestId; private $_server; private $_get; private $_post; - public function __construct(Session $session) + public function __construct(SessionInterface $session) { $this->session = $session; } - public function processRecord(array $record) + public function processRecord(array $record): array { if (null === $this->requestId) { $this->requestId = substr(uniqid(), -8); - if ('cli' === php_sapi_name()) { + if ('cli' === PHP_SAPI) { $this->sessionId = getmypid(); } else { try { @@ -51,7 +51,7 @@ public function processRecord(array $record) $record['request_id'] = $this->requestId; $record['session_id'] = $this->sessionId; - if (!'cli' === php_sapi_name()) { + if ('cli' !== PHP_SAPI) { $record['http.url'] = $this->_server['http.url']; $record['http.method'] = $this->_server['http.method']; $record['http.useragent'] = $this->_server['http.useragent']; @@ -62,19 +62,16 @@ public function processRecord(array $record) return $record; } - protected function clean($array) + protected function clean($array): array { - $toReturn = []; - foreach (array_keys($array) as $key) { - if (false !== strpos($key, 'password')) { - // Do not add - } elseif (false !== strpos($key, 'csrf_token')) { - // Do not add - } else { - $toReturn[$key] = $array[$key]; - } - } - - return $toReturn; + return array_filter( + $array, + static fn ($key) => + !(is_string($key) + && ( + false !== strpos($key, 'password') || false !== strpos($key, 'csrf_token') + )), + ARRAY_FILTER_USE_KEY, + ); } } diff --git a/tests/Processor/SessionRequestProcessorTest.php b/tests/Processor/SessionRequestProcessorTest.php new file mode 100644 index 0000000..ec561b8 --- /dev/null +++ b/tests/Processor/SessionRequestProcessorTest.php @@ -0,0 +1,45 @@ +processor = new SessionRequestProcessor( + $this->createMock(SessionInterface::class) + ); + } + + public function testCleansParamKeys(): void + { + $params = [ + 'foo' => 'bar', + 'test password test' => 'password', + 1 => 'one', + 'tester csrf_token tester' => 'csrf_token', + 'baz' => [ + 'qux' => 'quux', + ], + 'password' => 'password', + 'password test' => 'password', + ]; + + $cleanedParams = self::callPrivateMethod($this->processor, 'clean', $params); + + $this->assertSame([ + 'foo' => 'bar', + 1 => 'one', + 'baz' => [ + 'qux' => 'quux', + ], + ], $cleanedParams); + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..fb04185 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,16 @@ +getMethod($methodName); + $method->setAccessible(true); + + return $method->invokeArgs($object, $params); + } +} \ No newline at end of file From 378b02ac65b9dd14111b83d91821e770e8d83715 Mon Sep 17 00:00:00 2001 From: Nicky De Maeyer Date: Tue, 4 Apr 2023 10:13:19 +0200 Subject: [PATCH 2/2] [CLEARFACTS-8478] codestyle --- tests/Processor/SessionRequestProcessorTest.php | 1 + tests/TestCase.php | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/Processor/SessionRequestProcessorTest.php b/tests/Processor/SessionRequestProcessorTest.php index ec561b8..5090d6a 100644 --- a/tests/Processor/SessionRequestProcessorTest.php +++ b/tests/Processor/SessionRequestProcessorTest.php @@ -1,4 +1,5 @@