Skip to content

Commit 87ac5fa

Browse files
committed
fix(HTTP): strip CRLF from DownloadResponse filename to prevent header injection
Sanitize carriage return and newline characters from filename in DownloadResponse to prevent HTTP response splitting and invalid header values per RFC 7230.
1 parent 501706c commit 87ac5fa

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

‎system/HTTP/DownloadResponse.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function __construct(string $filename, bool $setMime)
7171
{
7272
parent::__construct(config(App::class));
7373

74-
$this->filename = $filename;
74+
$this->filename = str_replace(["\r", "\n"], '', $filename);
7575
$this->setMime = $setMime;
7676

7777
// Make sure the content type is either specified or detected
@@ -113,7 +113,7 @@ public function setFilePath(string $filepath)
113113
*/
114114
public function setFileName(string $filename)
115115
{
116-
$this->filename = $filename;
116+
$this->filename = str_replace(["\r", "\n"], '', $filename);
117117

118118
return $this;
119119
}
@@ -161,8 +161,8 @@ private function setContentTypeByMimeType(): void
161161
*/
162162
private function getDownloadFileName(): string
163163
{
164-
$filename = $this->filename;
165-
$x = explode('.', $this->filename);
164+
$filename = $this->filename;
165+
$x = explode('.', $filename);
166166
$extension = end($x);
167167

168168
/* It was reported that browsers on Android 2.1 (and possibly older as well)

‎tests/system/HTTP/DownloadResponseTest.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,30 @@ public function testDispositionInlineWithSetFileName(): void
154154
$this->assertSame('inline; filename="my\"quoted\"File.txt"; filename*=UTF-8\'\'my%22quoted%22File.txt', $response->getHeaderLine('Content-Disposition'));
155155
}
156156

157+
public function testContentDispositionSanitizesCRLF(): void
158+
{
159+
$response = new DownloadResponse("test\r\nfile.txt", false);
160+
$response->buildHeaders();
161+
162+
$header = $response->getHeaderLine('Content-Disposition');
163+
$this->assertStringNotContainsString("\r", $header);
164+
$this->assertStringNotContainsString("\n", $header);
165+
$this->assertSame('attachment; filename="testfile.txt"; filename*=UTF-8\'\'testfile.txt', $header);
166+
167+
$response = new DownloadResponse('report.pdf', false);
168+
$response->setFileName("report\r\nSet-Cookie: evil=1\r\n.pdf");
169+
170+
// The filename must be sanitized to strip carriage returns and newlines,
171+
// preventing HTTP response splitting / header injection without throwing an RFC 7230 error.
172+
$response->buildHeaders();
173+
174+
$header = $response->getHeaderLine('Content-Disposition');
175+
$this->assertStringNotContainsString("\r", $header);
176+
$this->assertStringNotContainsString("\n", $header);
177+
$this->assertFalse($response->hasHeader('Set-Cookie'));
178+
$this->assertSame('attachment; filename="reportSet-Cookie: evil=1.pdf"; filename*=UTF-8\'\'reportSet-Cookie%3A%20evil%3D1.pdf', $header);
179+
}
180+
157181
public function testNoCache(): void
158182
{
159183
$response = new DownloadResponse('unit-test.txt', true);

‎user_guide_src/source/changelogs/v4.7.5.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Bugs Fixed
4343
- **Content Security Policy:** Fixed a bug where empty ``Content-Security-Policy``, ``Content-Security-Policy-Report-Only``, and ``Reporting-Endpoints`` response headers were generated when no corresponding values existed.
4444
- **Cookie:** Fixed a bug where ``Cookie`` instances created with ``raw: true`` allowed invalid characters in cookie values rejected by ``setrawcookie()``.
4545
- **Database:** Fixed a bug where rebuilding a SQLite3 table (e.g., ``Forge::dropColumn()``, ``Forge::modifyColumn()``, ``Forge::dropForeignKey()`` and ``Forge::dropPrimaryKey()``) corrupted the table names referenced by its foreign keys when ``DBPrefix`` was set.
46+
- **DownloadResponse:** Fixed a bug where filenames containing carriage returns or newlines were not sanitized, potentially leading to HTTP response splitting (CRLF / header injection).
4647
- **Files:** Fixed a bug where ``File::move()`` and ``UploadedFile::move()`` set executable and overly permissive file permissions (``0777 & ~umask()`` instead of ``0666 & ~umask()``), and ``UploadedFile::move()`` targeted the parent directory instead of the destination file for ``chmod()``.
4748
- **Helpers:** Fixed a bug where ``get_dir_file_info()`` returned incomplete entries for subdirectories and missing files instead of omitting them.
4849
- **Honeypot:** Fixed a bug where bot detection returned an HTTP 500 response instead of 403 (Forbidden).

0 commit comments

Comments
 (0)