Fix RedactingFormatter missing secrets nested in array-valued sensitive keys

RedactingFormatter::collect() only added a sensitive key's value to the
secret list when it was a string or Stringable. When the value was an
array, addSecret() silently dropped it, so none of its leaf strings were
collected. PsrLogMessageProcessor json-encodes array context values when
interpolating them into the message, so a secret nested that way could
already be sitting in the message in plain text by the time this formatter
ran, and the output sweep had nothing to remove it with.

Add collectSensitiveValue() to walk a sensitive key's value and collect
every string leaf, however deeply nested, and use it instead of calling
addSecret() directly.
This commit is contained in:
dualfroz committed 2026-09-04 08:38:50 +02:00
1 parent fb5cc5f9ee
commit 7f7dc9b07c
3 files changed
+127 -2

No files matched your search

@@ -243,6 +243,23 @@ class RedactingFormatterTest extends MonologTestCase
$this->assertStringContainsString('calling api with [REDACTED]', $output);
}
public function testRedactsSecretNestedInAnArrayValueAlreadyInterpolatedIntoTheMessage()
{
// PsrLogMessageProcessor json-encodes array context values into the message, so a
// secret nested inside an array under a sensitive key leaks there before this
// formatter ever sees the record; sweeping has to catch it too, not just the key.
$formatter = new RedactingFormatter(new LineFormatter('%message% %context%', 'Y-m-d'));
$processor = new PsrLogMessageProcessor();
$output = $formatter->format($processor($this->getRecord(
message: 'password reset payload: {password}',
context: ['password' => ['new' => 'tok-supersecret-value']],
)));
$this->assertStringNotContainsString('tok-supersecret-value', $output);
$this->assertStringContainsString('[REDACTED]', $output);
}
public function testRedactsSecretEmbeddedInAnExceptionMessage()
{
$formatter = new RedactingFormatter(new JsonFormatter());