Skip to content

Commit 7f40eea

Browse files
committed
fix: early terminate processing of invalid library path
1 parent 3503b4d commit 7f40eea

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
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

+14-8
Original file line numberDiff line numberDiff line change
@@ -184,20 +184,26 @@ public function getVersion(): string
184184
*/
185185
protected function process(string $action, int $quality = 100): array
186186
{
187-
// Do we have a vaild library path?
188-
if (empty($this->config->libraryPath)) {
189-
throw ImageException::forInvalidImageLibraryPath($this->config->libraryPath);
187+
$cmd = $this->config->libraryPath;
188+
189+
if ($cmd === '') {
190+
throw ImageException::forInvalidImageLibraryPath($cmd);
190191
}
191192

192-
if ($action !== '-version') {
193-
$this->supportedFormatCheck();
193+
if (preg_match('/convert$/i', $cmd) !== 1) {
194+
$cmd = rtrim($cmd, '\/') . '/convert';
195+
196+
$this->config->libraryPath = $cmd;
194197
}
195198

196-
if (! preg_match('/convert$/i', $this->config->libraryPath)) {
197-
$this->config->libraryPath = rtrim($this->config->libraryPath, '/') . '/convert';
199+
if (! is_file($cmd)) {
200+
throw ImageException::forInvalidImageLibraryPath($cmd);
201+
}
202+
203+
if ($action !== '-version') {
204+
$this->supportedFormatCheck();
198205
}
199206

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

203209
$retval = 1;

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))->getVersion();
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)