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

[stable21] Escape filename in Content-Disposition #27359

Merged
merged 1 commit into from
Jun 7, 2021
Merged
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
8 changes: 2 additions & 6 deletions lib/public/AppFramework/Http/DownloadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,16 @@
* @since 7.0.0
*/
class DownloadResponse extends Response {
private $filename;
private $contentType;

/**
* Creates a response that prompts the user to download the file
* @param string $filename the name that the downloaded file should have
* @param string $contentType the mimetype that the downloaded file should have
* @since 7.0.0
*/
public function __construct($filename, $contentType) {
public function __construct(string $filename, string $contentType) {
parent::__construct();

$this->filename = $filename;
$this->contentType = $contentType;
$filename = strtr($filename, ['"' => '\\"', '\\' => '\\\\']);

$this->addHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
$this->addHeader('Content-Type', $contentType);
Expand Down
36 changes: 25 additions & 11 deletions tests/lib/AppFramework/Http/DownloadResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,36 @@ class ChildDownloadResponse extends DownloadResponse {


class DownloadResponseTest extends \Test\TestCase {

/**
* @var ChildDownloadResponse
*/
protected $response;

protected function setUp(): void {
parent::setUp();
$this->response = new ChildDownloadResponse('file', 'content');
}


public function testHeaders() {
$headers = $this->response->getHeaders();
$response = new ChildDownloadResponse('file', 'content');
$headers = $response->getHeaders();

$this->assertEquals('attachment; filename="file"', $headers['Content-Disposition']);
$this->assertEquals('content', $headers['Content-Type']);
}

/**
* @dataProvider filenameEncodingProvider
*/
public function testFilenameEncoding(string $input, string $expected) {
$response = new ChildDownloadResponse($input, 'content');
$headers = $response->getHeaders();

$this->assertEquals('attachment; filename="'.$expected.'"', $headers['Content-Disposition']);
}

$this->assertStringContainsString('attachment; filename="file"', $headers['Content-Disposition']);
$this->assertStringContainsString('content', $headers['Content-Type']);
public function filenameEncodingProvider() : array {
return [
['TestName.txt', 'TestName.txt'],
['A "Quoted" Filename.txt', 'A \\"Quoted\\" Filename.txt'],
['A "Quoted" Filename.txt', 'A \\"Quoted\\" Filename.txt'],
['A "Quoted" Filename With A Backslash \\.txt', 'A \\"Quoted\\" Filename With A Backslash \\\\.txt'],
['A "Very" Weird Filename \ / & <> " >\'""""\.text', 'A \\"Very\\" Weird Filename \\\\ / & <> \\" >\'\\"\\"\\"\\"\\\\.text'],
['\\\\\\\\\\\\', '\\\\\\\\\\\\\\\\\\\\\\\\'],
];
}
}