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..4ddf41d 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() @@ -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()