-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dumper, BlueScreen: added Scrubber (#439)
- Loading branch information
Showing
6 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
test('dumper with default keysToHide scrubbing', function () { | ||
$blueScreen = new Tracy\BlueScreen; | ||
$dumper = $blueScreen->getDumper(); | ||
Assert::contains('foo', $dumper('foo', 'bar')); | ||
Assert::notContains('secret', $dumper('secret', 'password')); | ||
Assert::notContains('secret', $dumper('secret', 'PiN')); | ||
}); | ||
|
||
test('dumper with custom scrubbing', function () { | ||
$blueScreen = new Tracy\BlueScreen; | ||
$blueScreen->scrubber = function (string $k, $v = null): bool { | ||
return strtolower($k) === 'pin' || strtolower($k) === 'foo' || $v === 42; | ||
}; | ||
$dumper = $blueScreen->getDumper(); | ||
Assert::contains('foo', $dumper('foo', 'bar')); | ||
Assert::notContains('secret', $dumper('secret', 'password')); // default keysToHide | ||
|
||
Assert::notContains('secret', $dumper('secret', 'PiN')); // scrubbed by key | ||
Assert::notContains('42', $dumper(42, 'bar')); // scrubbed by value | ||
}); | ||
|
||
test('dumper with regexp scrubbing', function () { | ||
$blueScreen = new Tracy\BlueScreen; | ||
$blueScreen->scrubber = function (string $k): bool { | ||
return (bool) preg_match('#password#i', $k); | ||
}; | ||
$dumper = $blueScreen->getDumper(); | ||
Assert::contains('foo', $dumper('foo', 'bar')); | ||
Assert::notContains('secret', $dumper('secret', 'super_password')); | ||
|
||
$fix = [ | ||
'password' => 'secret', | ||
'foo' => 'foo ok', | ||
'password_check' => 'secret', | ||
'DATABASE_PASSWORD' => 'secret', | ||
]; | ||
|
||
Assert::notContains('secret', $dumper($fix)); | ||
Assert::contains('foo ok', $dumper($fix)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Tester\Assert; | ||
use Tracy\Dumper; | ||
|
||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
$obj = (object) [ | ||
'a' => 456, | ||
'password' => 'secret1', | ||
'PASSWORD' => 'secret2', | ||
'Pin' => 'secret3', | ||
'foo' => 'bar', | ||
'q' => 42, | ||
'inner' => [ | ||
'a' => 123, | ||
'password' => 'secret4', | ||
'PASSWORD' => 'secret5', | ||
'Pin' => 'secret6', | ||
'bar' => 42, | ||
], | ||
]; | ||
$scrubber = function (string $k, $v = null): bool { | ||
return strtolower($k) === 'pin' || strtolower($k) === 'foo' || $v === 42; | ||
}; | ||
$expect1 = <<<'XX' | ||
stdClass #%d% | ||
a: 456 | ||
password: 'secret1' | ||
PASSWORD: 'secret2' | ||
Pin: ***** (string) | ||
foo: ***** (string) | ||
q: ***** (integer) | ||
inner: array (5) | ||
| 'a' => 123 | ||
| 'password' => 'secret4' | ||
| 'PASSWORD' => 'secret5' | ||
| 'Pin' => ***** (string) | ||
| 'bar' => ***** (integer) | ||
XX; | ||
|
||
Assert::match($expect1, Dumper::toText($obj, [Dumper::SCRUBBER => $scrubber])); | ||
|
||
// scrubber works with "keys to hide" (back compatibility) | ||
$expect2 = <<<'XX' | ||
stdClass #%d% | ||
a: 456 | ||
password: ***** (string) | ||
PASSWORD: ***** (string) | ||
Pin: ***** (string) | ||
foo: ***** (string) | ||
q: ***** (integer) | ||
inner: array (5) | ||
| 'a' => 123 | ||
| 'password' => ***** (string) | ||
| 'PASSWORD' => ***** (string) | ||
| 'Pin' => ***** (string) | ||
| 'bar' => ***** (integer) | ||
XX; | ||
Assert::match($expect2, Dumper::toText($obj, [Dumper::SCRUBBER => $scrubber, Dumper::KEYS_TO_HIDE => ['password']])); |