Skip to content

Commit d8dc20f

Browse files
committed
Validate binary hash before execution
1 parent 284a506 commit d8dc20f

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/JpegXlEncode/Encoder.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,22 @@
66
namespace Joppuyo\JpegXlEncode;
77

88
use ImageMimeTypeGuesser\ImageMimeTypeGuesser;
9+
use Joppuyo\JpegXlEncode\Exception\BinaryValidationException;
910
use Joppuyo\JpegXlEncode\Exception\InvalidArgumentException;
1011
use Symfony\Component\Process\Process;
1112
use Respect\Validation\Validator as v;
1213

1314
class Encoder {
15+
16+
/**
17+
* @var bool false
18+
*/
19+
private static $binaryValidated;
20+
21+
function __construct() {
22+
self::$binaryValidated = false;
23+
}
24+
1425
/*
1526
* Convert a JPEG or PNG file to JPEG XL
1627
* @throws \Exception
@@ -86,6 +97,7 @@ public static function encode(string $source, string $destination, array $option
8697
}
8798

8899
$binary_path = self::getBinaryPath();
100+
self::validateBinary($binary_path);
89101
self::ensure_permissions($binary_path);
90102

91103
$process_parameters = array_merge([$binary_path, $source, $destination], $flags);
@@ -152,4 +164,37 @@ private static function validateOptions(array $options)
152164
}
153165

154166
}
167+
168+
private static function validateBinary($binaryPath) {
169+
if(self::$binaryValidated) {
170+
// We validate binary only once per request to improve performance
171+
self::debug('Binary already validated.');
172+
return;
173+
}
174+
self::debug("Binary hasn't been validated yet. Validating...");
175+
$comparisonHash = self::getHash();
176+
$binaryHash = hash_file('sha256', $binaryPath);
177+
if(!hash_equals($binaryHash, $comparisonHash)) {
178+
self::debug('Hash does not match.');
179+
throw new BinaryValidationException("Binary hash check failed.");
180+
}
181+
self::debug('Hash hash matches. Caching result of hash comparison to speed up further conversions.');
182+
self::$binaryValidated = true;
183+
}
184+
185+
private static function getHash()
186+
{
187+
if (PHP_OS_FAMILY === 'Darwin') {
188+
// https://github.com/joppuyo/jpeg-xl-static-mac/releases/tag/v0.5.0-static-2
189+
return '292927130b4a83c639df6ba573916c2205234ca85f68a1e1357201e5b33b1904';
190+
}
191+
if (PHP_OS_FAMILY === 'Linux') {
192+
// https://github.com/joppuyo/jpeg-xl-static/releases/tag/v0.5.0-static-2
193+
return '50715d6af73bf177113ec4d46c35036b6295eb9a1be7e434c1a8ebbe5a1b8bda';
194+
}
195+
if (PHP_OS_FAMILY === 'Windows') {
196+
// https://github.com/joppuyo/jpeg-xl-static/releases/tag/v0.5.0-static
197+
return 'b78ec5a1b48c48c1e0dbb47865f7af8057a92291c65581a59e744a3dac6d5490';
198+
}
199+
}
155200
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Joppuyo\JpegXlEncode\Exception;
4+
5+
class BinaryValidationException extends \Exception {
6+
7+
}

0 commit comments

Comments
 (0)