Skip to content

Commit

Permalink
fix: early terminate processing of invalid library path
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Mar 29, 2024
1 parent 3503b4d commit ef03144
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
2 changes: 1 addition & 1 deletion phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -7723,7 +7723,7 @@
];
$ignoreErrors[] = [
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
'count' => 9,
'count' => 8,
'path' => __DIR__ . '/system/Images/Handlers/ImageMagickHandler.php',
];
$ignoreErrors[] = [
Expand Down
22 changes: 14 additions & 8 deletions system/Images/Handlers/ImageMagickHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,26 @@ public function getVersion(): string
*/
protected function process(string $action, int $quality = 100): array
{
// Do we have a vaild library path?
if (empty($this->config->libraryPath)) {
throw ImageException::forInvalidImageLibraryPath($this->config->libraryPath);
$cmd = $this->config->libraryPath;

if ($cmd === '') {
throw ImageException::forInvalidImageLibraryPath($cmd);
}

if ($action !== '-version') {
$this->supportedFormatCheck();
if (preg_match('/convert$/i', $cmd) !== 1) {
$cmd = rtrim($cmd, '/') . '/convert';

$this->config->libraryPath = $cmd;
}

if (! preg_match('/convert$/i', $this->config->libraryPath)) {
$this->config->libraryPath = rtrim($this->config->libraryPath, '/') . '/convert';
if (! is_file($cmd)) {
throw ImageException::forInvalidImageLibraryPath($cmd);
}

if ($action !== '-version') {
$this->supportedFormatCheck();
}

$cmd = $this->config->libraryPath;
$cmd .= $action === '-version' ? ' ' . $action : ' -quality ' . $quality . ' ' . $action;

$retval = 1;
Expand Down
28 changes: 28 additions & 0 deletions tests/system/Images/ImageMagickHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\Config\Services;
use CodeIgniter\Images\Exceptions\ImageException;
use CodeIgniter\Images\Handlers\BaseHandler;
use CodeIgniter\Images\Handlers\ImageMagickHandler;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Images;
use Imagick;
Expand Down Expand Up @@ -77,6 +78,33 @@ protected function setUp(): void
$this->handler = Services::image('imagick', $config, false);
}

/**
* @dataProvider provideNonexistentLibraryPathTerminatesProcessing
*/
public function testNonexistentLibraryPathTerminatesProcessing(string $path, string $invalidPath): void
{
$this->expectException(ImageException::class);
$this->expectExceptionMessage(lang('Images.libPathInvalid', [$invalidPath]));

$config = new Images();

$config->libraryPath = $path;

(new ImageMagickHandler($config))->getVersion();
}

/**
* @return iterable<string, list<string>>
*/
public static function provideNonexistentLibraryPathTerminatesProcessing(): iterable
{
yield 'empty string' => ['', ''];

yield 'invalid file' => ['/var/log/convert', '/var/log/convert'];

yield 'nonexistent file' => ['/var/www/file', '/var/www/file/convert'];
}

public function testGetVersion(): void
{
$version = $this->handler->getVersion();
Expand Down

0 comments on commit ef03144

Please sign in to comment.