Skip to content

Commit 1b0bf12

Browse files
author
Sjoerd Langkemper
committed
Throw ValueError instead of generic exception
Remove old counting code
1 parent d3a14c8 commit 1b0bf12

2 files changed

Lines changed: 43 additions & 12 deletions

File tree

ext/standard/php_fopen_wrapper.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,27 +145,20 @@ static const php_stream_ops php_stream_input_ops = {
145145
NULL /* set_option */
146146
};
147147

148-
static const char max_stream_filters = 5;
148+
static const char max_stream_filters = 16;
149149

150150
static void php_stream_apply_filter_list(php_stream *stream, char *filterlist, int read_chain, int write_chain) /* {{{ */
151151
{
152152
char *p, *token = NULL;
153153
php_stream_filter *temp_filter;
154-
char nb_filters = 0;
155154

156155
p = php_strtok_r(filterlist, "|", &token);
157156
while (p) {
158-
if (nb_filters >= max_stream_filters) {
159-
zend_throw_exception_ex(NULL, 0, "Unable to apply filter, maximum number (%d) reached", max_stream_filters);
160-
return;
161-
}
162-
nb_filters++;
163-
164157
php_url_decode(p, strlen(p));
165158
if (read_chain) {
166159
if ((temp_filter = php_stream_filter_create(p, NULL, php_stream_is_persistent(stream)))) {
167-
if (php_stream_filter_count(&stream->readfilters) > max_stream_filters) {
168-
zend_throw_exception_ex(NULL, 0, "Unable to apply read filter, maximum number (%d) reached", max_stream_filters);
160+
if (php_stream_filter_count(&stream->readfilters) >= max_stream_filters) {
161+
zend_value_error("Unable to apply read filter, maximum number (%d) reached", max_stream_filters);
169162
return;
170163
}
171164
php_stream_filter_append(&stream->readfilters, temp_filter);
@@ -175,8 +168,8 @@ static void php_stream_apply_filter_list(php_stream *stream, char *filterlist, i
175168
}
176169
if (write_chain) {
177170
if ((temp_filter = php_stream_filter_create(p, NULL, php_stream_is_persistent(stream)))) {
178-
if (php_stream_filter_count(&stream->writefilters) > max_stream_filters) {
179-
zend_throw_exception_ex(NULL, 0, "Unable to apply write filter, maximum number (%d) reached", max_stream_filters);
171+
if (php_stream_filter_count(&stream->writefilters) >= max_stream_filters) {
172+
zend_value_error("Unable to apply write filter, maximum number (%d) reached", max_stream_filters);
180173
return;
181174
}
182175
php_stream_filter_append(&stream->writefilters, temp_filter);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
At most 16 filters can be chained in one stream
3+
--EXTENSIONS--
4+
filter
5+
--FILE--
6+
<?php
7+
8+
function createFilterChains($n) {
9+
$filter = 'string.toupper';
10+
$resource = 'data:text/plain,hello';
11+
$pipes = 'php://filter/' . implode('|', array_fill(0, $n, $filter)) . "/resource=$resource";
12+
$slashes = 'php://filter/' . implode('/', array_fill(0, $n, $filter)) . "/resource=$resource";
13+
$resources = str_repeat("php://filter/$filter/resource=", $n) . $resource;
14+
return [$pipes, $slashes, $resources];
15+
}
16+
17+
$allowed_chains = createFilterChains(16);
18+
foreach ($allowed_chains as $chain) {
19+
var_dump(file_get_contents($chain));
20+
}
21+
22+
$blocked_chains = createFilterChains(17);
23+
foreach ($blocked_chains as $chain) {
24+
try {
25+
var_dump(file_get_contents($chain));
26+
} catch (ValueError $e) {
27+
echo "ValueError\n";
28+
}
29+
}
30+
31+
?>
32+
--EXPECT--
33+
string(5) "HELLO"
34+
string(5) "HELLO"
35+
string(5) "HELLO"
36+
ValueError
37+
ValueError
38+
ValueError

0 commit comments

Comments
 (0)