]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix(initial-state): Log an error when initial-state can not be JSON encoded 37572/head
authorJoas Schilling <coding@schilljs.com>
Tue, 4 Apr 2023 08:54:12 +0000 (10:54 +0200)
committerJoas Schilling <coding@schilljs.com>
Tue, 4 Apr 2023 08:54:12 +0000 (10:54 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/InitialStateService.php
tests/lib/InitialStateServiceTest.php

index 1d997e4133a3dd6924abce59ee170fa72f33cdce..c10442a5267393f7ec00e2ad8278adcbfe44008e 100644 (file)
@@ -64,7 +64,11 @@ class InitialStateService implements IInitialStateService {
                        if (!isset($this->states[$appName])) {
                                $this->states[$appName] = [];
                        }
-                       $this->states[$appName][$key] = json_encode($data);
+                       try {
+                               $this->states[$appName][$key] = json_encode($data, JSON_THROW_ON_ERROR);
+                       } catch (\JsonException $e) {
+                               $this->logger->error('Invalid '. $key . ' data provided to provideInitialState by ' . $appName, ['exception' => $e]);
+                       }
                        return;
                }
 
index f4decb43e1e06c44f0ce04392c30919473e88b1c..554478e123ff46ebc7adf35cdaa6e42a915c19db 100644 (file)
@@ -27,6 +27,7 @@ namespace Test;
 
 use OC\AppFramework\Bootstrap\Coordinator;
 use OCP\IServerContainer;
+use PHPUnit\Framework\MockObject\MockObject;
 use Psr\Log\LoggerInterface;
 use function json_encode;
 use JsonSerializable;
@@ -36,18 +37,22 @@ use stdClass;
 class InitialStateServiceTest extends TestCase {
        /** @var InitialStateService */
        private $service;
+       /** @var MockObject|LoggerInterface|(LoggerInterface&MockObject)  */
+       protected $logger;
 
        protected function setUp(): void {
                parent::setUp();
 
+               $this->logger = $this->createMock(LoggerInterface::class);
+
                $this->service = new InitialStateService(
-                       $this->createMock(LoggerInterface::class),
+                       $this->logger,
                        $this->createMock(Coordinator::class),
                        $this->createMock(IServerContainer::class)
                );
        }
 
-       public function staticData() {
+       public function staticData(): array {
                return [
                        ['string'],
                        [23],
@@ -63,7 +68,7 @@ class InitialStateServiceTest extends TestCase {
        /**
         * @dataProvider staticData
         */
-       public function testStaticData($value) {
+       public function testStaticData(mixed $value): void {
                $this->service->provideInitialState('test', 'key', $value);
                $data = $this->service->getInitialStates();
 
@@ -73,7 +78,23 @@ class InitialStateServiceTest extends TestCase {
                );
        }
 
-       public function testStaticButInvalidData() {
+       public function testValidDataButFailsToJSONEncode(): void {
+               $this->logger->expects($this->once())
+                       ->method('error');
+
+               $this->service->provideInitialState('test', 'key', ['upload' => INF]);
+               $data = $this->service->getInitialStates();
+
+               $this->assertEquals(
+                       [],
+                       $data
+               );
+       }
+
+       public function testStaticButInvalidData(): void {
+               $this->logger->expects($this->once())
+                       ->method('warning');
+
                $this->service->provideInitialState('test', 'key', new stdClass());
                $data = $this->service->getInitialStates();
 
@@ -86,7 +107,7 @@ class InitialStateServiceTest extends TestCase {
        /**
         * @dataProvider staticData
         */
-       public function testLazyData($value) {
+       public function testLazyData(mixed $value): void {
                $this->service->provideLazyInitialState('test', 'key', function () use ($value) {
                        return $value;
                });