Skip to content

Commit 25b1841

Browse files
committed
Implement $options parameter
1 parent 4281ac8 commit 25b1841

16 files changed

+395
-40
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
request_parse_body() invalid key
3+
--FILE--
4+
<?php
5+
6+
try {
7+
request_parse_body(options: [
8+
'foo' => 1,
9+
]);
10+
} catch (Error $e) {
11+
echo $e->getMessage(), "\n";
12+
}
13+
14+
?>
15+
--EXPECT--
16+
Invalid key "foo" in $options argument
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
request_parse_body() invalid value type
3+
--FILE--
4+
<?php
5+
6+
try {
7+
request_parse_body(options: [
8+
'max_input_vars' => [],
9+
]);
10+
} catch (Error $e) {
11+
echo $e->getMessage(), "\n";
12+
}
13+
14+
?>
15+
--EXPECT--
16+
Invalid array value in $options argument
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
request_parse_body() max_file_uploads option
3+
--INI--
4+
max_file_uploads=10
5+
--FILE--
6+
<?php
7+
8+
$one_kb_data = str_repeat('a', 1024);
9+
10+
$stream = fopen('php://memory','r+');
11+
fwrite($stream, <<<BODY
12+
-----------------------------84000087610663814162942123332
13+
Content-Disposition: form-data; name="file1"; filename="file1.txt"
14+
Content-Type: text/plain
15+
16+
file data
17+
-----------------------------84000087610663814162942123332
18+
Content-Disposition: form-data; name="file2"; filename="file2.txt"
19+
Content-Type: text/plain
20+
21+
file data
22+
-----------------------------84000087610663814162942123332--
23+
BODY);
24+
rewind($stream);
25+
26+
try {
27+
[$_POST, $_FILES] = request_parse_body($stream, 'multipart/form-data; boundary=---------------------------84000087610663814162942123332', [
28+
'max_file_uploads' => 1,
29+
]);
30+
} catch (Exception $e) {
31+
echo $e->getMessage(), "\n";
32+
}
33+
34+
var_dump($_POST, $_FILES);
35+
36+
?>
37+
--EXPECT--
38+
Maximum number of allowable file uploads has been exceeded
39+
array(0) {
40+
}
41+
array(0) {
42+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
request_parse_body() max_input_vars option
3+
--INI--
4+
max_input_vars=10
5+
--FILE--
6+
<?php
7+
8+
$one_kb_data = str_repeat('a', 1024);
9+
10+
$stream = fopen('php://memory','r+');
11+
fwrite($stream, <<<BODY
12+
-----------------------------84000087610663814162942123332
13+
Content-Disposition: form-data; name="field1"
14+
15+
post field data
16+
-----------------------------84000087610663814162942123332
17+
Content-Disposition: form-data; name="field2"
18+
19+
post field data
20+
-----------------------------84000087610663814162942123332--
21+
BODY);
22+
rewind($stream);
23+
24+
try {
25+
[$_POST, $_FILES] = request_parse_body($stream, 'multipart/form-data; boundary=---------------------------84000087610663814162942123332', [
26+
'max_input_vars' => 1,
27+
]);
28+
} catch (Exception $e) {
29+
echo $e->getMessage(), "\n";
30+
}
31+
32+
var_dump($_POST, $_FILES);
33+
34+
?>
35+
--EXPECT--
36+
Input variables exceeded 1. To increase the limit change max_input_vars in php.ini.
37+
array(0) {
38+
}
39+
array(0) {
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
request_parse_body() max_multipart_body_parts option
3+
--INI--
4+
max_multipart_body_parts=10
5+
--FILE--
6+
<?php
7+
8+
$one_kb_data = str_repeat('a', 1024);
9+
10+
$stream = fopen('php://memory','r+');
11+
fwrite($stream, <<<BODY
12+
-----------------------------84000087610663814162942123332
13+
Content-Disposition: form-data; name="post_field_name"
14+
15+
post field data
16+
-----------------------------84000087610663814162942123332
17+
Content-Disposition: form-data; name="file_name"; filename="original_file_name.txt"
18+
Content-Type: text/plain
19+
20+
file data
21+
-----------------------------84000087610663814162942123332--
22+
BODY);
23+
rewind($stream);
24+
25+
try {
26+
[$_POST, $_FILES] = request_parse_body($stream, 'multipart/form-data; boundary=---------------------------84000087610663814162942123332', [
27+
'max_multipart_body_parts' => 1,
28+
]);
29+
} catch (Exception $e) {
30+
echo $e->getMessage(), "\n";
31+
}
32+
33+
var_dump($_POST, $_FILES);
34+
35+
?>
36+
--EXPECT--
37+
Multipart body parts limit exceeded 1. To increase the limit change max_multipart_body_parts in php.ini.
38+
array(0) {
39+
}
40+
array(0) {
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
request_parse_body() post_max_size option
3+
--INI--
4+
post_max_size=1M
5+
--FILE--
6+
<?php
7+
8+
$one_kb_data = str_repeat('a', 1024);
9+
10+
$stream = fopen('php://memory','r+');
11+
fwrite($stream, <<<BODY
12+
-----------------------------84000087610663814162942123332
13+
Content-Disposition: form-data; name="field1"
14+
15+
post field data
16+
-----------------------------84000087610663814162942123332
17+
Content-Disposition: form-data; name="field2"
18+
19+
$one_kb_data
20+
-----------------------------84000087610663814162942123332--
21+
BODY);
22+
rewind($stream);
23+
24+
try {
25+
[$_POST, $_FILES] = request_parse_body($stream, 'multipart/form-data; boundary=---------------------------84000087610663814162942123332', [
26+
'post_max_size' => '1K',
27+
]);
28+
} catch (Exception $e) {
29+
echo $e->getMessage(), "\n";
30+
}
31+
32+
var_dump($_POST, $_FILES);
33+
34+
?>
35+
--EXPECT--
36+
POST Content-Length of 0 bytes exceeds the limit of 1024 bytes
37+
array(0) {
38+
}
39+
array(0) {
40+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
request_parse_body() upload_max_filesize option
3+
--INI--
4+
upload_max_filesize=1M
5+
--FILE--
6+
<?php
7+
8+
$one_kb_data = str_repeat('a', 1025);
9+
10+
$stream = fopen('php://memory','r+');
11+
fwrite($stream, <<<BODY
12+
-----------------------------84000087610663814162942123332
13+
Content-Disposition: name="file1"; filename="file1.txt"
14+
15+
$one_kb_data
16+
-----------------------------84000087610663814162942123332--
17+
BODY);
18+
rewind($stream);
19+
20+
try {
21+
[$_POST, $_FILES] = request_parse_body($stream, 'multipart/form-data; boundary=---------------------------84000087610663814162942123332', [
22+
'upload_max_filesize' => '1K',
23+
]);
24+
} catch (Exception $e) {
25+
echo $e->getMessage(), "\n";
26+
}
27+
28+
var_dump($_POST, $_FILES);
29+
30+
?>
31+
--EXPECT--
32+
array(0) {
33+
}
34+
array(1) {
35+
["file1"]=>
36+
array(6) {
37+
["name"]=>
38+
string(9) "file1.txt"
39+
["full_path"]=>
40+
string(9) "file1.txt"
41+
["type"]=>
42+
string(0) ""
43+
["tmp_name"]=>
44+
string(0) ""
45+
["error"]=>
46+
int(1)
47+
["size"]=>
48+
int(0)
49+
}
50+
}

ext/mbstring/mb_gpc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,9 @@ const mbfl_encoding *_php_mb_encoding_handler_ex(const php_mb_encoding_handler_i
221221
var = php_strtok_r(NULL, info->separator, &strtok_buf);
222222
}
223223

224-
if (ZEND_SIZE_T_GT_ZEND_LONG(n, (PG(max_input_vars) * 2))) {
225-
php_error_docref(NULL, E_WARNING, "Input variables exceeded " ZEND_LONG_FMT ". To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));
224+
zend_long max_input_vars = REQUEST_PARSE_BODY_OPTION_GET(max_input_vars, PG(max_input_vars));
225+
if (ZEND_SIZE_T_GT_ZEND_LONG(n, max_input_vars * 2)) {
226+
php_error_docref(NULL, E_WARNING, "Input variables exceeded " ZEND_LONG_FMT ". To increase the limit change max_input_vars in php.ini.", max_input_vars);
226227
goto out;
227228
}
228229

ext/standard/basic_functions.stub.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2265,10 +2265,11 @@ function get_html_translation_table(int $table = HTML_SPECIALCHARS, int $flags =
22652265

22662266
/**
22672267
* @param resource|null $input_stream
2268+
* @param array|null $options
22682269
* @return array<int, array>
22692270
* @refcount 1
22702271
*/
2271-
function request_parse_body($input_stream = null, ?string $content_type = null): array {}
2272+
function request_parse_body($input_stream = null, ?string $content_type = null, ?array $options = null): array {}
22722273

22732274
/* }}} */
22742275

ext/standard/basic_functions_arginfo.h

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)