Skip to content

Commit

Permalink
fix: prevent don't match extension when the file have uppercase name
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos authored and backportbot-libresign[bot] committed Oct 21, 2024
1 parent 91b25b1 commit 9c0f39e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/Service/SignFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
48 changes: 48 additions & 0 deletions tests/Unit/Service/SignFileServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}
}

0 comments on commit 9c0f39e

Please sign in to comment.