Skip to content

Commit 8fd5569

Browse files
committed
[HttpFoundation] Generate safe fallback filename for wrongly encoded filename
1 parent ee58cfc commit 8fd5569

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function setAutoEtag()
150150
* Sets the Content-Disposition header with the given filename.
151151
*
152152
* @param string $disposition ResponseHeaderBag::DISPOSITION_INLINE or ResponseHeaderBag::DISPOSITION_ATTACHMENT
153-
* @param string $filename Optionally use this filename instead of the real name of the file
153+
* @param string $filename Optionally use this UTF-8 encoded filename instead of the real name of the file
154154
* @param string $filenameFallback A fallback filename, containing only ASCII characters. Defaults to an automatically encoded filename
155155
*
156156
* @return $this
@@ -162,7 +162,7 @@ public function setContentDisposition($disposition, $filename = '', $filenameFal
162162
}
163163

164164
if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) {
165-
$encoding = mb_detect_encoding($filename, null, true);
165+
$encoding = mb_detect_encoding($filename, null, true) ?: '8bit';
166166

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

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ public function testSetContentDispositionGeneratesSafeFallbackFilename()
6868
$this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%C3%B6%C3%B6.html', $response->headers->get('Content-Disposition'));
6969
}
7070

71+
public function testSetContentDispositionGeneratesSafeFallbackFilenameForWronglyEncodedFilename()
72+
{
73+
$response = new BinaryFileResponse(__FILE__);
74+
75+
$iso88591EncodedFilename = utf8_decode('föö.html');
76+
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $iso88591EncodedFilename);
77+
78+
// the parameter filename* is invalid in this case (rawurldecode('f%F6%F6') does not provide a UTF-8 string but an ISO-8859-1 encoded one)
79+
$this->assertSame('attachment; filename="f__.html"; filename*=utf-8\'\'f%F6%F6.html', $response->headers->get('Content-Disposition'));
80+
}
81+
7182
/**
7283
* @dataProvider provideRanges
7384
*/

0 commit comments

Comments
 (0)