-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathOptionsRaceConditionSniff.php
More file actions
344 lines (293 loc) · 9.21 KB
/
OptionsRaceConditionSniff.php
File metadata and controls
344 lines (293 loc) · 9.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* WordPressVIPMinimum Coding Standard.
*
* @package VIPCS\WordPressVIPMinimum
*/
namespace WordPressVIPMinimum\Sniffs\Functions;
use WordPressVIPMinimum\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\AbstractScopeSniff;
/**
* This sniff checks to see if the deletion of an option is immediately followed by an addition of the same option.
*/
class OptionsRaceConditionSniff extends AbstractScopeSniff {
/**
* Function name to delete option.
*
* @var string
*/
private $delete_option = 'delete_option';
/**
* Function name to add option.
*
* @var string
*/
private $add_option = 'add_option';
/**
* Constructs the test with the tokens it wishes to listen for.
*/
public function __construct() {
parent::__construct(
[ T_FUNCTION ],
[ T_STRING ]
);
}
/**
* Processes the function tokens within the class.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position where the token was found.
* @param int $currScope The current scope opener token.
*
* @return void
*/
protected function processTokenWithinScope( File $phpcsFile, $stackPtr, $currScope ) {
$tokens = $phpcsFile->getTokens();
if ( $tokens[ $stackPtr ]['content'] !== $this->delete_option ) {
// delete_option is not first function found.
$stackPtr = $phpcsFile->findNext(
[ T_STRING ],
$stackPtr + 1,
null,
true,
$this->delete_option
);
if ( ! $stackPtr ) {
return; // No delete_option found, bail.
}
}
$delete_option_scope_start = $phpcsFile->findNext(
Tokens::$emptyTokens,
$stackPtr + 1,
null,
true
);
if ( ! $delete_option_scope_start || $tokens[ $delete_option_scope_start ]['code'] !== T_OPEN_PARENTHESIS ) {
return; // Not a function call, bail.
}
$delete_option_name = $phpcsFile->findNext(
Tokens::$emptyTokens,
$delete_option_scope_start + 1,
null,
true
);
$delete_option_concat = $phpcsFile->findNext(
Tokens::$emptyTokens,
$delete_option_name + 1,
null,
true
);
$delete_option_name = $this->trim_strip_quotes( $tokens[ $delete_option_name ]['content'] );
$is_delete_option_concat = $tokens[ $delete_option_concat ]['code'] === T_STRING_CONCAT;
if ( $is_delete_option_concat ) {
// If option name is concatenated, we need to build it out.
$delete_option_concat = $phpcsFile->findNext(
Tokens::$emptyTokens,
$delete_option_concat + 1,
null,
true
);
while ( $delete_option_concat < $tokens[ $delete_option_scope_start ]['parenthesis_closer'] ) {
$delete_option_name .= $this->trim_strip_quotes( $tokens[ $delete_option_concat ]['content'] );
$delete_option_concat = $phpcsFile->findNext(
array_merge( Tokens::$emptyTokens, [ T_STRING_CONCAT ] ),
$delete_option_concat + 1,
null,
true
);
}
}
$delete_option_scope_end = $phpcsFile->findNext(
[ T_SEMICOLON ],
$tokens[ $delete_option_scope_start ]['parenthesis_closer'] + 1,
null,
false
);
if ( ! $delete_option_scope_end ) {
return; // Something went wrong with the syntax.
}
$add_option = $phpcsFile->findNext(
Tokens::$emptyTokens,
$delete_option_scope_end + 1,
null,
true
);
$message = 'Concurrent calls to `delete_option()` and `add_option()` for %s may lead to race conditions in persistent object caching. Please consider using `update_option()` in place of both function calls, as it will also add the option if it does not exist.';
if ( $tokens[ $add_option ]['code'] === T_IF ) {
// Check inside scope of first IF statement after for `add_option()` being called.
$add_option_inside_if = $phpcsFile->findNext(
[ T_STRING ],
$tokens[ $add_option ]['scope_opener'] + 1,
null,
false,
$this->add_option
);
if ( ! $add_option_inside_if || $add_option_inside_if > $tokens[ $add_option ]['scope_closer'] ) {
return; // No add_option() call inside first IF statement or add_option found not in IF scope.
}
$add_option_inside_if_scope_start = $phpcsFile->findNext(
Tokens::$emptyTokens,
$add_option_inside_if + 1,
null,
true
);
if ( ! $add_option_inside_if_scope_start || $tokens[ $add_option_inside_if_scope_start ]['code'] !== T_OPEN_PARENTHESIS ) {
return; // Not a function call, bail.
}
$add_option_inside_if_option_name = $phpcsFile->findNext(
Tokens::$emptyTokens,
$add_option_inside_if_scope_start + 1,
null,
true
);
$add_option_inside_if_concat = $phpcsFile->findNext(
Tokens::$emptyTokens,
$add_option_inside_if_option_name + 1,
null,
true
);
$add_option_inside_if_option_name = $this->trim_strip_quotes( $tokens[ $add_option_inside_if_option_name ]['content'] );
if ( $is_delete_option_concat && $tokens[ $add_option_inside_if_concat ]['code'] === T_STRING_CONCAT ) {
$add_option_inside_if_concat = $phpcsFile->findNext(
array_merge( Tokens::$emptyTokens, [ T_STRING_CONCAT ] ),
$add_option_inside_if_concat + 1,
null,
true
);
$add_option_inside_if_scope_end = $phpcsFile->findNext(
[ T_COMMA ],
$add_option_inside_if_concat + 1,
null,
false
);
if ( ! $add_option_inside_if_scope_end ) {
return; // Something went wrong.
}
while ( $add_option_inside_if_concat < $add_option_inside_if_scope_end ) {
$add_option_inside_if_option_name .= $this->trim_strip_quotes( $tokens[ $add_option_inside_if_concat ]['content'] );
$add_option_inside_if_concat = $phpcsFile->findNext(
array_merge( Tokens::$emptyTokens, [ T_STRING_CONCAT ] ),
$add_option_inside_if_concat + 1,
null,
true
);
}
}
if ( $this->is_same_option_key( $delete_option_name, $add_option_inside_if_option_name ) ) {
$phpcsFile->addWarning( $message, $add_option_inside_if_scope_start, 'OptionsRaceCondition' );
}
// Walk ahead out of IF control structure.
$add_option = $phpcsFile->findNext(
Tokens::$emptyTokens,
$tokens[ $add_option ]['scope_closer'] + 1,
null,
true
);
}
if ( $tokens[ $add_option ]['code'] === T_VARIABLE ) {
// Account for variable assignments.
$equals = $phpcsFile->findNext(
Tokens::$emptyTokens,
$add_option + 1,
null,
true
);
if ( $tokens[ $equals ]['code'] === T_EQUAL ) {
$add_option = $phpcsFile->findNext(
Tokens::$emptyTokens,
$equals + 1,
null,
true
);
}
}
if ( $tokens[ $add_option ]['code'] !== T_STRING || $tokens[ $add_option ]['content'] !== $this->add_option ) {
return; // add_option() isn't immediately following delete_option(), bail.
}
$add_option_scope_start = $phpcsFile->findNext(
Tokens::$emptyTokens,
$add_option + 1,
null,
true
);
if ( ! $add_option_scope_start || $tokens[ $add_option_scope_start ]['code'] !== T_OPEN_PARENTHESIS ) {
return; // Not a function call, bail.
}
$add_option_name = $phpcsFile->findNext(
Tokens::$emptyTokens,
$add_option_scope_start + 1,
null,
true
);
$add_option_concat = $phpcsFile->findNext(
Tokens::$emptyTokens,
$add_option_name + 1,
null,
true
);
$add_option_name = $this->trim_strip_quotes( $tokens[ $add_option_name ]['content'] );
if ( $is_delete_option_concat && $tokens[ $add_option_concat ]['code'] === T_STRING_CONCAT ) {
$add_option_concat = $phpcsFile->findNext(
Tokens::$emptyTokens,
$add_option_concat + 1,
null,
true
);
$add_option_scope_end = $phpcsFile->findNext(
[ T_COMMA ],
$add_option_concat + 1,
null,
false
);
if ( ! $add_option_scope_end ) {
return; // Something went wrong.
}
while ( $add_option_concat < $add_option_scope_end ) {
$add_option_name .= $this->trim_strip_quotes( $tokens[ $add_option_concat ]['content'] );
$add_option_concat = $phpcsFile->findNext(
array_merge( Tokens::$emptyTokens, [ T_STRING_CONCAT ] ),
$add_option_concat + 1,
null,
true
);
}
}
if ( $this->is_same_option_key( $delete_option_name, $add_option_name ) ) {
$phpcsFile->addWarning( $message, $add_option_scope_start, 'OptionsRaceCondition' );
return;
}
}
/**
* Processes a token that is found within the scope that this test is
* listening to.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file where this token was found.
* @param int $stackPtr The position in the stack where this
* token was found.
* @return void
*/
protected function processTokenOutsideScope( File $phpcsFile, $stackPtr ) {
}
/**
* Check if option is the same.
*
* @param string $option_name Option name.
* @param string $option_name_to_compare Option name to compare against.
*
* @return false
*/
private function is_same_option_key( $option_name, $option_name_to_compare ) {
return $option_name === $option_name_to_compare;
}
/**
* Trim whitespace and strip quotes surrounding an arbitrary string.
*
* @param string $string The raw string.
* @return string String without whitespace or quotes around it.
*/
public function trim_strip_quotes( $string ) {
return trim( preg_replace( '`^([\'"])(.*)\1$`Ds', '$2', $string ) );
}
}