Skip to content

Commit 8581c40

Browse files
committed
Security/PHPFilterFunctions: improve the tests
* Test against false positives for calls to methods or namespaced function calls. * Test against false positives for attribute class using the same name as the function. * Ensure function import `use` statements are not flagged. We're not interested in those. * Document that if the filter is passed in dynamically (via a variable or function call), the sniff will stay silent. * Add some more variations to the pre-existing tests: - Non-lowercase function call(s). - Fully qualified function calls. - Use PHP 7.3+ trailing comma's in a few function calls.
1 parent 052706b commit 8581c40

File tree

2 files changed

+57
-28
lines changed

2 files changed

+57
-28
lines changed
Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,58 @@
11
<?php
22

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;
67

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+
*/
827
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, );
1230
filter_input( INPUT_GET, "foo" , FILTER_SANITIZE_STRING );
13-
filter_var_array( $array, FILTER_SANITIZE_STRING );
1431
filter_input_array( $array, FILTER_SANITIZE_STRING );
15-
filter_input_array( $array,FILTER_SANITIZE_STRING );
1632

17-
// Bad.
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+
*/
1844
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.
45+
\filter_input( INPUT_GET, 'foo', FILTER_DEFAULT ); // This filter ID does nothing.
46+
filter_input( INPUT_GET, "foo", FILTER_UNSAFE_RAW ,); // This filter ID does nothing.
47+
2148
filter_var( $url ); // Missing second parameter.
22-
filter_var( $url, FILTER_DEFAULT ); // This filter ID does nothing.
49+
Filter_Var( $url, FILTER_DEFAULT ); // This filter ID does nothing.
2350
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 second parameter.
2553
filter_var_array( $array, FILTER_DEFAULT ); // This filter ID does nothing.
2654
filter_var_array( $array, FILTER_UNSAFE_RAW ); // This filter ID does nothing.
55+
2756
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.
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.

WordPressVIPMinimum/Tests/Security/PHPFilterFunctionsUnitTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ public function getErrorList() {
3232
*/
3333
public function getWarningList() {
3434
return [
35-
18 => 1,
36-
19 => 1,
37-
20 => 1,
38-
21 => 1,
39-
22 => 1,
40-
23 => 1,
41-
24 => 1,
42-
25 => 1,
43-
26 => 1,
44-
27 => 1,
45-
28 => 1,
46-
29 => 1,
35+
44 => 1,
36+
45 => 1,
37+
46 => 1,
38+
48 => 1,
39+
49 => 1,
40+
50 => 1,
41+
52 => 1,
42+
53 => 1,
43+
54 => 1,
44+
56 => 1,
45+
57 => 1,
46+
58 => 1,
4747
];
4848
}
4949
}

0 commit comments

Comments
 (0)