|
10 | 10 | namespace WordPressVIPMinimum\Sniffs\Security; |
11 | 11 |
|
12 | 12 | use PHP_CodeSniffer\Util\Tokens; |
| 13 | +use PHPCSUtils\Exceptions\UnexpectedTokenType; |
13 | 14 | use PHPCSUtils\Tokens\Collections; |
14 | | -use PHPCSUtils\Utils\Arrays; |
15 | 15 | use PHPCSUtils\Utils\PassedParameters; |
16 | 16 | use WordPressCS\WordPress\AbstractFunctionParameterSniff; |
17 | 17 |
|
@@ -48,49 +48,50 @@ class StaticStrreplaceSniff extends AbstractFunctionParameterSniff { |
48 | 48 | * @return void |
49 | 49 | */ |
50 | 50 | public function process_parameters( $stackPtr, $group_name, $matched_content, $parameters ) { |
51 | | - |
52 | | - $openBracket = $this->phpcsFile->findNext( Tokens::$emptyTokens, $stackPtr + 1, null, true ); |
53 | | - |
54 | | - if ( $this->tokens[ $openBracket ]['code'] !== T_OPEN_PARENTHESIS ) { |
| 51 | + $search_param = PassedParameters::getParameterFromStack( $parameters, 1, 'search' ); |
| 52 | + $replace_param = PassedParameters::getParameterFromStack( $parameters, 2, 'replace' ); |
| 53 | + $subject_param = PassedParameters::getParameterFromStack( $parameters, 3, 'subject' ); |
| 54 | + |
| 55 | + if ( $search_param === false || $replace_param === false || $subject_param === false ) { |
| 56 | + /* |
| 57 | + * Either an invalid function call (missing PHP required parameter); or function call |
| 58 | + * with argument unpacking; or live coding. |
| 59 | + * In all these cases, this is not the code pattern this sniff is looking for, so bow out. |
| 60 | + */ |
55 | 61 | return; |
56 | 62 | } |
57 | 63 |
|
58 | 64 | $static_text_tokens = Tokens::$emptyTokens; |
59 | 65 | $static_text_tokens[ T_CONSTANT_ENCAPSED_STRING ] = T_CONSTANT_ENCAPSED_STRING; |
60 | 66 |
|
61 | | - $next_start_ptr = $openBracket + 1; |
62 | | - for ( $i = 0; $i < 3; $i++ ) { |
63 | | - $param_ptr = $this->phpcsFile->findNext( array_merge( Tokens::$emptyTokens, [ T_COMMA ] ), $next_start_ptr, null, true ); |
64 | | - if ( $param_ptr === false ) { |
65 | | - // Live coding or parse error. Ignore. |
66 | | - return; |
| 67 | + foreach ( [ $search_param, $replace_param, $subject_param ] as $param ) { |
| 68 | + $has_non_static_text = $this->phpcsFile->findNext( $static_text_tokens, $param['start'], ( $param['end'] + 1 ), true ); |
| 69 | + if ( $has_non_static_text === false ) { |
| 70 | + // The parameter contained only tokens which could be considered static text. |
| 71 | + continue; |
67 | 72 | } |
68 | 73 |
|
69 | | - if ( isset( Collections::arrayOpenTokensBC()[ $this->tokens[ $param_ptr ]['code'] ] ) ) { |
70 | | - $arrayOpenClose = Arrays::getOpenClose( $this->phpcsFile, $param_ptr ); |
71 | | - if ( $arrayOpenClose === false ) { |
| 74 | + if ( isset( Collections::arrayOpenTokensBC()[ $this->tokens[ $has_non_static_text ]['code'] ] ) ) { |
| 75 | + try { |
| 76 | + $array_items = PassedParameters::getParameters( $this->phpcsFile, $has_non_static_text ); |
| 77 | + } catch ( UnexpectedTokenType $e ) { |
72 | 78 | // Short list, parse error or live coding, bow out. |
73 | 79 | return; |
74 | 80 | } |
75 | 81 |
|
76 | | - $array_items = PassedParameters::getParameters( $this->phpcsFile, $param_ptr ); |
77 | 82 | foreach ( $array_items as $array_item ) { |
78 | 83 | $has_non_static_text = $this->phpcsFile->findNext( $static_text_tokens, $array_item['start'], $array_item['end'], true ); |
79 | 84 | if ( $has_non_static_text !== false ) { |
80 | 85 | return; |
81 | 86 | } |
82 | 87 | } |
83 | 88 |
|
84 | | - $next_start_ptr = $arrayOpenClose['closer'] + 1; |
| 89 | + // The array only contained items with tokens which could be considered static text. |
85 | 90 | continue; |
86 | 91 | } |
87 | 92 |
|
88 | | - if ( $this->tokens[ $param_ptr ]['code'] !== T_CONSTANT_ENCAPSED_STRING ) { |
89 | | - return; |
90 | | - } |
91 | | - |
92 | | - $next_start_ptr = $param_ptr + 1; |
93 | | - |
| 93 | + // Non-static text token found. Not what we're looking for. |
| 94 | + return; |
94 | 95 | } |
95 | 96 |
|
96 | 97 | $message = 'This code pattern is often used to run a very dangerous shell programs on your server. The code in these files needs to be reviewed, and possibly cleaned.'; |
|
0 commit comments