Skip to content

Commit 62b20ac

Browse files
committed
feat: better exception message
1 parent e6c0008 commit 62b20ac

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

system/Autoloader/Autoloader.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Config\Autoload;
1616
use Config\Modules;
1717
use InvalidArgumentException;
18+
use RuntimeException;
1819

1920
/**
2021
* An autoloader that uses both PSR4 autoloading, and traditional classmaps.
@@ -306,13 +307,25 @@ public function sanitizeFilename(string $filename): string
306307
// Plus the forward slash for directory separators since this might be a path.
307308
// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_278
308309
// Modified to allow backslash and colons for on Windows machines.
309-
$tmp = preg_replace('/[^0-9\p{L}\s\/\-\_\.\:\\\\]/u', '', $filename);
310+
$result = preg_match_all('/[^0-9\p{L}\s\/\-_.:\\\\]/u', $filename, $matches);
311+
312+
if ($result > 0) {
313+
$chars = implode('', $matches[0]);
314+
315+
throw new InvalidArgumentException(
316+
'The file path contains special characters "' . $chars
317+
. '" that are not allowed: "' . $filename . '"'
318+
);
319+
}
320+
if ($result === false) {
321+
throw new RuntimeException(preg_last_error_msg() . ' filename: "' . $filename . '"');
322+
}
310323

311324
// Clean up our filename edges.
312-
$cleanFilename = trim($tmp, '.-_');
325+
$cleanFilename = trim($filename, '.-_');
313326

314327
if ($filename !== $cleanFilename) {
315-
throw new InvalidArgumentException('The file path contains special character that is not allowed: "' . $filename . '"');
328+
throw new InvalidArgumentException('The characters ".-_" are not allowed in filename edges: "' . $filename . '"');
316329
}
317330

318331
return $cleanFilename;

tests/system/Autoloader/AutoloaderTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,30 @@ public function testloadClassNonNamespaced()
198198
$this->assertFalse($this->loader->loadClass('Modules'));
199199
}
200200

201-
public function testSanitizationSimply()
201+
public function testSanitizationContailsSpecialChars()
202202
{
203203
$this->expectException(InvalidArgumentException::class);
204+
$this->expectExceptionMessage(
205+
'The file path contains special characters "${}!#" that are not allowed: "${../path}!#/to/some/file.php_"'
206+
);
204207

205208
$test = '${../path}!#/to/some/file.php_';
206209

207210
$this->loader->sanitizeFilename($test);
208211
}
209212

213+
public function testSanitizationFilenameEdges()
214+
{
215+
$this->expectException(InvalidArgumentException::class);
216+
$this->expectExceptionMessage(
217+
'The characters ".-_" are not allowed in filename edges: "/path/to/some/file.php_"'
218+
);
219+
220+
$test = '/path/to/some/file.php_';
221+
222+
$this->loader->sanitizeFilename($test);
223+
}
224+
210225
public function testSanitizationAllowUnicodeChars()
211226
{
212227
$test = 'Ä/path/to/some/file.php';

0 commit comments

Comments
 (0)