Skip to content

Commit 3894437

Browse files
committed
ValidatedSanitizedInput: allow for validation using key_exists()
This builds onto the previous enhancement made in 1635 which started recognizing `array_key_exists()` as a way to validate a variable. `key_exists()` is an alias for `array_key_exists()` and while alias functions shouldn't be used, for the purposes of the ValidatedSanitizedInput sniff, both functions should be recognized. Includes unit test. Notes: * Removes the `array_key_exists()` method from the list of `$sanitizingFunctions` as it doesn't belong there and is now handled differently anyway (this should have been removed in 1635). * Updates the version numbers for the change in the method documentation. We never released version 2.0.1, so both this change as well as the one from 1635 will now be released in 2.1.0.
1 parent 2a0f154 commit 3894437

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

WordPress/Sniff.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ abstract class Sniff implements PHPCS_Sniff {
252252
*/
253253
protected $sanitizingFunctions = array(
254254
'_wp_handle_upload' => true,
255-
'array_key_exists' => true,
256255
'esc_url_raw' => true,
257256
'filter_input' => true,
258257
'filter_var' => true,
@@ -1437,8 +1436,8 @@ protected function has_nonce_check( $stackPtr ) {
14371436
* Check if a token is inside of an isset(), empty() or array_key_exists() statement.
14381437
*
14391438
* @since 0.5.0
1440-
* @since 2.0.1 Now checks for the token being used as the array parameter
1441-
* in function calls to array_key_exists() as well.
1439+
* @since 2.1.0 Now checks for the token being used as the array parameter
1440+
* in function calls to array_key_exists() and key_exists() as well.
14421441
*
14431442
* @param int $stackPtr The index of the token in the stack.
14441443
*
@@ -1465,7 +1464,12 @@ protected function is_in_isset_or_empty( $stackPtr ) {
14651464
return true;
14661465
}
14671466

1468-
$functionPtr = $this->is_in_function_call( $stackPtr, array( 'array_key_exists' => true ) );
1467+
$valid_functions = array(
1468+
'array_key_exists' => true,
1469+
'key_exists' => true, // Alias.
1470+
);
1471+
1472+
$functionPtr = $this->is_in_function_call( $stackPtr, $valid_functions );
14691473
if ( false !== $functionPtr ) {
14701474
$second_param = $this->get_function_call_parameter( $functionPtr, 2 );
14711475
if ( $stackPtr >= $second_param['start'] && $stackPtr <= $second_param['end'] ) {
@@ -1845,7 +1849,8 @@ protected function get_array_access_key( $stackPtr ) {
18451849
}
18461850

18471851
/**
1848-
* Check if the existence of a variable is validated with isset(), empty() or array_key_exists().
1852+
* Check if the existence of a variable is validated with isset(), empty(), array_key_exists()
1853+
* or key_exists().
18491854
*
18501855
* When $in_condition_only is false, (which is the default), this is considered
18511856
* valid:
@@ -1868,7 +1873,7 @@ protected function get_array_access_key( $stackPtr ) {
18681873
* ```
18691874
*
18701875
* @since 0.5.0
1871-
* @since 2.0.1 Now recognizes array_key_exists() as a validation function.
1876+
* @since 2.1.0 Now recognizes array_key_exists() and key_exists() as validation functions.
18721877
*
18731878
* @param int $stackPtr The index of this token in the stack.
18741879
* @param string $array_key An array key to check for ("bar" in $foo['bar']).
@@ -1982,8 +1987,10 @@ protected function is_validated( $stackPtr, $array_key = null, $in_condition_onl
19821987
break;
19831988

19841989
case 'function_call':
1985-
// Only check calls to array_key_exists().
1986-
if ( 'array_key_exists' !== $this->tokens[ $i ]['content'] ) {
1990+
// Only check calls to array_key_exists() and key_exists().
1991+
if ( 'array_key_exists' !== $this->tokens[ $i ]['content']
1992+
&& 'key_exists' !== $this->tokens[ $i ]['content']
1993+
) {
19871994
continue 2;
19881995
}
19891996

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,8 @@ function test_more_safe_functions() {
225225
$float = doubleval( $_GET['test'] ); // OK.
226226
$count = count( $_GET['test'] ); // Issue #1659; OK.
227227
}
228+
229+
function test_allow_array_key_exists_alias() {
230+
if ( key_exists( 'my_field1', $_POST ) ) {
231+
$id = (int) $_POST['my_field1']; // OK.
232+
}

0 commit comments

Comments
 (0)