Skip to content

Commit b26cff4

Browse files
fix(signature-text): reject invalid dimensions at save and auto-repair on read
At save: reject signatureWidth or signatureHeight that are non-finite (NaN, Inf, -Inf) or below SIGNATURE_DIMENSION_MINIMUM (1). The guard uses is_finite() before the range check because PHP NaN comparisons always return false, silently letting NaN through. At read: getSanitizedDimension() replaces getValueFloat() calls in getFullSignatureWidth() and getFullSignatureHeight(). If the stored value is non-finite or below the minimum it writes back the default, logs a warning, and returns the default — preventing Imagick from receiving a zero or invalid canvas size. Fixes: Imagick exception 'no pixels defined in cache' triggered when the app config contains a corrupted or zero dimension value. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent b0bd20b commit b26cff4

1 file changed

Lines changed: 31 additions & 3 deletions

File tree

lib/Service/SignatureTextService.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
class SignatureTextService {
3030
public const TEMPLATE_DEFAULT_FONT_SIZE = 10;
3131
public const SIGNATURE_DEFAULT_FONT_SIZE = 20;
32+
public const SIGNATURE_DIMENSION_MINIMUM = 1;
3233
public const FONT_SIZE_MINIMUM = 0.1;
3334
public const FRONT_SIZE_MAX = 30;
3435
public const DEFAULT_SIGNATURE_WIDTH = 350;
@@ -69,6 +70,19 @@ public function save(
6970
// within the accepted range.
7071
throw new LibresignException($this->l10n->t('Invalid signature font size. The value must be between %.1f and %.0f.', [self::FONT_SIZE_MINIMUM, self::FRONT_SIZE_MAX]));
7172
}
73+
if (
74+
!is_finite($signatureWidth)
75+
|| !is_finite($signatureHeight)
76+
|| $signatureWidth < self::SIGNATURE_DIMENSION_MINIMUM
77+
|| $signatureHeight < self::SIGNATURE_DIMENSION_MINIMUM
78+
) {
79+
// TRANSLATORS This message is shown when the visible signature box size
80+
// configured by the admin is invalid. "Signature box" is the rectangular
81+
// area reserved for the handwritten-style signature image in the signed
82+
// PDF. "Width" and "height" are its pixel dimensions. %.0f is the
83+
// minimum allowed value for each dimension.
84+
throw new LibresignException($this->l10n->t('Invalid signature box size. Width and height must be at least %.0f.', [self::SIGNATURE_DIMENSION_MINIMUM]));
85+
}
7286
$template = trim($template);
7387
$template = preg_replace(
7488
[
@@ -432,11 +446,11 @@ public function getDefaultTemplate(): string {
432446
}
433447

434448
public function getFullSignatureWidth(): float {
435-
return $this->appConfig->getValueFloat(Application::APP_ID, 'signature_width', self::DEFAULT_SIGNATURE_WIDTH);
449+
return $this->getSanitizedDimension('signature_width', self::DEFAULT_SIGNATURE_WIDTH);
436450
}
437451

438452
public function getFullSignatureHeight(): float {
439-
return $this->appConfig->getValueFloat(Application::APP_ID, 'signature_height', self::DEFAULT_SIGNATURE_HEIGHT);
453+
return $this->getSanitizedDimension('signature_height', self::DEFAULT_SIGNATURE_HEIGHT);
440454
}
441455

442456
public function getSignatureWidth(): float {
@@ -448,7 +462,21 @@ public function getSignatureWidth(): float {
448462
}
449463

450464
public function getSignatureHeight(): float {
451-
return $this->appConfig->getValueFloat(Application::APP_ID, 'signature_height', self::DEFAULT_SIGNATURE_HEIGHT);
465+
return $this->getFullSignatureHeight();
466+
}
467+
468+
private function getSanitizedDimension(string $key, float $default): float {
469+
$value = $this->appConfig->getValueFloat(Application::APP_ID, $key, $default);
470+
if (!is_finite($value) || $value < self::SIGNATURE_DIMENSION_MINIMUM) {
471+
$this->appConfig->setValueFloat(Application::APP_ID, $key, $default);
472+
$this->logger->warning('Invalid signature dimension found in app config. Falling back to default.', [
473+
'key' => $key,
474+
'value' => $value,
475+
'default' => $default,
476+
]);
477+
return $default;
478+
}
479+
return $value;
452480
}
453481

454482
public function getTemplateFontSize(): float {

0 commit comments

Comments
 (0)