Skip to content

Commit 1cabca6

Browse files
authored
Fix: Attachment::decodeName remove .. from file name (Webklex#501)
If attached file has name like test..xml, then dots remove and broke file extension.
1 parent 6bd8ba4 commit 1cabca6

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Attachment.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,12 @@ public function decodeName(?string $name): string {
325325

326326
// sanitize $name
327327
// order of '..' is important
328-
return str_replace(['\\', '/', chr(0), ':', '..'], '', $name);
328+
$replaces = [
329+
'/\\\\/' => '',
330+
'/[\/\0:]+/' => '',
331+
'/\.+/' => '.',
332+
];
333+
return preg_replace(array_keys($replaces), array_values($replaces), $name);
329334
}
330335
return "";
331336
}

tests/AttachmentTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use Tests\fixtures\FixtureTestCase;
8+
use Webklex\PHPIMAP\Attachment;
9+
10+
class AttachmentTest extends FixtureTestCase
11+
{
12+
protected Attachment $attachment;
13+
14+
public function setUp(): void
15+
{
16+
$message = $this->getFixture("attachment_encoded_filename.eml");
17+
$this->attachment = $message->getAttachments()->first();
18+
}
19+
/**
20+
* @dataProvider decodeNameDataProvider
21+
*/
22+
public function testDecodeName(string $input, string $output): void
23+
{
24+
$name = $this->attachment->decodeName($input);
25+
$this->assertEquals($output, $name);
26+
}
27+
28+
public function decodeNameDataProvider(): array
29+
{
30+
return [
31+
['../../../../../../../../../../../var/www/shell.php', '.varwwwshell.php'],
32+
['test..xml', 'test.xml'],
33+
[chr(0), ''],
34+
['C:\\file.txt', 'Cfile.txt'],
35+
];
36+
}
37+
}

0 commit comments

Comments
 (0)