Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5bbe951

Browse filesBrowse files
committed
bug #16656 [HttpFoundation] automatically generate safe fallback filename (xabbuh)
This PR was merged into the 2.3 branch. Discussion ---------- [HttpFoundation] automatically generate safe fallback filename | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #16603 | License | MIT | Doc PR | Commits ------- 03721e3 automatically generate safe fallback filename
2 parents ce60be5 + 03721e3 commit 5bbe951
Copy full SHA for 5bbe951

File tree

Expand file treeCollapse file tree

3 files changed

+27
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+27
-0
lines changed

‎src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/BinaryFileResponse.php
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ public function setContentDisposition($disposition, $filename = '', $filenameFal
160160
$filename = $this->file->getFilename();
161161
}
162162

163+
if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) {
164+
$encoding = mb_detect_encoding($filename, null, true);
165+
166+
for ($i = 0; $i < mb_strlen($filename, $encoding); ++$i) {
167+
$char = mb_substr($filename, $i, 1, $encoding);
168+
169+
if ('%' === $char || ord($char) < 32 || ord($char) > 126) {
170+
$filenameFallback .= '_';
171+
} else {
172+
$filenameFallback .= $char;
173+
}
174+
}
175+
}
176+
163177
$dispositionHeader = $this->headers->makeDisposition($disposition, $filename, $filenameFallback);
164178
$this->headers->set('Content-Disposition', $dispositionHeader);
165179

‎src/Symfony/Component/HttpFoundation/Tests/BinaryFileResponseTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/BinaryFileResponseTest.php
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function testConstruction()
3434
$this->assertEquals('inline; filename="README.md"', $response->headers->get('Content-Disposition'));
3535
}
3636

37+
public function testConstructWithNonAsciiFilename()
38+
{
39+
new BinaryFileResponse(__DIR__.'/Fixtures/föö.html', 200, array(), true, 'attachment');
40+
}
41+
3742
/**
3843
* @expectedException \LogicException
3944
*/
@@ -49,6 +54,14 @@ public function testGetContent()
4954
$this->assertFalse($response->getContent());
5055
}
5156

57+
public function testSetContentDispositionGeneratesSafeFallbackFilename()
58+
{
59+
$response = new BinaryFileResponse(__FILE__);
60+
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'föö.html');
61+
62+
$this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html', $response->headers->get('Content-Disposition'));
63+
}
64+
5265
/**
5366
* @dataProvider provideRanges
5467
*/

‎src/Symfony/Component/HttpFoundation/Tests/Fixtures/föö.html

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/Fixtures/föö.html
Whitespace-only changes.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.