fix: prevent don't match extension when the file have uppercase name

Signed-off-by: Vitor Mattos <vitor@php.rio>
This commit is contained in:
Vitor Mattos 2024-10-21 13:52:38 -03:00
parent cab732a070
commit 91346f4537
No known key found for this signature in database
GPG key ID: B7AB4B76A7CA7318
3 changed files with 50 additions and 6 deletions

View file

@ -265,7 +265,7 @@ class SignFileService {
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 @@ class SignFileService {
}
$originalFile = current($originalFile);
}
if ($originalFile->getExtension() === 'pdf') {
if (strtolower($originalFile->getExtension()) === 'pdf') {
return $this->getPdfToSign($libresignFile, $originalFile);
}
return $originalFile;

View file

@ -11,10 +11,6 @@
displayDetailsOnTestsThatTriggerWarnings="true"
beStrictAboutCoverageMetadata="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests/Api</directory>
<directory suffix="Test.php">tests/Unit</directory>
</testsuite>
<testsuite name="api">
<directory suffix="Test.php">tests/Api</directory>
</testsuite>

View file

@ -199,4 +199,52 @@ final class SignFileServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
->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'],
];
}
}