Skip to content

Commit 2475dd7

Browse files
authored
Merge pull request #897 from Automattic/GaryJones/753-configurable-restricted-filters
2 parents 3831f13 + 5c4991d commit 2475dd7

3 files changed

Lines changed: 60 additions & 19 deletions

File tree

WordPressVIPMinimum/Sniffs/Security/PHPFilterFunctionsSniff.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHP_CodeSniffer\Util\Tokens;
1313
use PHPCSUtils\Utils\PassedParameters;
1414
use WordPressCS\WordPress\AbstractFunctionParameterSniff;
15+
use WordPressCS\WordPress\Helpers\RulesetPropertyHelper;
1516

1617
/**
1718
* This sniff ensures that proper sanitization is occurring when PHP's filter_* functions are used.
@@ -65,6 +66,28 @@ class PHPFilterFunctionsSniff extends AbstractFunctionParameterSniff {
6566
'FILTER_UNSAFE_RAW' => true,
6667
];
6768

69+
/**
70+
* Filter names to exclude from the list of restricted filters.
71+
*
72+
* This allows a developer who knowingly uses a non-sanitizing filter (for
73+
* example, when they sanitize the value themselves afterwards) to prevent
74+
* the sniff from flagging it, without having to redeclare the full list.
75+
*
76+
* Set this from a custom ruleset, for example to allow `FILTER_UNSAFE_RAW`:
77+
* <code>
78+
* <rule ref="WordPressVIPMinimum.Security.PHPFilterFunctions">
79+
* <properties>
80+
* <property name="exclude_filters" type="array">
81+
* <element value="FILTER_UNSAFE_RAW"/>
82+
* </property>
83+
* </properties>
84+
* </rule>
85+
* </code>
86+
*
87+
* @var array<string>
88+
*/
89+
public $exclude_filters = [];
90+
6891
/**
6992
* Process the parameters of a matched function.
7093
*
@@ -118,7 +141,13 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
118141
return;
119142
}
120143

121-
if ( isset( $this->restricted_filters[ $target_param['clean'] ] ) ) {
144+
// Recalculated on each call so an inline change to the `exclude_filters` property is respected.
145+
$restricted_filters = array_diff_key(
146+
$this->restricted_filters,
147+
RulesetPropertyHelper::merge_custom_array( $this->exclude_filters )
148+
);
149+
150+
if ( isset( $restricted_filters[ $target_param['clean'] ] ) ) {
122151
$first_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, $target_param['start'], ( $target_param['end'] + 1 ), true );
123152

124153
$message = 'Please use an appropriate filter to sanitize, as "%s" does no filtering, see: http://php.net/manual/en/filter.filters.sanitize.php.';

WordPressVIPMinimum/Tests/Security/PHPFilterFunctionsUnitTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,13 @@ filter_input_array(
9898
],
9999
]
100100
);
101+
102+
/*
103+
* Bug #753: the list of restricted filters can be reduced via the `exclude_filters` property.
104+
*/
105+
$excludable = filter_input( INPUT_POST, 'a', FILTER_UNSAFE_RAW ); // Warning RestrictedFilter.
106+
107+
// phpcs:set WordPressVIPMinimum.Security.PHPFilterFunctions exclude_filters[] FILTER_UNSAFE_RAW
108+
$excluded = filter_input( INPUT_POST, 'b', FILTER_UNSAFE_RAW ); // OK - excluded via the property.
109+
$still_restricted = filter_var( $value, FILTER_DEFAULT ); // Warning RestrictedFilter - other defaults still apply.
110+
// phpcs:set WordPressVIPMinimum.Security.PHPFilterFunctions exclude_filters[]

WordPressVIPMinimum/Tests/Security/PHPFilterFunctionsUnitTest.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,26 @@ public function getErrorList() {
3232
*/
3333
public function getWarningList() {
3434
return [
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,
47-
65 => 1,
48-
70 => 1,
49-
71 => 1,
50-
73 => 1,
51-
75 => 1,
52-
81 => 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,
47+
65 => 1,
48+
70 => 1,
49+
71 => 1,
50+
73 => 1,
51+
75 => 1,
52+
81 => 1,
53+
105 => 1,
54+
109 => 1,
5355
];
5456
}
5557
}

0 commit comments

Comments
 (0)