Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/private/Log/ExceptionSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ private function encodeArg(mixed $arg, int $nestingLevel = 5): mixed {
}

$objectInfo = [ '__class__' => get_class($arg) ];
$objectVars = get_object_vars($arg);
try {
$objectVars = get_object_vars($arg);
} catch (\Throwable $e) {
// get_object_vars may throw if $arg was a ghost and failed to build
$objectVars = ['__failure__' => $e->getMessage()];
}
return array_map(function ($arg) use ($nestingLevel) {
return $this->encodeArg($arg, $nestingLevel - 1);
}, array_merge($objectInfo, $objectVars));
Expand Down
38 changes: 38 additions & 0 deletions tests/lib/Log/ExceptionSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,37 @@

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);
}
Expand All @@ -48,6 +66,10 @@ private function throwsException(string $maybeSecret): void {
throw new \Exception('expected 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
Expand Down Expand Up @@ -92,4 +114,20 @@ public function testSerializerWithSensitiveParameter(): void {
$this->assertSame(ExceptionSerializer::SENSITIVE_VALUE_PLACEHOLDER, $serializedData['Trace'][1]['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__']);
}
}
Loading