Skip to content

Commit 14c77a8

Browse files
authored
Merge pull request #1679 from WordPress-Coding-Standards/feature/1660-1618-validatedsanitizedinput-recognize-more-array-walking-functions
Sniff::is_sanitized(): allow for map_deep() to sanitize arrays
2 parents af937cc + 7841dab commit 14c77a8

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

WordPress/Sniff.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,25 @@ abstract class Sniff implements PHPCS_Sniff {
361361
\T_BOOL_CAST => true,
362362
);
363363

364+
/**
365+
* List of array functions which apply a callback to the array.
366+
*
367+
* These are often used for sanitization/escaping an array variable.
368+
*
369+
* Note: functions which alter the array by reference are not listed here on purpose.
370+
* These cannot easily be used for sanitization as they can't be combined with unslashing.
371+
* Similarly, they cannot be used for late escaping as the return value is a boolean, not
372+
* the altered array.
373+
*
374+
* @since 2.1.0
375+
*
376+
* @var array <string function name> => <int parameter position of the callback parameter>
377+
*/
378+
protected $arrayWalkingFunctions = array(
379+
'array_map' => 1,
380+
'map_deep' => 2,
381+
);
382+
364383
/**
365384
* Functions that format strings.
366385
*
@@ -1789,8 +1808,8 @@ protected function is_sanitized( $stackPtr, $require_unslash = false ) {
17891808

17901809
$valid_functions = $this->sanitizingFunctions;
17911810
$valid_functions += $this->unslashingSanitizingFunctions;
1811+
$valid_functions += $this->arrayWalkingFunctions;
17921812
$valid_functions['wp_unslash'] = true;
1793-
$valid_functions['array_map'] = true;
17941813

17951814
$functionPtr = $this->is_in_function_call( $stackPtr, $valid_functions );
17961815

@@ -1825,11 +1844,11 @@ protected function is_sanitized( $stackPtr, $require_unslash = false ) {
18251844
$is_unslashed = false;
18261845
}
18271846

1828-
// Arrays might be sanitized via array_map().
1829-
if ( 'array_map' === $functionName ) {
1847+
// Arrays might be sanitized via an array walking function using a callback.
1848+
if ( isset( $this->arrayWalkingFunctions[ $functionName ] ) ) {
18301849

1831-
// Get the first parameter.
1832-
$callback = $this->get_function_call_parameter( $functionPtr, 1 );
1850+
// Get the callback parameter.
1851+
$callback = $this->get_function_call_parameter( $functionPtr, $this->arrayWalkingFunctions[ $functionName ] );
18331852

18341853
if ( ! empty( $callback ) ) {
18351854
/*

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,12 @@ function test_ignoring_is_type_function_calls() {
267267
if ( array_key_exists( 'null', $_GET ) && ! is_null( $_GET['null'] ) ) {} // OK.
268268
if ( array_key_exists( 'null', $_POST ) && $_POST['null'] !== null ) {} // OK.
269269
}
270+
271+
function test_additional_array_walking_functions() {
272+
if ( ! isset( $_GET['test'] ) ) {
273+
return;
274+
}
275+
276+
$sane = map_deep( wp_unslash( $_GET['test'] ), 'sanitize_text_field' ); // Ok.
277+
$sane = map_deep( wp_unslash( $_GET['test'] ), 'foo' ); // Bad.
278+
}

WordPress/Tests/Security/ValidatedSanitizedInputUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function getErrorList() {
6666
251 => 1,
6767
257 => 1,
6868
266 => 1,
69+
277 => 1,
6970
);
7071
}
7172

0 commit comments

Comments
 (0)