Skip to content

Commit 282d3f2

Browse files
committed
Improved max_input_vars directive to check nested variables
1 parent 8f7381a commit 282d3f2

File tree

3 files changed

+35
-33
lines changed

3 files changed

+35
-33
lines changed

NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2012, PHP 5.3.11
4+
- Core:
5+
. Improved max_input_vars directive to check nested variables (Dmitry).
6+
47
- Session:
58
. Fixed bug #60860 (session.save_handler=user without defined function core
69
dumps). (Felipe)

main/php_variables.c

+15-23
Original file line numberDiff line numberDiff line change
@@ -196,21 +196,9 @@ PHPAPI void php_register_variable_ex(char *var_name, zval *val, zval *track_vars
196196
}
197197
if (zend_symtable_find(symtable1, escaped_index, index_len + 1, (void **) &gpc_element_p) == FAILURE
198198
|| Z_TYPE_PP(gpc_element_p) != IS_ARRAY) {
199-
if (zend_hash_num_elements(symtable1) <= PG(max_input_vars)) {
200-
if (zend_hash_num_elements(symtable1) == PG(max_input_vars)) {
201-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input variables exceeded %ld. To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));
202-
}
203-
MAKE_STD_ZVAL(gpc_element);
204-
array_init(gpc_element);
205-
zend_symtable_update(symtable1, escaped_index, index_len + 1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
206-
} else {
207-
if (index != escaped_index) {
208-
efree(escaped_index);
209-
}
210-
zval_dtor(val);
211-
efree(var_orig);
212-
return;
213-
}
199+
MAKE_STD_ZVAL(gpc_element);
200+
array_init(gpc_element);
201+
zend_symtable_update(symtable1, escaped_index, index_len + 1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
214202
}
215203
if (index != escaped_index) {
216204
efree(escaped_index);
@@ -255,14 +243,7 @@ PHPAPI void php_register_variable_ex(char *var_name, zval *val, zval *track_vars
255243
zend_symtable_exists(symtable1, escaped_index, index_len + 1)) {
256244
zval_ptr_dtor(&gpc_element);
257245
} else {
258-
if (zend_hash_num_elements(symtable1) <= PG(max_input_vars)) {
259-
if (zend_hash_num_elements(symtable1) == PG(max_input_vars)) {
260-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input variables exceeded %ld. To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));
261-
}
262-
zend_symtable_update(symtable1, escaped_index, index_len + 1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
263-
} else {
264-
zval_ptr_dtor(&gpc_element);
265-
}
246+
zend_symtable_update(symtable1, escaped_index, index_len + 1, &gpc_element, sizeof(zval *), (void **) &gpc_element_p);
266247
}
267248
if (escaped_index != index) {
268249
efree(escaped_index);
@@ -276,6 +257,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(php_std_post_handler)
276257
{
277258
char *var, *val, *e, *s, *p;
278259
zval *array_ptr = (zval *) arg;
260+
long count = 0;
279261

280262
if (SG(request_info).post_data == NULL) {
281263
return;
@@ -289,6 +271,10 @@ SAPI_API SAPI_POST_HANDLER_FUNC(php_std_post_handler)
289271
if ((val = memchr(s, '=', (p - s)))) { /* have a value */
290272
unsigned int val_len, new_val_len;
291273

274+
if (++count > PG(max_input_vars)) {
275+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input variables exceeded %ld. To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));
276+
return;
277+
}
292278
var = s;
293279

294280
php_url_decode(var, (val - s));
@@ -322,6 +308,7 @@ SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data)
322308
zval *array_ptr;
323309
int free_buffer = 0;
324310
char *strtok_buf = NULL;
311+
long count = 0;
325312

326313
switch (arg) {
327314
case PARSE_POST:
@@ -411,6 +398,11 @@ SAPI_API SAPI_TREAT_DATA_FUNC(php_default_treat_data)
411398
}
412399
}
413400

401+
if (++count > PG(max_input_vars)) {
402+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input variables exceeded %ld. To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));
403+
break;
404+
}
405+
414406
if (val) { /* have a value */
415407
int val_len;
416408
unsigned int new_val_len;

main/rfc1867.c

+17-10
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
779779
void *event_extra_data = NULL;
780780
int llen = 0;
781781
int upload_cnt = INI_INT("max_file_uploads");
782+
long count = 0;
782783

783784
if (SG(post_max_size) > 0 && SG(request_info).content_length > SG(post_max_size)) {
784785
sapi_module.sapi_error(E_WARNING, "POST Content-Length of %ld bytes exceeds the limit of %ld bytes", SG(request_info).content_length, SG(post_max_size));
@@ -918,7 +919,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
918919
value = estrdup("");
919920
}
920921

921-
if (sapi_module.input_filter(PARSE_POST, param, &value, value_len, &new_val_len TSRMLS_CC)) {
922+
if (++count <= PG(max_input_vars) && sapi_module.input_filter(PARSE_POST, param, &value, value_len, &new_val_len TSRMLS_CC)) {
922923
if (php_rfc1867_callback != NULL) {
923924
multipart_event_formdata event_formdata;
924925
size_t newlength = new_val_len;
@@ -945,15 +946,21 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
945946
#else
946947
safe_php_register_variable(param, value, new_val_len, array_ptr, 0 TSRMLS_CC);
947948
#endif
948-
} else if (php_rfc1867_callback != NULL) {
949-
multipart_event_formdata event_formdata;
950-
951-
event_formdata.post_bytes_processed = SG(read_post_bytes);
952-
event_formdata.name = param;
953-
event_formdata.value = &value;
954-
event_formdata.length = value_len;
955-
event_formdata.newlength = NULL;
956-
php_rfc1867_callback(MULTIPART_EVENT_FORMDATA, &event_formdata, &event_extra_data TSRMLS_CC);
949+
} else {
950+
if (count == PG(max_input_vars) + 1) {
951+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input variables exceeded %ld. To increase the limit change max_input_vars in php.ini.", PG(max_input_vars));
952+
}
953+
954+
if (php_rfc1867_callback != NULL) {
955+
multipart_event_formdata event_formdata;
956+
957+
event_formdata.post_bytes_processed = SG(read_post_bytes);
958+
event_formdata.name = param;
959+
event_formdata.value = &value;
960+
event_formdata.length = value_len;
961+
event_formdata.newlength = NULL;
962+
php_rfc1867_callback(MULTIPART_EVENT_FORMDATA, &event_formdata, &event_extra_data TSRMLS_CC);
963+
}
957964
}
958965

959966
if (!strcasecmp(param, "MAX_FILE_SIZE")) {

0 commit comments

Comments
 (0)