Skip to content

Commit 5f8f07d

Browse files
committed
FileUpload::getSanitizedName() changes the extension only for image files [Closes #239]
1 parent e66a32b commit 5f8f07d

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/Http/FileUpload.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function getUntrustedName(): string
8080

8181
/**
8282
* Returns the sanitized file name. The resulting name contains only ASCII characters [a-zA-Z0-9.-].
83-
* If the name does not contain such characters, it returns 'unknown'. If the file is JPEG, PNG, GIF, or WebP image,
83+
* If the name does not contain such characters, it returns 'unknown'. If the file is an image supported by PHP,
8484
* it returns the correct file extension. Do not blindly trust the value returned by this method.
8585
*/
8686
public function getSanitizedName(): string
@@ -89,9 +89,9 @@ public function getSanitizedName(): string
8989
$name = str_replace(['-.', '.-'], '.', $name);
9090
$name = trim($name, '.-');
9191
$name = $name === '' ? 'unknown' : $name;
92-
if ($ext = $this->getSuggestedExtension()) {
92+
if ($this->isImage()) {
9393
$name = preg_replace('#\.[^.]+$#D', '', $name);
94-
$name .= '.' . $ext;
94+
$name .= '.' . $this->getSuggestedExtension();
9595
}
9696

9797
return $name;

tests/Http/FileUpload.getSanitizedName.phpt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ use Tester\Assert;
1212
require __DIR__ . '/../bootstrap.php';
1313

1414

15-
function getSanitizedName(string $name, ?string $ext = null): string
15+
function getSanitizedName(string $name, ?string $type = null): string
1616
{
1717
$file = new FileUpload(['name' => $name, 'size' => 0, 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE]);
18-
Assert::with($file, fn() => $file->extension = $ext);
18+
Assert::with($file, function () use ($file, $type) {
19+
$file->type = $type;
20+
$file->extension = $type === null ? null : explode('/', $type)[1];
21+
});
1922
return $file->getSanitizedName();
2023
}
2124

@@ -34,10 +37,20 @@ test('name', function () {
3437

3538

3639
test('name & extension', function () {
37-
Assert::same('unknown.jpeg', getSanitizedName('', 'jpeg'));
38-
Assert::same('unknown.jpeg', getSanitizedName('--', 'jpeg'));
39-
Assert::same('foo.jpeg', getSanitizedName('foo', 'jpeg'));
40-
Assert::same('foo.jpeg', getSanitizedName('foo.jpg', 'jpeg'));
41-
Assert::same('foo.jpeg', getSanitizedName('foo.php', 'jpeg'));
42-
Assert::same('image.jpeg', getSanitizedName('./.image.png', 'jpeg'));
40+
Assert::same('unknown', getSanitizedName('', 'application/pdf'));
41+
Assert::same('unknown', getSanitizedName('--', 'application/pdf'));
42+
Assert::same('foo', getSanitizedName('foo', 'application/pdf'));
43+
Assert::same('foo.jpg', getSanitizedName('foo.jpg', 'application/pdf'));
44+
Assert::same('foo.php', getSanitizedName('foo.php', 'application/pdf'));
45+
Assert::same('image.png', getSanitizedName('./.image.png', 'application/pdf'));
46+
});
47+
48+
49+
test('image name & extension', function () {
50+
Assert::same('unknown.jpeg', getSanitizedName('', 'image/jpeg'));
51+
Assert::same('unknown.jpeg', getSanitizedName('--', 'image/jpeg'));
52+
Assert::same('foo.jpeg', getSanitizedName('foo', 'image/jpeg'));
53+
Assert::same('foo.jpeg', getSanitizedName('foo.jpg', 'image/jpeg'));
54+
Assert::same('foo.jpeg', getSanitizedName('foo.php', 'image/jpeg'));
55+
Assert::same('image.jpeg', getSanitizedName('./.image.png', 'image/jpeg'));
4356
});

0 commit comments

Comments
 (0)