|
12 | 12 | use PHP_CodeSniffer\Util\Tokens; |
13 | 13 | use PHPCSUtils\Utils\PassedParameters; |
14 | 14 | use WordPressCS\WordPress\AbstractFunctionParameterSniff; |
| 15 | +use WordPressCS\WordPress\Helpers\RulesetPropertyHelper; |
15 | 16 |
|
16 | 17 | /** |
17 | 18 | * This sniff ensures that proper sanitization is occurring when PHP's filter_* functions are used. |
@@ -65,6 +66,28 @@ class PHPFilterFunctionsSniff extends AbstractFunctionParameterSniff { |
65 | 66 | 'FILTER_UNSAFE_RAW' => true, |
66 | 67 | ]; |
67 | 68 |
|
| 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 | + |
68 | 91 | /** |
69 | 92 | * Process the parameters of a matched function. |
70 | 93 | * |
@@ -118,7 +141,13 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p |
118 | 141 | return; |
119 | 142 | } |
120 | 143 |
|
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'] ] ) ) { |
122 | 151 | $first_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, $target_param['start'], ( $target_param['end'] + 1 ), true ); |
123 | 152 |
|
124 | 153 | $message = 'Please use an appropriate filter to sanitize, as "%s" does no filtering, see: http://php.net/manual/en/filter.filters.sanitize.php.'; |
|
0 commit comments