Files
nextcloud-server-mirror/tests/lib/Log/ExceptionSerializerTest.php
2026-11-03 11:07:56 +00:00

107 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace lib\Log;
use OC\Log\ExceptionSerializer;
use OC\SystemConfig;
use PHPUnit\Framework\Attributes\RequiresPhp;
use Test\TestCase;
class LazyObjectFixture {
public string $value = 'value';
}
class ExceptionSerializerTest extends TestCase {
private ExceptionSerializer $serializer;
private string|false $exceptionIgnoreArgs;
#[\Override]
public function setUp(): void {
parent::setUp();
// traces only contain arguments if this is disabled
$this->exceptionIgnoreArgs = ini_set('zend.exception_ignore_args', '0');
$config = $this->createMock(SystemConfig::class);
$this->serializer = new ExceptionSerializer($config);
}
#[\Override]
public function tearDown(): void {
if ($this->exceptionIgnoreArgs !== false) {
ini_set('zend.exception_ignore_args', $this->exceptionIgnoreArgs);
}
parent::tearDown();
}
private function emit($arguments) {
\call_user_func_array([$this, 'bind'], $arguments);
}
private function bind(array &$myValues): void {
throw new \Exception('my exception');
}
private function customMagicAuthThing(string $login, string $parole): void {
throw new \Exception('expected custom auth exception');
}
private function throwsWithObject(object $object): void {
throw new \Exception('expected exception');
}
/**
* this test ensures that the serializer does not overwrite referenced
* variables. It is crafted after a scenario we experienced: the DAV server
* emitting the "validateTokens" event, of which later on a handled
* exception was passed to the logger. The token was replaced, the original
* variable overwritten.
*/
public function testSerializer(): void {
try {
$secret = ['Secret'];
$this->emit([&$secret]);
} catch (\Exception $e) {
$serializedData = $this->serializer->serializeException($e);
$this->assertSame(['Secret'], $secret);
$this->assertSame(ExceptionSerializer::SENSITIVE_VALUE_PLACEHOLDER, $serializedData['Trace'][0]['args'][0]);
}
}
public function testSerializerWithRegisteredMethods(): void {
$this->serializer->enlistSensitiveMethods(self::class, ['customMagicAuthThing']);
try {
$this->customMagicAuthThing('u57474', 'Secret');
} catch (\Exception $e) {
$serializedData = $this->serializer->serializeException($e);
$this->assertSame('customMagicAuthThing', $serializedData['Trace'][0]['function']);
$this->assertSame(ExceptionSerializer::SENSITIVE_VALUE_PLACEHOLDER, $serializedData['Trace'][0]['args'][0]);
$this->assertFalse(isset($serializedData['Trace'][0]['args'][1]));
}
}
#[RequiresPhp('>= 8.4')]
public function testSerializerDoesNotInitializeLazyObjects(): void {
$ghost = (new \ReflectionClass(LazyObjectFixture::class))->newLazyGhost(function (): void {
throw new \RuntimeException('Lazy ghost could not be initialized');
});
try {
$this->throwsWithObject($ghost);
} catch (\Exception $e) {
$serializedData = $this->serializer->serializeException($e);
}
$this->assertSame(LazyObjectFixture::class, $serializedData['Trace'][0]['args'][0]['__class__']);
$this->assertSame('Lazy ghost could not be initialized', $serializedData['Trace'][0]['args'][0]['__failure__']);
}
}