From 30ce483db2b905fd2a8485d4a1cc1bfb287f3174 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Sun, 23 Aug 2026 13:31:19 -0400 Subject: [PATCH 1/2] Fix boundary extraction from Content-Type with additional parameters The greedy pattern boundary="?(.*)"?$ captured everything up to the end of the header, so a header like multipart/form-data; boundary=ABC; charset=utf-8 extracted the boundary 'ABC; charset=utf-8' and parsing returned no parts. Match either a quoted boundary string or an unquoted token that ends at the next parameter separator or whitespace instead, and bail out on an empty boundary. --- framework/web/MultipartFormDataParser.php | 7 +++-- .../web/MultipartFormDataParserTest.php | 27 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/framework/web/MultipartFormDataParser.php b/framework/web/MultipartFormDataParser.php index 78583770233..7e33e58ee82 100644 --- a/framework/web/MultipartFormDataParser.php +++ b/framework/web/MultipartFormDataParser.php @@ -142,11 +142,14 @@ public function parse($rawBody, $contentType) return []; } - if (!preg_match('/boundary="?(.*)"?$/is', $contentType, $matches)) { + if (!preg_match('/boundary=(?:"([^"]*)"|([^;\s]*))/is', $contentType, $matches)) { return []; } - $boundary = trim($matches[1], '"'); + $boundary = isset($matches[1]) && $matches[1] !== '' ? $matches[1] : $matches[2]; + if ($boundary === '') { + return []; + } $bodyParts = preg_split('/\\R?-+' . preg_quote($boundary, '/') . '/s', $rawBody); array_pop($bodyParts); // last block always has no data, contains boundary ending like `--` diff --git a/tests/framework/web/MultipartFormDataParserTest.php b/tests/framework/web/MultipartFormDataParserTest.php index b7e79f2ea3b..2ca9e37e09b 100644 --- a/tests/framework/web/MultipartFormDataParserTest.php +++ b/tests/framework/web/MultipartFormDataParserTest.php @@ -13,6 +13,33 @@ 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', + ] 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=')); + } + public function testParse(): void { if (defined('HHVM_VERSION')) { From 3d09f43733745e33c0d9ace98f8d03dafe2ff8fc Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Wed, 26 Aug 2026 13:11:50 -0400 Subject: [PATCH 2/2] Parse Content-Type parameters with quoted-string awareness Empty quoted boundary (`boundary=""`) left $matches[2] unset and became a PHP warning. The substring match also picked `boundary=` inside other parameter names and quoted values. Split parameters only outside quoted-strings, reject an empty boundary, and cover those cases in tests. --- framework/CHANGELOG.md | 1 + framework/web/MultipartFormDataParser.php | 97 +++++++++++++++++-- .../web/MultipartFormDataParserTest.php | 6 ++ 3 files changed, 98 insertions(+), 6 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 79593edccd9..bbbcd2f1d3c 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 #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) diff --git a/framework/web/MultipartFormDataParser.php b/framework/web/MultipartFormDataParser.php index 7e33e58ee82..382c2f3d6f7 100644 --- a/framework/web/MultipartFormDataParser.php +++ b/framework/web/MultipartFormDataParser.php @@ -142,12 +142,8 @@ public function parse($rawBody, $contentType) return []; } - if (!preg_match('/boundary=(?:"([^"]*)"|([^;\s]*))/is', $contentType, $matches)) { - return []; - } - - $boundary = isset($matches[1]) && $matches[1] !== '' ? $matches[1] : $matches[2]; - if ($boundary === '') { + $boundary = $this->getMimeParameter($contentType, 'boundary'); + if ($boundary === null || $boundary === '') { return []; } @@ -214,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 2ca9e37e09b..a36c29cd858 100644 --- a/tests/framework/web/MultipartFormDataParserTest.php +++ b/tests/framework/web/MultipartFormDataParserTest.php @@ -25,6 +25,10 @@ public function testParseWithBoundaryFollowedByAdditionalParameters(): void [ '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); @@ -38,6 +42,8 @@ public function testParseWithoutBoundary(): void $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