|
10 | 10 | namespace WordPressVIPMinimum\Sniffs\Security; |
11 | 11 |
|
12 | 12 | use PHP_CodeSniffer\Util\Tokens; |
| 13 | +use PHPCSUtils\Utils\PassedParameters; |
| 14 | +use WordPressCS\WordPress\AbstractFunctionParameterSniff; |
13 | 15 | use WordPressCS\WordPress\Helpers\PrintingFunctionsTrait; |
14 | | -use WordPressVIPMinimum\Sniffs\Sniff; |
15 | 16 |
|
16 | 17 | /** |
17 | 18 | * Flag functions that don't return anything, yet are wrapped in an escaping function call. |
|
20 | 21 | * |
21 | 22 | * @uses \WordPressCS\WordPress\Helpers\PrintingFunctionsTrait::$customPrintingFunctions |
22 | 23 | */ |
23 | | -class EscapingVoidReturnFunctionsSniff extends Sniff { |
| 24 | +class EscapingVoidReturnFunctionsSniff extends AbstractFunctionParameterSniff { |
24 | 25 |
|
25 | 26 | use PrintingFunctionsTrait; |
26 | 27 |
|
27 | 28 | /** |
28 | | - * Returns an array of tokens this test wants to listen for. |
| 29 | + * The group name for this group of functions. |
29 | 30 | * |
30 | | - * @return array<int|string> |
| 31 | + * @var string |
31 | 32 | */ |
32 | | - public function register() { |
33 | | - return [ |
34 | | - T_STRING, |
35 | | - ]; |
36 | | - } |
| 33 | + protected $group_name = 'escaping_void'; |
| 34 | + |
| 35 | + /** |
| 36 | + * Functions this sniff is looking for. |
| 37 | + * |
| 38 | + * @var array<string, array{param_position: int, param_name: string}> Keys are the target functions, |
| 39 | + * value, the name and position of the target parameter. |
| 40 | + */ |
| 41 | + protected $target_functions = [ |
| 42 | + 'esc_attr' => [ |
| 43 | + 'param_position' => 1, |
| 44 | + 'param_name' => 'text', |
| 45 | + ], |
| 46 | + 'esc_attr__' => [ |
| 47 | + 'param_position' => 1, |
| 48 | + 'param_name' => 'text', |
| 49 | + ], |
| 50 | + 'esc_attr_e' => [ |
| 51 | + 'param_position' => 1, |
| 52 | + 'param_name' => 'text', |
| 53 | + ], |
| 54 | + 'esc_attr_x' => [ |
| 55 | + 'param_position' => 1, |
| 56 | + 'param_name' => 'text', |
| 57 | + ], |
| 58 | + 'esc_html' => [ |
| 59 | + 'param_position' => 1, |
| 60 | + 'param_name' => 'text', |
| 61 | + ], |
| 62 | + 'esc_html__' => [ |
| 63 | + 'param_position' => 1, |
| 64 | + 'param_name' => 'text', |
| 65 | + ], |
| 66 | + 'esc_html_e' => [ |
| 67 | + 'param_position' => 1, |
| 68 | + 'param_name' => 'text', |
| 69 | + ], |
| 70 | + 'esc_html_x' => [ |
| 71 | + 'param_position' => 1, |
| 72 | + 'param_name' => 'text', |
| 73 | + ], |
| 74 | + 'esc_js' => [ |
| 75 | + 'param_position' => 1, |
| 76 | + 'param_name' => 'text', |
| 77 | + ], |
| 78 | + 'esc_textarea' => [ |
| 79 | + 'param_position' => 1, |
| 80 | + 'param_name' => 'text', |
| 81 | + ], |
| 82 | + 'esc_url' => [ |
| 83 | + 'param_position' => 1, |
| 84 | + 'param_name' => 'url', |
| 85 | + ], |
| 86 | + 'esc_url_raw' => [ |
| 87 | + 'param_position' => 1, |
| 88 | + 'param_name' => 'url', |
| 89 | + ], |
| 90 | + 'esc_xml' => [ |
| 91 | + 'param_position' => 1, |
| 92 | + 'param_name' => 'text', |
| 93 | + ], |
| 94 | + 'tag_escape' => [ |
| 95 | + 'param_position' => 1, |
| 96 | + 'param_name' => 'tag_name', |
| 97 | + ], |
| 98 | + 'wp_kses' => [ |
| 99 | + 'param_position' => 1, |
| 100 | + 'param_name' => 'content', |
| 101 | + ], |
| 102 | + 'wp_kses_data' => [ |
| 103 | + 'param_position' => 1, |
| 104 | + 'param_name' => 'data', |
| 105 | + ], |
| 106 | + 'wp_kses_one_attr' => [ |
| 107 | + 'param_position' => 1, |
| 108 | + 'param_name' => 'attr', |
| 109 | + ], |
| 110 | + 'wp_kses_post' => [ |
| 111 | + 'param_position' => 1, |
| 112 | + 'param_name' => 'data', |
| 113 | + ], |
| 114 | + ]; |
37 | 115 |
|
38 | 116 | /** |
39 | | - * Process this test when one of its tokens is encountered |
| 117 | + * Process the parameters of a matched function. |
40 | 118 | * |
41 | | - * @param int $stackPtr The position of the current token in the stack passed in $tokens. |
| 119 | + * @param int $stackPtr The position of the current token in the stack. |
| 120 | + * @param string $group_name The name of the group which was matched. |
| 121 | + * @param string $matched_content The token content (function name) which was matched |
| 122 | + * in lowercase. |
| 123 | + * @param array $parameters Array with information about the parameters. |
42 | 124 | * |
43 | 125 | * @return void |
44 | 126 | */ |
45 | | - public function process_token( $stackPtr ) { |
| 127 | + public function process_parameters( $stackPtr, $group_name, $matched_content, $parameters ) { |
| 128 | + $param_position = $this->target_functions[ $matched_content ]['param_position']; |
| 129 | + $param_name = $this->target_functions[ $matched_content ]['param_name']; |
46 | 130 |
|
47 | | - if ( strpos( $this->tokens[ $stackPtr ]['content'], 'esc_' ) !== 0 && strpos( $this->tokens[ $stackPtr ]['content'], 'wp_kses' ) !== 0 ) { |
48 | | - // Not what we are looking for. |
| 131 | + $target_param = PassedParameters::getParameterFromStack( $parameters, $param_position, $param_name ); |
| 132 | + if ( $target_param === false ) { |
| 133 | + // Missing (required) target parameter. Probably live coding, nothing to examine (yet). Bow out. |
49 | 134 | return; |
50 | 135 | } |
51 | 136 |
|
52 | | - $next_token = $this->phpcsFile->findNext( Tokens::$emptyTokens, $stackPtr + 1, null, true ); |
| 137 | + $ignore = Tokens::$emptyTokens; |
| 138 | + $ignore[ T_NS_SEPARATOR ] = T_NS_SEPARATOR; |
53 | 139 |
|
54 | | - if ( $this->tokens[ $next_token ]['code'] !== T_OPEN_PARENTHESIS ) { |
55 | | - // Not a function call. |
| 140 | + $next_token = $this->phpcsFile->findNext( $ignore, $target_param['start'], ( $target_param['end'] + 1 ), true ); |
| 141 | + if ( $next_token === false || $this->tokens[ $next_token ]['code'] !== T_STRING ) { |
| 142 | + // Not what we are looking for. |
56 | 143 | return; |
57 | 144 | } |
58 | 145 |
|
59 | | - $next_token = $this->phpcsFile->findNext( Tokens::$emptyTokens, $next_token + 1, null, true ); |
60 | | - |
61 | | - if ( $this->tokens[ $next_token ]['code'] !== T_STRING ) { |
62 | | - // Not what we are looking for. |
| 146 | + $next_after = $this->phpcsFile->findNext( Tokens::$emptyTokens, $next_token + 1, ( $target_param['end'] + 1 ), true ); |
| 147 | + if ( $next_after === false || $this->tokens[ $next_after ]['code'] !== T_OPEN_PARENTHESIS ) { |
| 148 | + // Not a function call inside the escaping function. |
63 | 149 | return; |
64 | 150 | } |
65 | 151 |
|
|
0 commit comments