From 9c0f39ee69b8760595850906cb6d218c01479980 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Mon, 21 Oct 2024 13:52:38 -0300 Subject: [PATCH] fix: prevent don't match extension when the file have uppercase name Signed-off-by: Vitor Mattos --- lib/Service/SignFileService.php | 4 +- phpunit.xml | 4 -- tests/Unit/Service/SignFileServiceTest.php | 48 ++++++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/lib/Service/SignFileService.php b/lib/Service/SignFileService.php index 954f3cd7b3..6c6139dc38 100644 --- a/lib/Service/SignFileService.php +++ b/lib/Service/SignFileService.php @@ -265,7 +265,7 @@ public function setVisibleElements(array $list): self { public function sign(): File { $fileToSign = $this->getFileToSing($this->libreSignFile); $pfxFileContent = $this->getPfxFile(); - switch ($fileToSign->getExtension()) { + switch (strtolower($fileToSign->getExtension())) { case 'pdf': $signedFile = $this->pkcs12Handler ->setInputFile($fileToSign) @@ -403,7 +403,7 @@ public function getFileToSing(FileEntity $libresignFile): \OCP\Files\Node { } $originalFile = current($originalFile); } - if ($originalFile->getExtension() === 'pdf') { + if (strtolower($originalFile->getExtension()) === 'pdf') { return $this->getPdfToSign($libresignFile, $originalFile); } return $originalFile; diff --git a/phpunit.xml b/phpunit.xml index 8bb48f9218..19c648835f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -11,10 +11,6 @@ displayDetailsOnTestsThatTriggerWarnings="true" beStrictAboutCoverageMetadata="true"> - - tests/Api - tests/Unit - tests/Api diff --git a/tests/Unit/Service/SignFileServiceTest.php b/tests/Unit/Service/SignFileServiceTest.php index 7da71e9abb..cbe9d3ddfc 100644 --- a/tests/Unit/Service/SignFileServiceTest.php +++ b/tests/Unit/Service/SignFileServiceTest.php @@ -199,4 +199,52 @@ public function testSignWithFileNotFound():void { ->setPassword('password') ->sign(); } + + /** + * @dataProvider dataSignWithSuccess + */ + public function testSignWithSuccess(string $mimetype, string $filename, string $extension):void { + $this->createAccount('username', 'password'); + + $file = new \OCA\Libresign\Db\File(); + $file->setUserId('username'); + + $nextcloudFile = $this->createMock(\OCP\Files\File::class); + $nextcloudFile->method('getMimeType')->willReturn($mimetype); + $nextcloudFile->method('getExtension')->willReturn($extension); + $nextcloudFile->method('getPath')->willReturn($filename); + $nextcloudFile->method('getContent')->willReturn('fake content'); + $nextcloudFile->method('getId')->willReturn(171); + + $this->root->method('getById')->willReturn([$nextcloudFile]); + $this->root->method('newFile')->willReturn($nextcloudFile); + $this->userMountCache->method('getMountsForFileId')->wilLReturn([]); + + $this->pkcs12Handler->method('setInputFile')->willReturn($this->pkcs12Handler); + $this->pkcs12Handler->method('setCertificate')->willReturn($this->pkcs12Handler); + $this->pkcs12Handler->method('setVisibleElements')->willReturn($this->pkcs12Handler); + $this->pkcs12Handler->method('setPassword')->willReturn($this->pkcs12Handler); + $this->pkcs12Handler->method('sign')->willReturn($nextcloudFile); + + $this->pkcs7Handler->method('setInputFile')->willReturn($this->pkcs12Handler); + $this->pkcs7Handler->method('setCertificate')->willReturn($this->pkcs12Handler); + $this->pkcs7Handler->method('setPassword')->willReturn($this->pkcs12Handler); + $this->pkcs7Handler->method('sign')->willReturn($nextcloudFile); + + $signRequest = new \OCA\Libresign\Db\SignRequest(); + $signRequest->setFileId(171); + $this->getService() + ->setLibreSignFile($file) + ->setSignRequest($signRequest) + ->setPassword('password') + ->sign(); + $this->assertTrue(true); + } + + public static function dataSignWithSuccess(): array { + return [ + ['application/pdf', 'file.PDF', 'PDF'], + ['application/pdf', 'file.pdf', 'pdf'], + ]; + } }