From ed1857edd408758ee800261e453545b20d669317 Mon Sep 17 00:00:00 2001 From: rflavien Date: Thu, 18 Jun 2026 17:38:41 +0200 Subject: [PATCH 1/2] feat(masker): add withoutValuePatterns to drop default value patterns by name Symmetric to withValuePatterns: lets callers exclude default value patterns by name (e.g. email, iban) while keeping the other defaults and Luhn card detection. Previously this required replacing the whole value matcher via withValueMatcher, losing every other default. Co-Authored-By: Claude Opus 4.8 (1M context) --- README.md | 2 + src/MaskerBuilder.php | 25 ++++++++++- tests/MaskerBuilderTest.php | 84 +++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 72dd3f4..8b016a4 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/MaskerBuilder.php b/src/MaskerBuilder.php index 65e4d56..be77b6c 100644 --- a/src/MaskerBuilder.php +++ b/src/MaskerBuilder.php @@ -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; @@ -33,6 +34,7 @@ final class MaskerBuilder * @param list $additionalKeys * @param array $additionalPatterns * @param list $jsonKeys + * @param list $excludedPatterns */ private function __construct( private readonly ?KeyMatcherInterface $keyMatcher = null, @@ -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 = [], ) { } @@ -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 $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). @@ -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(), ); } @@ -185,6 +205,7 @@ private function defaultValueMatcher(): ValueMatcherInterface * @param list|null $additionalKeys * @param array|null $additionalPatterns * @param list|null $jsonKeys + * @param list|null $excludedPatterns */ private function cloneWith( ?KeyMatcherInterface $keyMatcher = null, @@ -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, @@ -211,6 +233,7 @@ private function cloneWith( $maskMessage ?? $this->maskMessage, $traverseObjects ?? $this->traverseObjects, $jsonKeys ?? $this->jsonKeys, + $excludedPatterns ?? $this->excludedPatterns, ); } } diff --git a/tests/MaskerBuilderTest.php b/tests/MaskerBuilderTest.php index 7bb83e1..b0502af 100644 --- a/tests/MaskerBuilderTest.php +++ b/tests/MaskerBuilderTest.php @@ -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 { $masker = MaskerBuilder::create() From cc2f45bc5a3bf071bc6301c99749dec87d414eeb Mon Sep 17 00:00:00 2001 From: rflavien Date: Thu, 18 Jun 2026 18:01:49 +0200 Subject: [PATCH 2/2] test(masker): cover message path for withoutValuePatterns The default value matcher also masks the log message via MaskingProcessor, so assert the exclusion flows through that path: an excluded email is left intact in the message while card detection still redacts. Co-Authored-By: Claude Opus 4.8 (1M context) --- tests/MaskerBuilderTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/MaskerBuilderTest.php b/tests/MaskerBuilderTest.php index b0502af..4ddf41d 100644 --- a/tests/MaskerBuilderTest.php +++ b/tests/MaskerBuilderTest.php @@ -346,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()