Skip to content

Commit 0bf6567

Browse files
committed
fix: harden footer policy fallback
Signed-off-by: Vitor Mattos <[email protected]>
1 parent 0406158 commit 0bf6567

5 files changed

Lines changed: 27 additions & 9 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Libresign\Exception;
10+
11+
use RuntimeException;
12+
13+
class FooterStampUnavailableException extends RuntimeException {
14+
}

lib/Handler/PdfTk/Pdf.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace OCA\Libresign\Handler\PdfTk;
1010

1111
use OCA\Libresign\AppInfo\Application;
12-
use OCA\Libresign\Exception\LibresignException;
12+
use OCA\Libresign\Exception\FooterStampUnavailableException;
1313
use OCA\Libresign\Helper\JavaHelper;
1414
use OCA\Libresign\Vendor\mikehaertl\pdftk\Command;
1515
use OCA\Libresign\Vendor\mikehaertl\pdftk\Pdf as BasePdf;
@@ -45,17 +45,16 @@ public function applyStamp(string $input, string $stamp): string {
4545
protected function configureCommand(): void {
4646
$this->javaPath = $this->javaHelper->getJavaPath();
4747
if ($this->javaPath === '') {
48-
throw new RuntimeException('Java path not set.');
48+
throw new FooterStampUnavailableException('Java path not set.');
4949
}
5050

5151
$this->pdftkPath = $this->appConfig->getValueString(Application::APP_ID, 'pdftk_path');
5252
if ($this->pdftkPath === '') {
53-
throw new RuntimeException('PDFtk path not set.');
53+
throw new FooterStampUnavailableException('PDFtk path not set.');
5454
}
5555

56-
5756
if (!file_exists($this->javaPath) || !file_exists($this->pdftkPath)) {
58-
throw new LibresignException($this->l10n->t('The admin hasn\'t set up LibreSign yet, please wait.'));
57+
throw new FooterStampUnavailableException($this->l10n->t('The admin hasn\'t set up LibreSign yet, please wait.'));
5958
}
6059

6160
$cmd = sprintf('%s -jar %s', escapeshellcmd($this->javaPath), escapeshellarg($this->pdftkPath));

lib/Service/Policy/Provider/RequestSignGroups/RequestSignGroupsPolicyGuard.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function assertUserScopeSupported(string $policyKey): void {
3434
throw new \InvalidArgumentException($this->l10n->t(self::USER_SCOPE_NOT_SUPPORTED_MESSAGE));
3535
}
3636

37-
public function normalizeManagedValue(string $policyKey, null|bool|int|float|string $value, bool $allowNullReset = false): null|bool|int|float|string {
37+
public function normalizeManagedValue(string $policyKey, mixed $value, bool $allowNullReset = false): mixed {
3838
if ($policyKey !== RequestSignGroupsPolicy::KEY) {
3939
return $value;
4040
}

lib/Service/SignFileService.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCA\Libresign\Db\UserElementMapper;
3030
use OCA\Libresign\Enum\FileStatus;
3131
use OCA\Libresign\Events\SignedEventFactory;
32+
use OCA\Libresign\Exception\FooterStampUnavailableException;
3233
use OCA\Libresign\Exception\LibresignException;
3334
use OCA\Libresign\Handler\DocMdpHandler;
3435
use OCA\Libresign\Handler\FooterHandler;
@@ -1372,7 +1373,12 @@ protected function getPdfToSign(File $originalFile): File {
13721373

13731374
try {
13741375
$pdfContent = $this->pdf->applyStamp($input, $stamp);
1375-
} catch (RuntimeException $e) {
1376+
} catch (FooterStampUnavailableException $e) {
1377+
$this->logger->warning('Using original PDF because footer stamping is unavailable.', [
1378+
'exception' => $e,
1379+
]);
1380+
$pdfContent = $originalContent;
1381+
} catch (RuntimeException|LibresignException $e) {
13761382
throw new LibresignException($e->getMessage());
13771383
}
13781384
} else {

tests/php/Unit/Handler/PdfTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace OCA\Libresign\Tests\Unit\Handler;
77

88
use OCA\Libresign\AppInfo\Application;
9-
use OCA\Libresign\Exception\LibresignException;
109
use OCA\Libresign\Handler\PdfTk\Pdf;
1110
use OCA\Libresign\Helper\JavaHelper;
1211
use OCP\IAppConfig;
@@ -90,7 +89,7 @@ public function testInvalidDependenciesPath(): void {
9089
$this->appConfig->setValueString(Application::APP_ID, 'pdftk_path', '/fake/path');
9190
$pdf = $this->getInstance();
9291

93-
$this->expectException(LibresignException::class);
92+
$this->expectException(RuntimeException::class);
9493
$this->expectExceptionMessageMatches('/set up/');
9594

9695
$pdf->applyStamp('/tmp/input.pdf', '/tmp/stamp.pdf');

0 commit comments

Comments
 (0)