Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ Other knobs:
- `withKeyMatcher()` / `withValueMatcher()` — replace detection entirely with
your own `KeyMatcherInterface` / `ValueMatcherInterface`.
- `withoutValueMatching()` — key-based masking only (skip value detection).
- `withoutValuePatterns(['email', 'iban'])` — drop specific default value
patterns by name (keeps the others and card detection).
- `matchKeysExactly()` — whole-string key matching instead of segment-aware
(no compound-key detection, fewer false positives).
- `maskMessage(false)` — stop masking the log message.
Expand Down
25 changes: 24 additions & 1 deletion src/MaskerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tiime\MonologMasker;

use Tiime\MonologMasker\Config\DefaultValuePatterns;
use Tiime\MonologMasker\Masker\Masker;
use Tiime\MonologMasker\Matcher\ChainValueMatcher;
use Tiime\MonologMasker\Matcher\CreditCardMatcher;
Expand Down Expand Up @@ -33,6 +34,7 @@ final class MaskerBuilder
* @param list<string> $additionalKeys
* @param array<string, string> $additionalPatterns
* @param list<string> $jsonKeys
* @param list<string> $excludedPatterns
*/
private function __construct(
private readonly ?KeyMatcherInterface $keyMatcher = null,
Expand All @@ -46,6 +48,7 @@ private function __construct(
private readonly bool $maskMessage = true,
private readonly bool $traverseObjects = true,
private readonly array $jsonKeys = [],
private readonly array $excludedPatterns = [],
) {
}

Expand Down Expand Up @@ -103,6 +106,18 @@ public function withValuePatterns(array $patterns): self
return $this->cloneWith(additionalPatterns: [...$this->additionalPatterns, ...$patterns]);
}

/**
* Drops default value patterns by name (keys of {@see DefaultValuePatterns}),
* e.g. ['email', 'iban']. Other defaults and card detection stay active.
* Unknown names are ignored. Accumulates across calls.
*
* @param list<string> $names
*/
public function withoutValuePatterns(array $names): self
{
return $this->cloneWith(excludedPatterns: [...$this->excludedPatterns, ...$names]);
}

/**
* Replaces the value matcher entirely (ignores the default patterns and
* card detection).
Expand Down Expand Up @@ -175,8 +190,13 @@ private function defaultKeyMatcher(): KeyMatcherInterface

private function defaultValueMatcher(): ValueMatcherInterface
{
$patterns = DefaultValuePatterns::all();
foreach ($this->excludedPatterns as $name) {
unset($patterns[$name]);
}

return new ChainValueMatcher(
RegexValueMatcher::withDefaults($this->additionalPatterns),
new RegexValueMatcher([...$patterns, ...$this->additionalPatterns]),
new CreditCardMatcher(),
);
}
Expand All @@ -185,6 +205,7 @@ private function defaultValueMatcher(): ValueMatcherInterface
* @param list<string>|null $additionalKeys
* @param array<string, string>|null $additionalPatterns
* @param list<string>|null $jsonKeys
* @param list<string>|null $excludedPatterns
*/
private function cloneWith(
?KeyMatcherInterface $keyMatcher = null,
Expand All @@ -198,6 +219,7 @@ private function cloneWith(
?bool $maskMessage = null,
?bool $traverseObjects = null,
?array $jsonKeys = null,
?array $excludedPatterns = null,
): self {
return new self(
$keyMatcher ?? $this->keyMatcher,
Expand All @@ -211,6 +233,7 @@ private function cloneWith(
$maskMessage ?? $this->maskMessage,
$traverseObjects ?? $this->traverseObjects,
$jsonKeys ?? $this->jsonKeys,
$excludedPatterns ?? $this->excludedPatterns,
);
}
}
103 changes: 103 additions & 0 deletions tests/MaskerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,90 @@ public function testWithValuePatternsAccumulatesAcrossCalls(): void
self::assertSame(['x' => '***', 'y' => '***'], $result);
}

public function testWithoutValuePatternsDropsNamedDefault(): void
{
$masker = MaskerBuilder::create()
->withoutValuePatterns(['email'])
->withStrategy(new FullMaskStrategy('***'))
->buildMasker();

$result = $masker->mask([
'contact' => 'john.doe@example.com',
'card' => '4242424242424242',
]);

// The excluded default no longer matches, but other defaults (and card
// detection) stay active.
self::assertSame('john.doe@example.com', $result['contact']);
self::assertSame('***', $result['card']);
}

public function testWithoutValuePatternsKeepsAddedPatterns(): void
{
$masker = MaskerBuilder::create()
->withValuePatterns(['fr_phone' => '/\b0[1-9](?:\d{2}){4}\b/'])
->withoutValuePatterns(['email'])
->withStrategy(new FullMaskStrategy('***'))
->buildMasker();

$result = $masker->mask([
'phone' => '0612345678',
'email' => 'john.doe@example.com',
]);

// Added pattern still masks; the excluded default does not.
self::assertSame('***', $result['phone']);
self::assertSame('john.doe@example.com', $result['email']);
}

public function testWithoutValuePatternsDropsSeveralNamesAtOnce(): void
{
$masker = MaskerBuilder::create()
->withoutValuePatterns(['email', 'iban'])
->withStrategy(new FullMaskStrategy('***'))
->buildMasker();

$result = $masker->mask([
'email' => 'john.doe@example.com',
'iban' => 'FR7630006000011234567890189',
]);

// Every name passed in a single call is dropped, not just the first.
self::assertSame('john.doe@example.com', $result['email']);
self::assertSame('FR7630006000011234567890189', $result['iban']);
}

public function testWithoutValuePatternsAccumulatesAcrossCalls(): void
{
$masker = MaskerBuilder::create()
->withoutValuePatterns(['email'])
->withoutValuePatterns(['iban'])
->withStrategy(new FullMaskStrategy('***'))
->buildMasker();

$result = $masker->mask([
'email' => 'john.doe@example.com',
'iban' => 'FR7630006000011234567890189',
]);

// Both excluded names are dropped.
self::assertSame('john.doe@example.com', $result['email']);
self::assertSame('FR7630006000011234567890189', $result['iban']);
}

public function testWithoutValuePatternsIgnoresUnknownName(): void
{
$masker = MaskerBuilder::create()
->withoutValuePatterns(['does_not_exist'])
->withStrategy(new FullMaskStrategy('***'))
->buildMasker();

$result = $masker->mask(['contact' => 'john.doe@example.com']);

// An unknown name is a no-op; known defaults still apply.
self::assertSame('***', $result['contact']);
}

public function testLatestKeyMatcherWins(): void
Comment thread
rflavien marked this conversation as resolved.
Resolved
{
$masker = MaskerBuilder::create()
Expand Down Expand Up @@ -262,6 +346,25 @@ public function testMasksMessageByDefaultViaBuilder(): void
self::assertSame('mail ***', $processor($this->record())->message);
}

public function testWithoutValuePatternsAppliesToTheMessageToo(): void
{
$processor = MaskerBuilder::create()
->withoutValuePatterns(['email'])
->withStrategy(new FullMaskStrategy('***'))
->buildProcessor();

$record = new LogRecord(
datetime: new \DateTimeImmutable('@0'),
channel: 'app',
level: Level::Info,
message: 'mail john.doe@example.com card 4242424242424242',
);

// The exclusion flows through the message path, not just context:
// the email is left intact while card detection still redacts.
self::assertSame('mail john.doe@example.com card ***', $processor($record)->message);
}

public function testMessageMaskingCanBeReEnabledWithNoArgument(): void
{
$processor = MaskerBuilder::create()
Expand Down
Loading