diff --git a/lib/Controller/PublicFileHandlingController.php b/lib/Controller/PublicFileHandlingController.php index 7af9a10..ed0a420 100644 --- a/lib/Controller/PublicFileHandlingController.php +++ b/lib/Controller/PublicFileHandlingController.php @@ -89,6 +89,10 @@ public function load($token) { return new DataResponse(['message' => $this->l->t('You are not authorized to open this share')], Http::STATUS_BAD_REQUEST); } + if (!$this->checkPermissions($share, \OCP\Constants::PERMISSION_READ)) { + return new DataResponse(['message' => $this->l->t('You are not authorized to open this share')], Http::STATUS_FORBIDDEN); + } + try { $node = $share->getNode(); } catch (NotFoundException $e) { diff --git a/tests/Unit/Controller/PublicFileHandlingControllerTest.php b/tests/Unit/Controller/PublicFileHandlingControllerTest.php new file mode 100644 index 0000000..1fba76e --- /dev/null +++ b/tests/Unit/Controller/PublicFileHandlingControllerTest.php @@ -0,0 +1,79 @@ +createMock(IRequest::class); + $request->method('getParam')->willReturnMap([ + ['dir', null, '/'], + ['filename', null, 'secret.km'], + ]); + $this->shareManager = $this->createMock(IManager::class); + $l10n = $this->createMock(IL10N::class); + $l10n->method('t')->willReturnArgument(0); + + $this->controller = new PublicFileHandlingController( + 'files_mindmap', + $request, + $l10n, + $this->createMock(LoggerInterface::class), + $this->shareManager, + $this->createMock(ISession::class), + ); + } + + public function testLoadFromFileDropShareIsRefused(): void { + $folder = $this->createMock(Folder::class); + $folder->expects($this->never())->method('get'); + $this->mockShare(Constants::PERMISSION_CREATE, $folder); + + $this->assertSame(Http::STATUS_FORBIDDEN, $this->controller->load('token')->getStatus()); + } + + public function testLoadFromReadableShare(): void { + $file = $this->createMock(File::class); + $file->method('getContent')->willReturn('{"root":{}}'); + $folder = $this->createMock(Folder::class); + $folder->method('get')->with('/secret.km')->willReturn($file); + $this->mockShare(Constants::PERMISSION_READ, $folder); + + $response = $this->controller->load('token'); + + $this->assertSame(Http::STATUS_OK, $response->getStatus()); + $this->assertSame(base64_encode('{"root":{}}'), $response->getData()['filecontents']); + } + + private function mockShare(int $permissions, Folder $node): void { + $share = $this->createMock(IShare::class); + $share->method('getPermissions')->willReturn($permissions); + $share->method('getNode')->willReturn($node); + $this->shareManager->method('getShareByToken')->willReturn($share); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..a65b0f3 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,20 @@ +addPsr4('OCA\\Files_MindMap\\Tests\\', __DIR__ . '/', true); + +Server::get(IAppManager::class)->loadApp('files_mindmap'); diff --git a/tests/phpunit.xml b/tests/phpunit.xml new file mode 100644 index 0000000..0916df0 --- /dev/null +++ b/tests/phpunit.xml @@ -0,0 +1,12 @@ + + + + + + Unit + + +