fix(logger): Make the handling of SensitiveParameters consistent

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2024-03-28 11:10:39 +01:00
parent 28c8a46ef9
commit ca08437967
No known key found for this signature in database
GPG key ID: 74434EFE0D2E2205
2 changed files with 22 additions and 1 deletions

View file

@ -220,7 +220,9 @@ class ExceptionSerializer {
private function removeValuesFromArgs($args, $values): array {
$workArgs = [];
foreach ($args as $arg) {
if (in_array($arg, $values, true)) {
if (isset($arg['__class__']) && $arg['__class__'] === \SensitiveParameterValue::class) {
$arg = self::SENSITIVE_VALUE_PLACEHOLDER;
} elseif (in_array($arg, $values, true)) {
$arg = self::SENSITIVE_VALUE_PLACEHOLDER;
} elseif (is_array($arg)) {
$arg = $this->removeValuesFromArgs($arg, $values);

View file

@ -52,6 +52,14 @@ class ExceptionSerializerTest extends TestCase {
throw new \Exception('expected custom auth exception');
}
private function usingSensitiveParameterAttribute(
string $login,
#[\SensitiveParameter]
string $parole,
): void {
throw new \Exception('SensitiveParameter attribute');
}
/**
* this test ensures that the serializer does not overwrite referenced
* variables. It is crafted after a scenario we experienced: the DAV server
@ -81,4 +89,15 @@ class ExceptionSerializerTest extends TestCase {
$this->assertFalse(isset($serializedData['Trace'][0]['args'][1]));
}
}
public function testSensitiveParameterAttribute(): void {
try {
$this->usingSensitiveParameterAttribute('u57474', 'Secret');
} catch (\Exception $e) {
$serializedData = $this->serializer->serializeException($e);
$this->assertSame('usingSensitiveParameterAttribute', $serializedData['Trace'][0]['function']);
$this->assertSame('u57474', $serializedData['Trace'][0]['args'][0]);
$this->assertSame('*** sensitive parameters replaced ***', $serializedData['Trace'][0]['args'][1]);
}
}
}