Skip to content

Commit 42ce4e7

Browse files
authored
Merge pull request #7151 from LibreSign/backport/7149/stable33
[stable33] fix: signer common name array
2 parents f8d6c54 + 1c51a01 commit 42ce4e7

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/Service/SignFileService.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,16 +927,33 @@ protected function getSignatureParams(): array {
927927
}
928928

929929
private function buildBaseSignatureParams(array $certificateData): array {
930+
$issuerCommonName = $this->normalizeCertificateFieldToString($certificateData['issuer']['CN'] ?? '');
931+
$signerCommonName = $this->normalizeCertificateFieldToString($certificateData['subject']['CN'] ?? '');
932+
930933
return [
931934
'DocumentUUID' => $this->libreSignFile?->getUuid(),
932-
'IssuerCommonName' => $certificateData['issuer']['CN'] ?? '',
933-
'SignerCommonName' => $certificateData['subject']['CN'] ?? '',
935+
'IssuerCommonName' => $issuerCommonName,
936+
'SignerCommonName' => $signerCommonName,
934937
'LocalSignerTimezone' => $this->dateTimeZone->getTimeZone()->getName(),
935938
'LocalSignerSignatureDateTime' => (new DateTime('now', new \DateTimeZone('UTC')))
936939
->format(DateTimeInterface::ATOM)
937940
];
938941
}
939942

943+
private function normalizeCertificateFieldToString(mixed $value): string {
944+
if (is_array($value)) {
945+
$flattened = [];
946+
array_walk_recursive($value, static function (mixed $item) use (&$flattened): void {
947+
if ($item !== null) {
948+
$flattened[] = (string)$item;
949+
}
950+
});
951+
return implode(', ', $flattened);
952+
}
953+
954+
return $value === null ? '' : (string)$value;
955+
}
956+
940957
private function addEmailToSignatureParams(array $signatureParams, array $certificateData): array {
941958
$email = $this->subjectAlternativeNameService->extractEmailFromCertificate($certificateData);
942959
if ($email) {

tests/php/Unit/Service/SignFileServiceTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,14 @@ public static function providerGetSignatureParamsCommonName(): array {
11661166
'LibreCode',
11671167
'Jane Doe',
11681168
],
1169+
'array CNs should be normalized' => [
1170+
[
1171+
'issuer' => ['CN' => ['LibreCode', 'CA']],
1172+
'subject' => ['CN' => ['Doe', 'Jane']],
1173+
],
1174+
'LibreCode, CA',
1175+
'Doe, Jane',
1176+
],
11691177
'empty CNs' => [
11701178
[
11711179
'issuer' => ['CN' => ''],

0 commit comments

Comments
 (0)