Skip to content

Commit f22b8cf

Browse files
authored
Merge pull request #8680 from paulbalandan/imagemagick-library-path
fix: [ImageMagickHandler] early terminate processing of invalid library path
2 parents 9b1fffe + 2c7d445 commit f22b8cf

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

phpstan-baseline.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7723,7 +7723,7 @@
77237723
];
77247724
$ignoreErrors[] = [
77257725
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
7726-
'count' => 9,
7726+
'count' => 8,
77277727
'path' => __DIR__ . '/system/Images/Handlers/ImageMagickHandler.php',
77287728
];
77297729
$ignoreErrors[] = [

system/Images/Handlers/ImageMagickHandler.php

+16-11
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ class ImageMagickHandler extends BaseHandler
3232
protected $resource;
3333

3434
/**
35-
* Constructor.
36-
*
3735
* @param Images $config
3836
*
3937
* @throws ImageException
@@ -45,6 +43,22 @@ public function __construct($config = null)
4543
if (! (extension_loaded('imagick') || class_exists(Imagick::class))) {
4644
throw ImageException::forMissingExtension('IMAGICK'); // @codeCoverageIgnore
4745
}
46+
47+
$cmd = $this->config->libraryPath;
48+
49+
if ($cmd === '') {
50+
throw ImageException::forInvalidImageLibraryPath($cmd);
51+
}
52+
53+
if (preg_match('/convert$/i', $cmd) !== 1) {
54+
$cmd = rtrim($cmd, '\/') . '/convert';
55+
56+
$this->config->libraryPath = $cmd;
57+
}
58+
59+
if (! is_file($cmd)) {
60+
throw ImageException::forInvalidImageLibraryPath($cmd);
61+
}
4862
}
4963

5064
/**
@@ -184,19 +198,10 @@ public function getVersion(): string
184198
*/
185199
protected function process(string $action, int $quality = 100): array
186200
{
187-
// Do we have a vaild library path?
188-
if (empty($this->config->libraryPath)) {
189-
throw ImageException::forInvalidImageLibraryPath($this->config->libraryPath);
190-
}
191-
192201
if ($action !== '-version') {
193202
$this->supportedFormatCheck();
194203
}
195204

196-
if (! preg_match('/convert$/i', $this->config->libraryPath)) {
197-
$this->config->libraryPath = rtrim($this->config->libraryPath, '/') . '/convert';
198-
}
199-
200205
$cmd = $this->config->libraryPath;
201206
$cmd .= $action === '-version' ? ' ' . $action : ' -quality ' . $quality . ' ' . $action;
202207

tests/system/Images/ImageMagickHandlerTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use CodeIgniter\Config\Services;
1515
use CodeIgniter\Images\Exceptions\ImageException;
1616
use CodeIgniter\Images\Handlers\BaseHandler;
17+
use CodeIgniter\Images\Handlers\ImageMagickHandler;
1718
use CodeIgniter\Test\CIUnitTestCase;
1819
use Config\Images;
1920
use Imagick;
@@ -77,6 +78,33 @@ protected function setUp(): void
7778
$this->handler = Services::image('imagick', $config, false);
7879
}
7980

81+
/**
82+
* @dataProvider provideNonexistentLibraryPathTerminatesProcessing
83+
*/
84+
public function testNonexistentLibraryPathTerminatesProcessing(string $path, string $invalidPath): void
85+
{
86+
$this->expectException(ImageException::class);
87+
$this->expectExceptionMessage(lang('Images.libPathInvalid', [$invalidPath]));
88+
89+
$config = new Images();
90+
91+
$config->libraryPath = $path;
92+
93+
new ImageMagickHandler($config);
94+
}
95+
96+
/**
97+
* @return iterable<string, list<string>>
98+
*/
99+
public static function provideNonexistentLibraryPathTerminatesProcessing(): iterable
100+
{
101+
yield 'empty string' => ['', ''];
102+
103+
yield 'invalid file' => ['/var/log/convert', '/var/log/convert'];
104+
105+
yield 'nonexistent file' => ['/var/www/file', '/var/www/file/convert'];
106+
}
107+
80108
public function testGetVersion(): void
81109
{
82110
$version = $this->handler->getVersion();

0 commit comments

Comments
 (0)