Skip to content

Commit 5a8213a

Browse files
committed
Relax allowed characters in file names
Allows all unicode letters and all unicode numbers instead of only a-z, A-Z, 0-9. Signed-off-by: Maximilian Krög <maxi_kroeg@web.de>
1 parent a274608 commit 5a8213a

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

libraries/classes/Sanitize.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,8 @@ static function (array $match): string {
247247
*/
248248
public static function sanitizeFilename($filename, $replaceDots = false)
249249
{
250-
$pattern = '/[^A-Za-z0-9_';
251-
// if we don't have to replace dots
252-
if (! $replaceDots) {
253-
// then add the dot to the list of legit characters
254-
$pattern .= '.';
255-
}
256-
257-
$pattern .= '-]/';
250+
// Keep only numbers (N), letters (L), dash, underbar, and maybe dot
251+
$pattern = '/[^\p{N}\p{L}' . ($replaceDots ? '' : '.') . '_-]/u';
258252
$filename = preg_replace($pattern, '_', $filename);
259253

260254
return $filename;

test/classes/SanitizeTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
use PhpMyAdmin\Sanitize;
88

9+
use function implode;
10+
use function range;
11+
use function str_repeat;
12+
913
/**
1014
* @covers \PhpMyAdmin\Sanitize
1115
*/
@@ -158,10 +162,30 @@ public function testEscape(): void
158162

159163
/**
160164
* Test for Sanitize::sanitizeFilename
165+
*
166+
* @dataProvider providerTestSanitizeFileName
161167
*/
162-
public function testSanitizeFilename(): void
168+
public function testSanitizeFilename(string $expected, string $input, bool $replaceDot): void
163169
{
164-
self::assertSame('File_name_123', Sanitize::sanitizeFilename('File_name 123'));
170+
self::assertSame($expected, Sanitize::sanitizeFilename($input, $replaceDot));
171+
}
172+
173+
/** @psalm-return list<array{string,string,bool}> */
174+
public static function providerTestSanitizeFileName(): array
175+
{
176+
return [
177+
['Hello123', 'Hello123', false],
178+
['宮保雞丁', '宮保雞丁', false],
179+
['Україна', 'Україна', false],
180+
['-_-', '-.-', true],
181+
['-.-', '-.-', false],
182+
['___', '"\'"', false],
183+
['_test_', '<test>', false],
184+
['Hello__World_', "Hello\r\nWorld!", false],
185+
['_', "\u{fffd}", false],
186+
['_', '🚀', false],
187+
[str_repeat('_', 32), implode('', range("\0", "\x1f")), false],
188+
];
165189
}
166190

167191
/**

0 commit comments

Comments
 (0)