-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathRedactSensitiveProcessorTest.php
More file actions
58 lines (48 loc) · 1.81 KB
/
Copy pathRedactSensitiveProcessorTest.php
File metadata and controls
58 lines (48 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
declare(strict_types=1);
namespace Monolog\Processor;
use Monolog\Level;
use Monolog\LogRecord;
use PHPUnit\Framework\TestCase;
final class RedactSensitiveProcessorTest extends TestCase
{
public function testRedactsContextAndExtraKeys(): void
{
$p = new RedactSensitiveProcessor();
$rec = new LogRecord(
datetime: new \DateTimeImmutable('@0'),
channel: 'test',
level: Level::Info,
message: 'Login for {user}',
context: ['user' => 'rishi', 'password' => 'super-secret', 'nested' => ['api_key' => 'abc123']],
extra: ['token' => 't-456', 'irrelevant' => 'keep']
);
$out = $p($rec);
$this->assertSame('REDACTED', $out->context['password']);
$this->assertSame('REDACTED', $out->context['nested']['api_key']);
$this->assertSame('REDACTED', $out->extra['token']);
$this->assertSame('keep', $out->extra['irrelevant']);
}
public function testRedactsWithPatterns(): void
{
$p = new RedactSensitiveProcessor(
sensitiveKeys: [],
patterns: ['/(Bearer\\s+)[A-Za-z0-9\\._-]+/i', '/([\\w.%+-]+@[\\w.-]+\\.[A-Za-z]{2,})/']
);
$rec = new LogRecord(
new \DateTimeImmutable('@0'), 'test', Level::Info,
'Auth {h}: Bearer abc.def-ghi and user john@example.com',
['h' => 'header'], []
);
$out = $p($rec);
$this->assertSame('Auth {h}: REDACTED and user REDACTED', $out->message);
}
public function testIgnoresInvalidRegexSafely(): void
{
$p = new RedactSensitiveProcessor([], ['/[invalid/']);
$rec = new LogRecord(new \DateTimeImmutable('@0'), 'test', Level::Info, 'hello', [], []);
$out = $p($rec);
$this->assertSame('hello', $out->message);
}
}
?>