Skip to content

Commit 4febbf3

Browse files
committed
Security/PHPFilterFunctions: ignore function calls using PHP 5.6+ argument unpacking
This sniff ignores the function call if it cannot reliably be determined if a warning should be thrown. So, for function calls using PHP 5.6+ argument unpacking, I've elected to also ignore these function calls. Includes tests covering the change.
1 parent c65e55c commit 4febbf3

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

WordPressVIPMinimum/Sniffs/Security/PHPFilterFunctionsSniff.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,28 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
7979

8080
$target_param = PassedParameters::getParameterFromStack( $parameters, $param_position, $param_name );
8181
if ( $target_param === false ) {
82+
/*
83+
* Check for PHP 5.6+ argument unpacking.
84+
*
85+
* No need for extensive defensive coding, we already know this is syntactically a valid function call,
86+
* otherwise this method would not have been reached.
87+
*/
88+
$tokens = $this->phpcsFile->getTokens();
89+
$open_parens = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true );
90+
$has_ellipses = $this->phpcsFile->findNext( T_ELLIPSIS, ( $open_parens + 1 ), $tokens[ $open_parens ]['parenthesis_closer'] );
91+
92+
if ( $has_ellipses !== false ) {
93+
$target_nesting_level = 1;
94+
if ( isset( $tokens[ $open_parens ]['nested_parenthesis'] ) ) {
95+
$target_nesting_level = ( count( $tokens[ $open_parens ]['nested_parenthesis'] ) + 1 );
96+
}
97+
98+
if ( $target_nesting_level === count( $tokens[ $has_ellipses ]['nested_parenthesis'] ) ) {
99+
// Bow out as undetermined.
100+
return;
101+
}
102+
}
103+
82104
$message = 'Missing $%s parameter for "%s()".';
83105
$data = [ $param_name, $matched_content ];
84106

WordPressVIPMinimum/Tests/Security/PHPFilterFunctionsUnitTest.inc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,9 @@ filter_var(value: $value, filters: FILTER_SANITIZE_URL); // Typo in parameter na
7373
filter_var_array(options: FILTER_UNSAFE_RAW, array: $array); // This filter ID does nothing.
7474

7575
filter_input_array($type, add_empty: false, options: FILTER_DEFAULT,); // This filter ID does nothing.
76+
77+
// Ignore function calls using PHP 5.6 argument unpacking as we don't know what parameters were passed.
78+
filter_input(INPUT_GET, ...$params);
79+
trim(filter_input(INPUT_GET, ...$params));
80+
// ... but only ignore unpacking if done at the correct nesting level.
81+
filter_input(INPUT_GET, $obj->getVarname(...$params)); // Missing $filter parameter.

WordPressVIPMinimum/Tests/Security/PHPFilterFunctionsUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function getWarningList() {
4949
71 => 1,
5050
73 => 1,
5151
75 => 1,
52+
81 => 1,
5253
];
5354
}
5455
}

0 commit comments

Comments
 (0)