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

[Mime] Fix encoding filenames in multipart/form-data #44728

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
Dec 20, 2021
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
18 changes: 17 additions & 1 deletion 18 src/Symfony/Component/Mime/Header/ParameterizedHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ private function createParameter(string $name, string $value): string
$maxValueLength = $this->getMaxLineLength() - \strlen($name.'*N*="";') - 1;
$firstLineOffset = \strlen($this->getCharset()."'".$this->getLanguage()."'");
}

if (\in_array($name, ['name', 'filename'], true) && 'form-data' === $this->getValue() && 'content-disposition' === strtolower($this->getName()) && preg_match('//u', $value)) {
// WHATWG HTML living standard 4.10.21.8 2 specifies:
// For field names and filenames for file fields, the result of the
// encoding in the previous bullet point must be escaped by replacing
// any 0x0A (LF) bytes with the byte sequence `%0A`, 0x0D (CR) with `%0D`
// and 0x22 (") with `%22`.
// The user agent must not perform any other escapes.
$value = str_replace(['"', "\r", "\n"], ['%22', '%0D', '%0A'], $value);

if (\strlen($value) <= $maxValueLength) {
return $name.'="'.$value.'"';
}

$value = $origValue;
}
}

// Encode if we need to
Expand Down Expand Up @@ -158,7 +174,7 @@ private function createParameter(string $name, string $value): string
*/
private function getEndOfParameterValue(string $value, bool $encoded = false, bool $firstLine = false): string
{
$forceHttpQuoting = 'content-disposition' === strtolower($this->getName()) && 'form-data' === $this->getValue();
$forceHttpQuoting = 'form-data' === $this->getValue() && 'content-disposition' === strtolower($this->getName());
if ($forceHttpQuoting || !preg_match('/^'.self::TOKEN_REGEX.'$/D', $value)) {
$value = '"'.$value.'"';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ public function testSpaceInParamResultsInQuotedString()
$this->assertEquals('attachment; filename="my file.txt"', $header->getBodyAsString());
}

public function testFormDataResultsInQuotedString()
{
$header = new ParameterizedHeader('Content-Disposition', 'form-data');
$header->setParameters(['filename' => 'file.txt']);
$this->assertEquals('form-data; filename="file.txt"', $header->getBodyAsString());
}

public function testFormDataUtf8()
{
$header = new ParameterizedHeader('Content-Disposition', 'form-data');
$header->setParameters(['filename' => "déjà%\"\n\r.txt"]);
$this->assertEquals('form-data; filename="déjà%%22%0A%0D.txt"', $header->getBodyAsString());
}

public function testLongParamsAreBrokenIntoMultipleAttributeStrings()
{
/* -- RFC 2231, 3.
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.