Skip to content

Commit f10e45f

Browse files
committed
Implement promotion to exceptions
1 parent 3eb4519 commit f10e45f

12 files changed

+289
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
request_parse_body() with multipart and invalid boundary
3+
--FILE--
4+
<?php
5+
6+
$stream = fopen('php://memory','r+');
7+
8+
try {
9+
[$_POST, $_FILES] = request_parse_body($stream, 'multipart/form-data; boundary="foobar');
10+
} catch (Exception $e) {
11+
echo $e->getMessage(), "\n";
12+
}
13+
14+
var_dump($_POST, $_FILES);
15+
16+
?>
17+
--EXPECT--
18+
Invalid boundary in multipart/form-data POST data
19+
array(0) {
20+
}
21+
array(0) {
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
request_parse_body() with multipart and garbled field
3+
--INI--
4+
max_file_uploads=1
5+
--FILE--
6+
<?php
7+
8+
$stream = fopen('php://memory','r+');
9+
fwrite($stream, <<<BODY
10+
-----------------------------84000087610663814162942123332
11+
Content-Disposition: form-data;
12+
Content-Type: text/plain
13+
14+
post field data
15+
-----------------------------84000087610663814162942123332--
16+
BODY);
17+
rewind($stream);
18+
19+
try {
20+
[$_POST, $_FILES] = request_parse_body($stream, 'multipart/form-data; boundary=---------------------------84000087610663814162942123332');
21+
} catch (Exception $e) {
22+
echo $e->getMessage(), "\n";
23+
}
24+
25+
var_dump($_POST, $_FILES);
26+
27+
?>
28+
--EXPECT--
29+
File Upload Mime headers garbled
30+
array(0) {
31+
}
32+
array(0) {
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
request_parse_body() with multipart and exceeding max files
3+
--INI--
4+
max_file_uploads=1
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+
} catch (Exception $e) {
29+
echo $e->getMessage(), "\n";
30+
}
31+
32+
var_dump($_POST, $_FILES);
33+
34+
?>
35+
--EXPECT--
36+
Maximum number of allowable file uploads has been exceeded
37+
array(0) {
38+
}
39+
array(0) {
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
request_parse_body() with multipart and exceeding max input vars
3+
--INI--
4+
max_input_vars=1
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+
} catch (Exception $e) {
27+
echo $e->getMessage(), "\n";
28+
}
29+
30+
var_dump($_POST, $_FILES);
31+
32+
?>
33+
--EXPECT--
34+
Input variables exceeded 1. To increase the limit change max_input_vars in php.ini.
35+
array(0) {
36+
}
37+
array(0) {
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
--TEST--
2+
request_parse_body() with multipart and exceeding max parts
3+
--INI--
4+
max_multipart_body_parts=1
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+
} catch (Exception $e) {
28+
echo $e->getMessage(), "\n";
29+
}
30+
31+
var_dump($_POST, $_FILES);
32+
33+
?>
34+
--EXPECT--
35+
Multipart body parts limit exceeded 1. To increase the limit change max_multipart_body_parts in php.ini.
36+
array(0) {
37+
}
38+
array(0) {
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
request_parse_body() with multipart and missing boundary
3+
--FILE--
4+
<?php
5+
6+
$one_kb_data = str_repeat('a', 1024);
7+
8+
$stream = fopen('php://memory','r+');
9+
10+
try {
11+
[$_POST, $_FILES] = request_parse_body($stream, 'multipart/form-data');
12+
} catch (Exception $e) {
13+
echo $e->getMessage(), "\n";
14+
}
15+
16+
var_dump($_POST, $_FILES);
17+
18+
?>
19+
--EXPECT--
20+
Missing boundary in multipart/form-data POST data
21+
array(0) {
22+
}
23+
array(0) {
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
request_parse_body() with multipart and exceeding post_max_size
3+
--INI--
4+
post_max_size=1K
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+
} catch (Exception $e) {
27+
echo $e->getMessage(), "\n";
28+
}
29+
30+
var_dump($_POST, $_FILES);
31+
32+
?>
33+
--EXPECT--
34+
POST Content-Length of 0 bytes exceeds the limit of 1024 bytes
35+
array(0) {
36+
}
37+
array(0) {
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
request_parse_body() with multipart and unsupported Content-Type
3+
--INI--
4+
max_input_vars=1
5+
--FILE--
6+
<?php
7+
8+
$one_kb_data = str_repeat('a', 1024);
9+
10+
$stream = fopen('php://memory','r+');
11+
12+
try {
13+
[$_POST, $_FILES] = request_parse_body($stream, 'application/json');
14+
} catch (Exception $e) {
15+
echo $e->getMessage(), "\n";
16+
}
17+
18+
var_dump($_POST, $_FILES);
19+
20+
?>
21+
--EXPECTF--
22+
Content-Type "application/json" is not supported
23+
array(0) {
24+
}
25+
array(0) {
26+
}

ext/standard/html.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
#include <zend_hash.h>
5252
#include "html_tables.h"
53+
#include "zend_exceptions.h"
5354

5455
/* Macro for disabling flag of translation of non-basic entities where this isn't supported.
5556
* Not appropriate for html_entity_decode/htmlspecialchars_decode */
@@ -1596,10 +1597,15 @@ PHP_FUNCTION(request_parse_body)
15961597
}
15971598
}
15981599

1600+
sapi_read_post_data_ex(content_type_c);
1601+
if (!SG(request_info).post_entry) {
1602+
zend_throw_exception_ex(NULL, 0, "Content-Type \"%s\" is not supported", content_type_c);
1603+
RETURN_THROWS();
1604+
}
1605+
15991606
zval post, files;
16001607
array_init(&post);
16011608
array_init(&files);
1602-
sapi_read_post_data_ex(content_type_c);
16031609
sapi_handle_post_ex(&post, &files);
16041610
RETURN_ARR(zend_new_pair(&post, &files));
16051611
}

0 commit comments

Comments
 (0)