8000 [HttpFoundation] Throw exception if filename encoding cannot be detected · symfony/symfony@9762238 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9762238

Browse files
committed
[HttpFoundation] Throw exception if filename encoding cannot be detected
1 parent ee58cfc commit 9762238

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ public function setAutoEtag()
154154
* @param string $filenameFallback A fallback filename, containing only ASCII characters. Defaults to an automatically encoded filename
155155
*
156156
* @return $this
157+
*
158+
* @throws \InvalidArgumentException
157159
*/
158160
public function setContentDisposition($disposition, $filename = '', $filenameFallback = '')
159161
{
@@ -164,6 +166,10 @@ public function setContentDisposition($disposition, $filename = '', $filenameFal
164166
if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) {
165167
$encoding = mb_detect_encoding($filename, null, true);
166168

169+
if (false === $encoding) {
170+
throw new \InvalidArgumentException('The filename encoding cannot be detected.');
171+
}
172+
167173
for ($i = 0, $filenameLength = mb_strlen($filename, $encoding); $i < $filenameLength; ++$i) {
168174
$char = mb_substr($filename, $i, 1, $encoding);
169175

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ 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+
/**
72+
* @expectedException \InvalidArgumentException
73+
*/
74+
public function testSetContentDispositionThrowsExceptionWhenPassingInvalidEncodedFilename()
75+
{
76+
$response = new BinaryFileResponse(__FILE__);
77+
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, utf8_decode('föö.html'));
78+
}
79+
7180
/**
7281
* @dataProvider provideRanges
7382
*/

0 commit comments

Comments
 (0)
0