Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Attachment::decodeName remove .. from file name #501

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,12 @@ public function decodeName(?string $name): string {

// sanitize $name
// order of '..' is important
return str_replace(['\\', '/', chr(0), ':', '..'], '', $name);
$replaces = [
'/\\\\/' => '',
'/[\/\0:]+/' => '',
'/\.+/' => '.',
];
return preg_replace(array_keys($replaces), array_values($replaces), $name);
}
return "";
}
Expand Down
37 changes: 37 additions & 0 deletions tests/AttachmentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Tests;

use Tests\fixtures\FixtureTestCase;
use Webklex\PHPIMAP\Attachment;

class AttachmentTest extends FixtureTestCase
{
protected Attachment $attachment;

public function setUp(): void
{
$message = $this->getFixture("attachment_encoded_filename.eml");
$this->attachment = $message->getAttachments()->first();
}
/**
* @dataProvider decodeNameDataProvider
*/
public function testDecodeName(string $input, string $output): void
{
$name = $this->attachment->decodeName($input);
$this->assertEquals($output, $name);
}

public function decodeNameDataProvider(): array
{
return [
['../../../../../../../../../../../var/www/shell.php', '.varwwwshell.php'],
['test..xml', 'test.xml'],
[chr(0), ''],
['C:\\file.txt', 'Cfile.txt'],
];
}
}