diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 49dc42cac1e..fce59aaecf8 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -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 #21121: Fix `yii\validators\DateValidator` rejecting non-ASCII dates on PHP 8.4.27 and 8.5.12 (eluhr) - Bug #21020: Fix duplicate `@return` annotation for `yii\db\ActiveRecord::hasOne()` (nazard) - Bug #20873: Fix PHPDoc annotations for the `yii\log\Target::$enabled` (mspirkov) diff --git a/framework/web/MultipartFormDataParser.php b/framework/web/MultipartFormDataParser.php index 78583770233..382c2f3d6f7 100644 --- a/framework/web/MultipartFormDataParser.php +++ b/framework/web/MultipartFormDataParser.php @@ -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 `--` @@ -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 diff --git a/tests/framework/web/MultipartFormDataParserTest.php b/tests/framework/web/MultipartFormDataParserTest.php index b7e79f2ea3b..a36c29cd858 100644 --- a/tests/framework/web/MultipartFormDataParserTest.php +++ b/tests/framework/web/MultipartFormDataParserTest.php @@ -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')) {