Skip to content

fix: add filename parameters to inline Content-Disposition headers #9638

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
Jul 18, 2025
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
15 changes: 7 additions & 8 deletions system/HTTP/DownloadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,21 @@ private function getDownloadFileName(): string
}

/**
* get Content-Disposition Header string.
* Get Content-Disposition Header string.
*/
private function getContentDisposition(): string
private function getContentDisposition(bool $inline = false): string
{
$downloadFilename = $this->getDownloadFileName();

$utf8Filename = $downloadFilename;
$downloadFilename = $utf8Filename = $this->getDownloadFileName();
$disposition = $inline ? 'inline' : 'attachment';

if (strtoupper($this->charset) !== 'UTF-8') {
$utf8Filename = mb_convert_encoding($downloadFilename, 'UTF-8', $this->charset);
}

$result = sprintf('attachment; filename="%s"', $downloadFilename);
$result = sprintf('%s; filename="%s"', $disposition, addslashes($downloadFilename));

if ($utf8Filename !== '') {
$result .= '; filename*=UTF-8\'\'' . rawurlencode($utf8Filename);
$result .= sprintf('; filename*=UTF-8\'\'%s', rawurlencode($utf8Filename));
}

return $result;
Expand Down Expand Up @@ -341,7 +340,7 @@ private function sendBodyByBinary()
*/
public function inline()
{
$this->setHeader('Content-Disposition', 'inline');
$this->setHeader('Content-Disposition', $this->getContentDisposition(true));

return $this;
}
Expand Down
11 changes: 10 additions & 1 deletion tests/system/HTTP/DownloadResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,16 @@ public function testDispositionInline(): void
$response = new DownloadResponse('unit-test.txt', true);
$response->inline();
$response->buildHeaders();
$this->assertSame('inline', $response->getHeaderLine('Content-Disposition'));
$this->assertSame('inline; filename="unit-test.txt"; filename*=UTF-8\'\'unit-test.txt', $response->getHeaderLine('Content-Disposition'));
}

public function testDispositionInlineWithSetFileName(): void
{
$response = new DownloadResponse('unit-test.txt', true);
$response->setFileName('my"quoted"File.txt');
$response->inline();
$response->buildHeaders();
$this->assertSame('inline; filename="my\"quoted\"File.txt"; filename*=UTF-8\'\'my%22quoted%22File.txt', $response->getHeaderLine('Content-Disposition'));
}

public function testNoCache(): void
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.6.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Bugs Fixed
- **CURLRequest:** Fixed a bug where intermediate HTTP responses were not properly removed from the response chain in certain scenarios, causing incorrect status codes and headers to be returned instead of the final response.
- **Database:** Fixed a bug where ``when()`` and ``whenNot()`` in ``ConditionalTrait`` incorrectly evaluated certain falsy values (such as ``[]``, ``0``, ``0.0``, and ``'0'``) as truthy, causing callbacks to be executed unexpectedly. These methods now cast the condition to a boolean using ``(bool)`` to ensure consistent behavior with PHP's native truthiness.
- **Database:** Fixed encapsulation violation in ``BasePreparedQuery`` when accessing ``BaseConnection::transStatus`` protected property.
- **DownloadResponse:** Fixed a bug where ``filename`` parameters were missing from ``Content-Disposition`` headers when using inline disposition, causing browsers to use the last URL segment for filenames instead of the intended filename.
- **Email:** Fixed a bug where ``Email::getHostname()`` failed to use ``$_SERVER['SERVER_ADDR']`` when ``$_SERVER['SERVER_NAME']`` was not set.
- **Security:** Fixed a bug where the ``sanitize_filename()`` function from the Security helper would throw an error when used in CLI requests.
- **Session:** Fixed a bug where using the ``DatabaseHandler`` with an unsupported database driver (such as ``SQLSRV``, ``OCI8``, or ``SQLite3``) did not throw an appropriate error.
Expand Down
Loading