Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.56 under development
------------------------

- Bug #21073: Fix `MultipartFormDataParser` boundary extraction when `Content-Type` has additional parameters (iliaal)
- Bug #21020: Fix duplicate `@return` annotation for `yii\db\ActiveRecord::hasOne()` (nazard)
- Bug #20873: Fix PHPDoc annotations for the `yii\log\Target::$enabled` (mspirkov)
- Enh #20875: Clarify the type of the `yii\base\Model::$errors` (mspirkov)
Expand Down
94 changes: 91 additions & 3 deletions framework/web/MultipartFormDataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,11 @@ public function parse($rawBody, $contentType)
return [];
}

if (!preg_match('/boundary="?(.*)"?$/is', $contentType, $matches)) {
$boundary = $this->getMimeParameter($contentType, 'boundary');
if ($boundary === null || $boundary === '') {
return [];
}

$boundary = trim($matches[1], '"');

$bodyParts = preg_split('/\\R?-+' . preg_quote($boundary, '/') . '/s', $rawBody);
array_pop($bodyParts); // last block always has no data, contains boundary ending like `--`

Expand Down Expand Up @@ -211,6 +210,95 @@ public function parse($rawBody, $contentType)
return $bodyParams;
}

/**
* Returns a MIME header parameter (RFC 2045), or `null` if it is absent.
*
* `;` and `name=value` inside quoted-strings are not treated as delimiters.
*
* @param string $header Content-Type (or similar) header value
* @param string $name parameter name (matched case-insensitively)
* @return string|null
*/
private function getMimeParameter($header, $name)
{
$name = strtolower($name);
$length = strlen($header);
$i = 0;

while ($i < $length) {
if ($header[$i] === '"') {
$i++;
while ($i < $length) {
if ($header[$i] === '\\') {
$i += ($i + 1 < $length) ? 2 : 1;
continue;
}
if ($header[$i] === '"') {
$i++;
break;
}
$i++;
}
continue;
}

if ($header[$i] !== ';') {
$i++;
continue;
}

$i++;
while ($i < $length && ($header[$i] === ' ' || $header[$i] === "\t")) {
$i++;
}

$attrStart = $i;
while ($i < $length && $header[$i] !== '=' && $header[$i] !== ';') {
$i++;
}
$attr = strtolower(trim(substr($header, $attrStart, $i - $attrStart)));

if ($attr === '' || $i >= $length || $header[$i] !== '=') {
continue;
}

$i++;
while ($i < $length && ($header[$i] === ' ' || $header[$i] === "\t")) {
$i++;
}

if ($i < $length && $header[$i] === '"') {
$i++;
$value = '';
while ($i < $length) {
if ($header[$i] === '\\' && $i + 1 < $length) {
$value .= $header[$i + 1];
$i += 2;
continue;
}
if ($header[$i] === '"') {
$i++;
break;
}
$value .= $header[$i];
$i++;
}
} else {
$valueStart = $i;
while ($i < $length && $header[$i] !== ';' && $header[$i] !== ' ' && $header[$i] !== "\t") {
$i++;
}
$value = substr($header, $valueStart, $i - $valueStart);
}

if ($attr === $name) {
return $value;
}
}

return null;
}

/**
* Parses content part headers.
* @param string $headerContent headers source content
Expand Down
33 changes: 33 additions & 0 deletions tests/framework/web/MultipartFormDataParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,39 @@

class MultipartFormDataParserTest extends TestCase
{
public function testParseWithBoundaryFollowedByAdditionalParameters(): void
{
$parser = new MultipartFormDataParser();

$boundary = '---------------------------22472926011618';
$rawBody = "--{$boundary}\nContent-Disposition: form-data; name=\"title\"\r\n\r\ntest-title";
$rawBody .= "\r\n--{$boundary}--";

foreach (
[
'multipart/form-data; boundary=' . $boundary . '; charset=utf-8',
'multipart/form-data; boundary="' . $boundary . '"; charset=utf-8',
'multipart/form-data; x-boundary=wrong; boundary=' . $boundary,
'multipart/form-data; foo="a; boundary=wrong"; boundary=' . $boundary,
'multipart/form-data; charset="boundary=abc"; boundary=' . $boundary,
'multipart/form-data; charset="boundary=abc"; boundary="' . $boundary . '"',
] as $contentType
) {
$bodyParams = $parser->parse($rawBody, $contentType);
$this->assertSame(['title' => 'test-title'], $bodyParams, 'Content-Type: ' . $contentType);
}
}

public function testParseWithoutBoundary(): void
{
$parser = new MultipartFormDataParser();

$this->assertSame([], $parser->parse("--irrelevant\r\n\r\ndata", 'multipart/form-data'));
$this->assertSame([], $parser->parse('--irrelevant', 'multipart/form-data; boundary='));
$this->assertSame([], $parser->parse('--irrelevant', 'multipart/form-data; boundary=""'));
$this->assertSame([], $parser->parse('--irrelevant', 'multipart/form-data; boundary=""; charset=utf-8'));
}

public function testParse(): void
{
if (defined('HHVM_VERSION')) {
Expand Down
Loading