Skip to content

[HttpFoundation] automatically generate safe fallback filename #16656

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

Merged
merged 1 commit into from
Mar 4, 2016
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
14 changes: 14 additions & 0 deletions src/Symfony/Component/HttpFoundation/BinaryFileResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ public function setContentDisposition($disposition, $filename = '', $filenameFal
$filename = $this->file->getFilename();
}

if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the if block should be moved in ResponseHeaderBag::makeDisposition IMO, isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that people will expect the ResponseHeaderBag::makeDisposition() method to throw an exception when invalid characters are passed. Silently convertin the fallback filename is a BC break imo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point.

$encoding = mb_detect_encoding($filename, null, true);

for ($i = 0; $i < mb_strlen($filename, $encoding); ++$i) {
$char = mb_substr($filename, $i, 1, $encoding);

if ('%' === $char || ord($char) < 32 || ord($char) > 126) {
$filenameFallback .= '_';
} else {
$filenameFallback .= $char;
}
}
}

$dispositionHeader = $this->headers->makeDisposition($disposition, $filename, $filenameFallback);
$this->headers->set('Content-Disposition', $dispositionHeader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function testConstruction()
$this->assertEquals('inline; filename="README.md"', $response->headers->get('Content-Disposition'));
}

public function testConstructWithNonAsciiFilename()
{
new BinaryFileResponse(__DIR__.'/Fixtures/föö.html', 200, array(), true, 'attachment');
}

/**
* @expectedException \LogicException
*/
Expand All @@ -49,6 +54,14 @@ public function testGetContent()
$this->assertFalse($response->getContent());
}

public function testSetContentDispositionGeneratesSafeFallbackFilename()
{
$response = new BinaryFileResponse(__FILE__);
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'föö.html');

$this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html', $response->headers->get('Content-Disposition'));
}

/**
* @dataProvider provideRanges
*/
Expand Down
Empty file.