Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions framework/web/MultipartFormDataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Comment thread
terabytesoftw marked this conversation as resolved.
Resolved
return [];
}

$boundary = trim($matches[1], '"');
$boundary = isset($matches[1]) && $matches[1] !== '' ? $matches[1] : $matches[2];
Comment thread
terabytesoftw marked this conversation as resolved.
Resolved
if ($boundary === '') {
return [];
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

$bodyParts = preg_split('/\\R?-+' . preg_quote($boundary, '/') . '/s', $rawBody);
array_pop($bodyParts); // last block always has no data, contains boundary ending like `--`
Expand Down
27 changes: 27 additions & 0 deletions tests/framework/web/MultipartFormDataParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down
Loading