Skip to content

Add request_parse_body() function #11472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 Zend/Optimizer/zend_func_infos.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ static const func_info_t func_infos[] = {
F1("fsockopen", MAY_BE_RESOURCE|MAY_BE_FALSE),
FN("pfsockopen", MAY_BE_RESOURCE|MAY_BE_FALSE),
F1("http_build_query", MAY_BE_STRING),
F1("request_parse_body", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_ARRAY),
F1("image_type_to_mime_type", MAY_BE_STRING),
F1("image_type_to_extension", MAY_BE_STRING|MAY_BE_FALSE),
F1("getimagesize", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_KEY_STRING|MAY_BE_ARRAY_OF_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE),
Expand Down
25 changes: 25 additions & 0 deletions Zend/tests/request_parse_body/invalid_boundary.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
request_parse_body() with multipart and invalid boundary
--ENV--
REQUEST_METHOD=PUT
--POST_RAW--
Content-Type: multipart/form-data; boundary="foobar
empty
--FILE--
<?php

try {
[$_POST, $_FILES] = request_parse_body();
} catch (Throwable $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

var_dump($_POST, $_FILES);

?>
--EXPECT--
RequestParseBodyException: Invalid boundary in multipart/form-data POST data
array(0) {
}
array(0) {
}
56 changes: 56 additions & 0 deletions Zend/tests/request_parse_body/multipart.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
--TEST--
request_parse_body() with multipart
--ENV--
REQUEST_METHOD=PUT
--POST_RAW--
Content-Type: multipart/form-data; boundary=---------------------------84000087610663814162942123332
-----------------------------84000087610663814162942123332
Content-Disposition: form-data; name="post_field_name"

post field data
-----------------------------84000087610663814162942123332
Content-Disposition: form-data; name="file_name"; filename="original_file_name.txt"
Content-Type: text/plain

file data
-----------------------------84000087610663814162942123332--
--FILE--
<?php

[$_POST, $_FILES] = request_parse_body();

var_dump($_POST, $_FILES);

$file_path = __DIR__ . '/put_multipart_uploaded_file.txt';
move_uploaded_file($_FILES['file_name']['tmp_name'], $file_path);
var_dump(file_get_contents($file_path));

?>
--CLEAN--
<?php
$file_path = __DIR__ . '/put_multipart_uploaded_file.txt';
@unlink($file_path);
?>
--EXPECTF--
array(1) {
["post_field_name"]=>
string(15) "post field data"
}
array(1) {
["file_name"]=>
array(6) {
["name"]=>
string(22) "original_file_name.txt"
["full_path"]=>
string(22) "original_file_name.txt"
["type"]=>
string(10) "text/plain"
["tmp_name"]=>
string(%d) "%s"
["error"]=>
int(0)
["size"]=>
int(9)
}
}
string(9) "file data"
32 changes: 32 additions & 0 deletions Zend/tests/request_parse_body/multipart_garbled.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
request_parse_body() with multipart and garbled field
--INI--
max_file_uploads=1
--ENV--
REQUEST_METHOD=PUT
--POST_RAW--
Content-Type: multipart/form-data; boundary=---------------------------84000087610663814162942123332
-----------------------------84000087610663814162942123332
Content-Disposition: form-data;
Content-Type: text/plain

post field data
-----------------------------84000087610663814162942123332--
--FILE--
<?php

try {
[$_POST, $_FILES] = request_parse_body();
} catch (Throwable $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

var_dump($_POST, $_FILES);

?>
--EXPECT--
RequestParseBodyException: File Upload Mime headers garbled
array(0) {
}
array(0) {
}
37 changes: 37 additions & 0 deletions Zend/tests/request_parse_body/multipart_max_files.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
request_parse_body() with multipart and exceeding max files
--INI--
max_file_uploads=1
--ENV--
REQUEST_METHOD=PUT
--POST_RAW--
Content-Type: multipart/form-data; boundary=---------------------------84000087610663814162942123332
-----------------------------84000087610663814162942123332
Content-Disposition: form-data; name="file1"; filename="file1.txt"
Content-Type: text/plain

file data
-----------------------------84000087610663814162942123332
Content-Disposition: form-data; name="file2"; filename="file2.txt"
Content-Type: text/plain

file data
-----------------------------84000087610663814162942123332--
--FILE--
<?php

try {
[$_POST, $_FILES] = request_parse_body();
} catch (Throwable $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

var_dump($_POST, $_FILES);

?>
--EXPECT--
RequestParseBodyException: Maximum number of allowable file uploads has been exceeded
array(0) {
}
array(0) {
}
35 changes: 35 additions & 0 deletions Zend/tests/request_parse_body/multipart_max_input_vars.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
request_parse_body() with multipart and exceeding max input vars
--INI--
max_input_vars=1
--ENV--
REQUEST_METHOD=PUT
--POST_RAW--
Content-Type: multipart/form-data; boundary=---------------------------84000087610663814162942123332
-----------------------------84000087610663814162942123332
Content-Disposition: form-data; name="field1"

post field data
-----------------------------84000087610663814162942123332
Content-Disposition: form-data; name="field2"

post field data
-----------------------------84000087610663814162942123332--
--FILE--
<?php

try {
[$_POST, $_FILES] = request_parse_body();
} catch (Throwable $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

var_dump($_POST, $_FILES);

?>
--EXPECT--
RequestParseBodyException: Input variables exceeded 1. To increase the limit change max_input_vars in php.ini.
array(0) {
}
array(0) {
}
36 changes: 36 additions & 0 deletions Zend/tests/request_parse_body/multipart_max_parts.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
request_parse_body() with multipart and exceeding max parts
--INI--
max_multipart_body_parts=1
--ENV--
REQUEST_METHOD=PUT
--POST_RAW--
Content-Type: multipart/form-data; boundary=---------------------------84000087610663814162942123332
-----------------------------84000087610663814162942123332
Content-Disposition: form-data; name="post_field_name"

post field data
-----------------------------84000087610663814162942123332
Content-Disposition: form-data; name="file_name"; filename="original_file_name.txt"
Content-Type: text/plain

file data
-----------------------------84000087610663814162942123332--
--FILE--
<?php

try {
[$_POST, $_FILES] = request_parse_body();
} catch (Throwable $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

var_dump($_POST, $_FILES);

?>
--EXPECT--
RequestParseBodyException: Multipart body parts limit exceeded 1. To increase the limit change max_multipart_body_parts in php.ini.
array(0) {
}
array(0) {
}
27 changes: 27 additions & 0 deletions Zend/tests/request_parse_body/multipart_missing_boundary.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
request_parse_body() with multipart and missing boundary
--ENV--
REQUEST_METHOD=PUT
--POST_RAW--
Content-Type: multipart/form-data
empty
--FILE--
<?php

$stream = fopen('php://memory','r+');

try {
[$_POST, $_FILES] = request_parse_body();
} catch (Throwable $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

var_dump($_POST, $_FILES);

?>
--EXPECT--
RequestParseBodyException: Missing boundary in multipart/form-data POST data
array(0) {
}
array(0) {
}
21 changes: 21 additions & 0 deletions Zend/tests/request_parse_body/multipart_options_invalid_key.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
request_parse_body() invalid key
--FILE--
<?php

try {
request_parse_body(options: ['foo' => 1]);
} catch (Error $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

try {
request_parse_body(options: [42 => 1]);
} catch (Error $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

?>
--EXPECT--
ValueError: Invalid key "foo" in $options argument
ValueError: Invalid integer key in $options argument
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
request_parse_body() invalid quantity
--FILE--
<?php

try {
request_parse_body(options: [
'upload_max_filesize' => '1GB',
]);
} catch (Throwable $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

?>
--EXPECTF--
Warning: Invalid quantity "1GB": unknown multiplier "B", interpreting as "1" for backwards compatibility in %s on line %d
RequestParseBodyException: Request does not provide a content type
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
request_parse_body() invalid value type
--FILE--
<?php

try {
request_parse_body(options: [
'max_input_vars' => [],
]);
} catch (Error $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

?>
--EXPECT--
ValueError: Invalid array value in $options argument
39 changes: 39 additions & 0 deletions Zend/tests/request_parse_body/multipart_options_max_files.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
request_parse_body() max_file_uploads option
--INI--
max_file_uploads=10
--ENV--
REQUEST_METHOD=PUT
--POST_RAW--
Content-Type: multipart/form-data; boundary=---------------------------84000087610663814162942123332
-----------------------------84000087610663814162942123332
Content-Disposition: form-data; name="file1"; filename="file1.txt"
Content-Type: text/plain

file data
-----------------------------84000087610663814162942123332
Content-Disposition: form-data; name="file2"; filename="file2.txt"
Content-Type: text/plain

file data
-----------------------------84000087610663814162942123332--
--FILE--
<?php

try {
[$_POST, $_FILES] = request_parse_body([
'max_file_uploads' => 1,
]);
} catch (Throwable $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

var_dump($_POST, $_FILES);

?>
--EXPECT--
RequestParseBodyException: Maximum number of allowable file uploads has been exceeded
array(0) {
}
array(0) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
request_parse_body() max_input_vars option
--INI--
max_input_vars=10
--ENV--
REQUEST_METHOD=PUT
--POST_RAW--
Content-Type: multipart/form-data; boundary=---------------------------84000087610663814162942123332
-----------------------------84000087610663814162942123332
Content-Disposition: form-data; name="field1"

post field data
-----------------------------84000087610663814162942123332
Content-Disposition: form-data; name="field2"

post field data
-----------------------------84000087610663814162942123332--
--FILE--
<?php

try {
[$_POST, $_FILES] = request_parse_body([
'max_input_vars' => 1,
]);
} catch (Throwable $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}

var_dump($_POST, $_FILES);

?>
--EXPECT--
RequestParseBodyException: Input variables exceeded 1. To increase the limit change max_input_vars in php.ini.
array(0) {
}
array(0) {
}
Loading