|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -$url = 'http://www.google.ca'; |
4 |
| -$_GET['foo'] = 'bar'; |
5 |
| -$array = [ 'something_something', 'https://www.google.com', '6' ]; |
| 3 | +/* |
| 4 | + * Not the sniff target. |
| 5 | + */ |
| 6 | +use filter_var; |
6 | 7 |
|
7 |
| -// Ok. |
| 8 | +my\ns\filter_input($a, $b); |
| 9 | +$this->filter_var_array($a, $b); |
| 10 | +$this?->filter_input_array($a, $b); |
| 11 | +MyClass::filter_var($a, $b); |
| 12 | +echo FILTER_INPUT; |
| 13 | +namespace\filter_var_array($a, $b); |
| 14 | + |
| 15 | +// Looks like a function call, but is a PHP 8.0+ class instantiation via an attribute. |
| 16 | +#[Filter_Input('text')] |
| 17 | +function foo() {} |
| 18 | + |
| 19 | +// PHP 8.1 first class callable. |
| 20 | +// As we have no knowledge about what parameters will be passed, we shouldn't flag this. |
| 21 | +add_filter('my_filter', filter_var(...)); |
| 22 | + |
| 23 | + |
| 24 | +/* |
| 25 | + * These should all be okay. |
| 26 | + */ |
8 | 27 | filter_var( $url, FILTER_SANITIZE_URL );
|
9 |
| -filter_var( 'test', FILTER_SANITIZE_STRING ); |
10 |
| -filter_var( "test", FILTER_SANITIZE_STRING ); |
11 |
| -filter_input( INPUT_GET, 'foo', FILTER_SANITIZE_STRING ); |
| 28 | +\filter_var( 'test', FILTER_SANITIZE_STRING ); |
| 29 | +FILTER_INPUT( INPUT_GET, 'foo', FILTER_SANITIZE_STRING, ); |
12 | 30 | filter_input( INPUT_GET, "foo" , FILTER_SANITIZE_STRING );
|
13 |
| -filter_var_array( $array, FILTER_SANITIZE_STRING ); |
14 |
| -filter_input_array( $array, FILTER_SANITIZE_STRING ); |
15 |
| -filter_input_array( $array,FILTER_SANITIZE_STRING ); |
16 |
| - |
17 |
| -// Bad. |
18 |
| -filter_input( INPUT_GET, 'foo' ); // Missing third parameter. |
19 |
| -filter_input( INPUT_GET, 'foo', FILTER_DEFAULT ); // This filter ID does nothing. |
20 |
| -filter_input( INPUT_GET, "foo", FILTER_UNSAFE_RAW ); // This filter ID does nothing. |
21 |
| -filter_var( $url ); // Missing second parameter. |
22 |
| -filter_var( $url, FILTER_DEFAULT ); // This filter ID does nothing. |
| 31 | +filter_input_array( $array, filter_default ); // Constants are case-sensitive, so this is not the FILTER_DEFAULT constant. |
| 32 | + |
| 33 | +// Ignore as undetermined. |
| 34 | +filter_var( "test", get_filter() ); |
| 35 | +\Filter_Var_Array( $array, $filterName ); |
| 36 | +filter_input_array( $array,$obj->get_filter() , ); |
| 37 | + |
| 38 | +// Incomplete function call, should be ignored by the sniff. |
| 39 | +$incorrect_but_ok = filter_input(); |
| 40 | + |
| 41 | +/* |
| 42 | + * These should all be flagged with a warning. |
| 43 | + */ |
| 44 | +filter_input( INPUT_GET, 'foo' ); // Missing $filter parameter. |
| 45 | +\filter_input( INPUT_GET, 'foo', FILTER_DEFAULT ); // This filter ID does nothing. |
| 46 | +filter_input( INPUT_GET, "foo", FILTER_UNSAFE_RAW /* comment */ ,); // This filter ID does nothing. |
| 47 | + |
| 48 | +filter_var( $url ); // Missing $filter parameter. |
| 49 | +Filter_Var( $url, FILTER_DEFAULT ); // This filter ID does nothing. |
23 | 50 | filter_var( 'https://google.com', FILTER_UNSAFE_RAW ); // This filter ID does nothing.
|
24 |
| -filter_var_array( $array ); // Missing second parameter. |
| 51 | + |
| 52 | +filter_var_array( $array, ); // Missing $options parameter. |
25 | 53 | filter_var_array( $array, FILTER_DEFAULT ); // This filter ID does nothing.
|
26 | 54 | filter_var_array( $array, FILTER_UNSAFE_RAW ); // This filter ID does nothing.
|
27 |
| -filter_input_array( $array ); // Missing second parameter. |
28 |
| -filter_input_array( $array, FILTER_DEFAULT ); // This filter ID does nothing. |
29 |
| -filter_input_array( $array, FILTER_UNSAFE_RAW ); // This filter ID does nothing. |
| 55 | + |
| 56 | +filter_input_array( $array ); // Missing $options parameter. |
| 57 | +\FILTER_INPUT_ARRAY( $array, FILTER_DEFAULT ); // This filter ID does nothing. |
| 58 | +filter_input_array( $array, FILTER_UNSAFE_RAW, ); // This filter ID does nothing. |
| 59 | + |
| 60 | +// Safeguard handling of function calls using PHP 8.0+ named parameters. |
| 61 | +filter_input(var_name: $var_name, filter: FILTER_SANITIZE_STRING, type: FILTER_DEFAULT); // OK, invalid input value for $type, but that's not our concern. |
| 62 | +filter_input(var_name: $var_name, filter: $filter, type: $type); // Ignore, undetermined. |
| 63 | +filter_input( |
| 64 | + var_name: $var_name, |
| 65 | + filter: FILTER_DEFAULT, // This filter ID does nothing. |
| 66 | + type: $type, |
| 67 | +); |
| 68 | + |
| 69 | +filter_var(filter: FILTER_SANITIZE_URL); // OK, well not really, missing required parameter, but that's not our concern. |
| 70 | +filter_var($value, options: FILTER_NULL_ON_FAILURE); // Missing $filter parameter. |
| 71 | +filter_var(value: $value, filters: FILTER_SANITIZE_URL); // Typo in parameter name, report as missing $filter parameter. |
| 72 | + |
| 73 | +filter_var_array(options: FILTER_UNSAFE_RAW, array: $array); // This filter ID does nothing. |
| 74 | + |
| 75 | +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. |
| 82 | + |
| 83 | +// False negatives: $options arrays are currently not (yet) supported by this sniff. |
| 84 | +// See: https://www.php.net/manual/en/function.filter-var-array.php and https://www.php.net/manual/en/function.filter-input-array.php |
| 85 | +filter_var_array( |
| 86 | + $array, |
| 87 | + array('keyA' => FILTER_DEFAULT), // This filter ID does nothing. |
| 88 | +); |
| 89 | +filter_input_array( |
| 90 | + $array, |
| 91 | + [ |
| 92 | + 'keyA' => [ |
| 93 | + 'filter' => FILTER_UNSAFE_RAW, // This filter ID does nothing. |
| 94 | + 'flags' => FILTER_FORCE_ARRAY, |
| 95 | + ], |
| 96 | + 'keyB' => [ |
| 97 | + 'filter' => FILTER_SANITIZE_ENCODED, |
| 98 | + ], |
| 99 | + ] |
| 100 | +); |
0 commit comments