Skip to content

Commit 861557c

Browse files
committed
Filename sanitization optimized to remove any leftover dot prefix
1 parent e88fd4c commit 861557c

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/Attachment.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,13 +324,17 @@ public function decodeName(?string $name): string {
324324
}
325325

326326
// sanitize $name
327-
// order of '..' is important
328327
$replaces = [
329328
'/\\\\/' => '',
330329
'/[\/\0:]+/' => '',
331330
'/\.+/' => '.',
332331
];
333-
return preg_replace(array_keys($replaces), array_values($replaces), $name);
332+
$name_starts_with_dots = str_starts_with($name, '..');
333+
$name = preg_replace(array_keys($replaces), array_values($replaces), $name);
334+
if($name_starts_with_dots) {
335+
return substr($name, 1);
336+
}
337+
return $name;
334338
}
335339
return "";
336340
}

tests/AttachmentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testDecodeName(string $input, string $output): void
2828
public function decodeNameDataProvider(): array
2929
{
3030
return [
31-
['../../../../../../../../../../../var/www/shell.php', '.varwwwshell.php'],
31+
['../../../../../../../../../../../var/www/shell.php', 'varwwwshell.php'],
3232
['test..xml', 'test.xml'],
3333
[chr(0), ''],
3434
['C:\\file.txt', 'Cfile.txt'],

0 commit comments

Comments
 (0)