|
| 1 | +<?php |
| 2 | +declare(strict_types = 1); |
| 3 | + |
| 4 | +namespace Spaze\PhpInfo; |
| 5 | + |
| 6 | +use Tester\Assert; |
| 7 | +use Tester\TestCase; |
| 8 | + |
| 9 | +require __DIR__ . '/bootstrap.php'; |
| 10 | + |
| 11 | +class SensitiveValueSanitizerTest extends TestCase |
| 12 | +{ |
| 13 | + |
| 14 | + private const SESSION_ID = 'foobar,baz'; |
| 15 | + |
| 16 | + private string $string; |
| 17 | + |
| 18 | + |
| 19 | + public function __construct() |
| 20 | + { |
| 21 | + $this->string = sprintf('waldo %s fred %s quux', self::SESSION_ID, urlencode(self::SESSION_ID)); |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + session_set_save_handler(new TestSessionHandler(self::SESSION_ID)); |
| 28 | + session_start(); |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + protected function tearDown(): void |
| 33 | + { |
| 34 | + session_destroy(); |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + public function testSanitize(): void |
| 39 | + { |
| 40 | + $string = (new SensitiveValueSanitizer())->sanitize($this->string); |
| 41 | + Assert::contains('waldo', $string); |
| 42 | + Assert::notContains(self::SESSION_ID, $string); |
| 43 | + Assert::notContains(urlencode(self::SESSION_ID), $string); |
| 44 | + Assert::contains('[***]', $string); |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + public function testSanitizeSessionIdCustomReplacement(): void |
| 49 | + { |
| 50 | + $sanitizer = new SensitiveValueSanitizer(); |
| 51 | + $sanitizer->addSanitization(self::SESSION_ID, 'yeah, sure'); |
| 52 | + $string = $sanitizer->sanitize($this->string); |
| 53 | + Assert::contains('waldo', $string); |
| 54 | + Assert::notContains(self::SESSION_ID, $string); |
| 55 | + Assert::notContains(urlencode(self::SESSION_ID), $string); |
| 56 | + Assert::contains('yeah, sure', $string); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + public function testSanitizeDoNotSanitizeSessionIdButWhy(): void |
| 61 | + { |
| 62 | + $string = (new SensitiveValueSanitizer())->doNotSanitizeSessionId()->sanitize($this->string); |
| 63 | + Assert::contains('waldo', $string); |
| 64 | + Assert::contains(self::SESSION_ID, $string); |
| 65 | + Assert::contains(urlencode(self::SESSION_ID), $string); |
| 66 | + Assert::notContains('[***]', $string); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + public function testSanitizeAddSanitization(): void |
| 71 | + { |
| 72 | + $string = (new SensitiveValueSanitizer())->addSanitization('setec', '🍌')->sanitize('setec astronomy'); |
| 73 | + Assert::notContains('setec', $string); |
| 74 | + Assert::contains('🍌', $string); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + public function testGetHtmlNumericSessionId(): void |
| 79 | + { |
| 80 | + $sessionId = '31337'; |
| 81 | + |
| 82 | + // Set a new session id |
| 83 | + session_destroy(); |
| 84 | + session_set_save_handler(new TestSessionHandler($sessionId)); |
| 85 | + session_start(); |
| 86 | + |
| 87 | + Assert::noError(function () use ($sessionId, &$html): void { |
| 88 | + $html = (new SensitiveValueSanitizer())->sanitize("31336 + 1 = {$sessionId}"); |
| 89 | + }); |
| 90 | + Assert::notContains($sessionId, $html); |
| 91 | + } |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +(new SensitiveValueSanitizerTest())->run(); |
0 commit comments