From 3ec5e3871f0ef5be33db36e6be935d523219788c Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 2 Dec 2025 10:44:20 -0300 Subject: [PATCH 1/3] fix: handle corrupted signature data in PDF validation When processing PDF signatures, hex2bin() and stream_get_contents() can fail and return false when dealing with corrupted or malformed signature data. Previously, this would cause a TypeError when false was passed to processSignature() which expects ?string. Now, invalid signatures are properly reported to users with a 'Digest Mismatch' validation error instead of causing a fatal error. This fix: - Validates hex2bin() return value before yielding - Validates stream_get_contents() return value before processing - Returns null for corrupted signatures which are then reported as invalid - Suppresses expected hex2bin() warnings for non-hexadecimal data Fixes the issue reported where users encountered: TypeError: Argument #2 ($signature) must be of type ?string, false given Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- lib/Handler/SignEngine/Pkcs12Handler.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/Handler/SignEngine/Pkcs12Handler.php b/lib/Handler/SignEngine/Pkcs12Handler.php index f82c9c1314..1bcfa753e6 100644 --- a/lib/Handler/SignEngine/Pkcs12Handler.php +++ b/lib/Handler/SignEngine/Pkcs12Handler.php @@ -70,8 +70,17 @@ private function getSignatures($resource): iterable { rewind($resource); $signature = stream_get_contents($resource, $signatureLength, $signatureStart); + if ($signature === false) { + yield null; + continue; + } - yield hex2bin($signature); + $decodedSignature = @hex2bin($signature); + if ($decodedSignature === false) { + yield null; + continue; + } + yield $decodedSignature; } $this->tempManager->clean(); From 734f1e4d8140fa5eedc1427eb6e1b9c6adf10a40 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 2 Dec 2025 10:44:37 -0300 Subject: [PATCH 2/3] test: add coverage for corrupted signature data handling Add test case to verify that PDFs with corrupted signature data (invalid hexadecimal strings) are properly handled and reported to users with appropriate validation errors instead of throwing exceptions. The test verifies: - Corrupted signatures return validation error - Error has correct structure with id and label - Error is identified as 'Digest mismatch' (id: 3) - No fatal errors or exceptions are thrown Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- .../Handler/SignEngine/Pkcs12HandlerTest.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/php/Unit/Handler/SignEngine/Pkcs12HandlerTest.php b/tests/php/Unit/Handler/SignEngine/Pkcs12HandlerTest.php index 86ea13e376..2256142f49 100644 --- a/tests/php/Unit/Handler/SignEngine/Pkcs12HandlerTest.php +++ b/tests/php/Unit/Handler/SignEngine/Pkcs12HandlerTest.php @@ -225,6 +225,31 @@ public function testGetCertificateChainWithInvalidInput(): void { fclose($invalidResource); } + public function testGetCertificateChainWithCorruptedSignature(): void { + $handler = $this->getHandler(); + + $corruptedPdf = "%PDF-1.4\n" . + "1 0 obj<>endobj\n" . + "ZZZZZZZZ\n" . // Invalid hex data - will cause hex2bin to fail + str_repeat("x", 100); + + $resource = fopen('php://memory', 'r+'); + fwrite($resource, $corruptedPdf); + rewind($resource); + + $result = $handler->getCertificateChain($resource); + + $this->assertIsArray($result); + $this->assertCount(1, $result); + $this->assertArrayHasKey('chain', $result[0]); + $this->assertArrayHasKey('signature_validation', $result[0]['chain'][0]); + $this->assertIsArray($result[0]['chain'][0]['signature_validation']); + $this->assertEquals(3, $result[0]['chain'][0]['signature_validation']['id']); // Digest Mismatch + $this->assertStringContainsString('Digest mismatch', $result[0]['chain'][0]['signature_validation']['label']); + + fclose($resource); + } + public function testRealWorldUsagePattern(): void { $handler = $this->getHandler(); From a76827235552c970c670bb98216eb9cd79c91d0b Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 2 Dec 2025 10:48:17 -0300 Subject: [PATCH 3/3] fix: cs Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- tests/php/Unit/Handler/SignEngine/Pkcs12HandlerTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/php/Unit/Handler/SignEngine/Pkcs12HandlerTest.php b/tests/php/Unit/Handler/SignEngine/Pkcs12HandlerTest.php index 2256142f49..ff6e7988af 100644 --- a/tests/php/Unit/Handler/SignEngine/Pkcs12HandlerTest.php +++ b/tests/php/Unit/Handler/SignEngine/Pkcs12HandlerTest.php @@ -228,10 +228,10 @@ public function testGetCertificateChainWithInvalidInput(): void { public function testGetCertificateChainWithCorruptedSignature(): void { $handler = $this->getHandler(); - $corruptedPdf = "%PDF-1.4\n" . - "1 0 obj<>endobj\n" . - "ZZZZZZZZ\n" . // Invalid hex data - will cause hex2bin to fail - str_repeat("x", 100); + $corruptedPdf = "%PDF-1.4\n" + . "1 0 obj<>endobj\n" + . "ZZZZZZZZ\n" // Invalid hex data - will cause hex2bin to fail + . str_repeat('x', 100); $resource = fopen('php://memory', 'r+'); fwrite($resource, $corruptedPdf);