Skip to content

Commit aad224b

Browse files
committed
Ignore excessive number of empty file uploads
1 parent c1e9c32 commit aad224b

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/Io/MultipartParser.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ final class MultipartParser
6363

6464
private $postCount = 0;
6565
private $filesCount = 0;
66+
private $emptyCount = 0;
6667

6768
/**
6869
* @param int|null $uploadMaxFilesize
@@ -97,6 +98,7 @@ public function parse(ServerRequestInterface $request)
9798
$this->request = null;
9899
$this->postCount = 0;
99100
$this->filesCount = 0;
101+
$this->emptyCount = 0;
100102
$this->maxFileSize = null;
101103

102104
return $request;
@@ -176,6 +178,11 @@ private function parseUploadedFile($filename, $contentType, $contents)
176178

177179
// no file selected (zero size and empty filename)
178180
if ($size === 0 && $filename === '') {
181+
// ignore excessive number of empty file uploads
182+
if (++$this->emptyCount + $this->filesCount > $this->maxInputVars) {
183+
return;
184+
}
185+
179186
return new UploadedFile(
180187
Psr7\stream_for(''),
181188
$size,

tests/Middleware/RequestBodyParserMiddlewareTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,44 @@ function (ServerRequestInterface $request) {
316316
$this->assertTrue(isset($body['a']));
317317
$this->assertCount($allowed, $body['a']);
318318
}
319+
320+
public function testMultipartFormDataTruncatesExcessiveNumberOfEmptyFileUploads()
321+
{
322+
// ini setting exists in PHP 5.3.9, not in HHVM: https://3v4l.org/VF6oV
323+
// otherwise default to 1000 as implemented within
324+
$allowed = (int)ini_get('max_input_vars');
325+
if ($allowed === 0) {
326+
$allowed = 1000;
327+
}
328+
329+
$middleware = new RequestBodyParserMiddleware();
330+
331+
$boundary = "---------------------------12758086162038677464950549563";
332+
333+
$data = "";
334+
for ($i = 0; $i < $allowed + 1; ++$i) {
335+
$data .= "--$boundary\r\n";
336+
$data .= "Content-Disposition: form-data; name=\"empty[]\"; filename=\"\"\r\n";
337+
$data .= "\r\n";
338+
$data .= "\r\n";
339+
}
340+
$data .= "--$boundary--\r\n";
341+
342+
$request = new ServerRequest('POST', 'http://example.com/', array(
343+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
344+
), $data, 1.1);
345+
346+
/** @var ServerRequestInterface $parsedRequest */
347+
$parsedRequest = $middleware(
348+
$request,
349+
function (ServerRequestInterface $request) {
350+
return $request;
351+
}
352+
);
353+
354+
$body = $parsedRequest->getUploadedFiles();
355+
$this->assertCount(1, $body);
356+
$this->assertTrue(isset($body['empty']));
357+
$this->assertCount($allowed, $body['empty']);
358+
}
319359
}

0 commit comments

Comments
 (0)