-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHelpersEscapeSniff.php
More file actions
317 lines (275 loc) · 9.81 KB
/
HelpersEscapeSniff.php
File metadata and controls
317 lines (275 loc) · 9.81 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
<?php
/**
* Eightshift coding standards for WordPress
*
* @package EightshiftCS
*
* @author Eightshift <team.wordpress@infinum.com>
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/infinum/eightshift-coding-standards
*/
namespace EightshiftCS\Eightshift\Sniffs\Security;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\GetTokensAsString;
use PHPCSUtils\Utils\UseStatements;
use WordPressCS\WordPress\Sniffs\Security\EscapeOutputSniff;
/**
* Override the WordPress.Security.EscapeOutput sniff
*
* This sniff will ignore escaping errors whenever it finds the
* EightshiftLibs\Helpers\Helpers::$allowedMethods, where the $allowedMethods are by default `render` and `outputCssVariables`, but can be extended in the ruleset.
*
* $allowedMethods are considered safe.
*
* @since 2.0.0 Add list of allowed static methods that shouldn't trigger the sniff error.
* @since 1.4.0
*/
class HelpersEscapeSniff extends EscapeOutputSniff
{
/**
* A fully qualified class name for Helpers class override.
*
* You should put the fully qualified class name of the class you used
* to override the EightshiftLibs\Helpers\Component class.
*
* For Example: namespace\\SomeSubNamespace\\MyHelpers.
*
* @since 1.4.0
*
* @var string Defaults to empty string.
*/
public string $overriddenClass = '';
/**
* List of allowed methods that won't trigger the EscapeOutput error.
*
* @since 2.0.0
*
* @var array<string, string>
*/
public array $allowedMethods = [
'render' => 'render',
'outputCssVariables' => 'outputCssVariables',
];
/**
* Processes this test, when one of its tokens is encountered.
*
* @since 1.4.0
*
* @param int $stackPtr The position of the current token in the stack.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function process_token($stackPtr)
{
$tokens = $this->tokens;
$phpcsFile = $this->phpcsFile;
$importData = [
'importExists' => false,
'fullImportExists' => false
];
$importExists = false;
// Check if the current token is a part of the import, if it is, skip the check.
$useToken = $phpcsFile->findPrevious(\T_USE, ($stackPtr - 1), null, false, null, false);
if ($useToken) {
// Find all use tokens.
foreach ($tokens as $tokenPtr => $token) {
if ($token['code'] === \T_USE) {
$importData = $this->checkIfImportInformation($tokenPtr, $importData);
if ($importData['importExists']) {
break;
}
}
}
$importExists = $importData['importExists'];
}
if ($tokens[$stackPtr]['code'] === \T_ECHO) {
// Check the next token after echo.
$elementPtr = $phpcsFile->findNext(Tokens::$emptyTokens, ($stackPtr + 1), null, true);
// If it's not the string token, move on, we're only interested in Helpers string.
if ($tokens[$elementPtr]['code'] !== \T_STRING) {
return parent::process_token($stackPtr);
}
// Check the next string token content.
if ($importExists) {
$fullyQualifiedImport = $importData['importName'];
if (\strpos($fullyQualifiedImport, $tokens[$elementPtr]['content']) === false) {
return parent::process_token($stackPtr);
}
}
// Check for Helpers string token that is followed by the double colon (static method call).
// We need the class name "Helpers", not a namespace segment "Helpers".
$helpersClassNamePtr = false;
$searchPtr = $stackPtr;
while (($searchPtr = $phpcsFile->findNext(\T_STRING, ($searchPtr + 1), null, false, 'Helpers')) !== false) {
if (isset($tokens[$searchPtr + 1]) && $tokens[$searchPtr + 1]['code'] === \T_DOUBLE_COLON) {
$helpersClassNamePtr = $searchPtr;
break;
}
}
if (!$helpersClassNamePtr) {
// If there is no Helpers down the line, just run the regular sniff.
return parent::process_token($stackPtr);
}
// Check if the next token is double colon. We are interested in static methods.
if ($tokens[$helpersClassNamePtr + 1]['code'] !== \T_DOUBLE_COLON) {
$echoPtr = $phpcsFile->findPrevious(\T_ECHO, ($helpersClassNamePtr - 1), null, false, null, true);
return parent::process_token($echoPtr);
}
// If it is, check, if the class is imported or fully qualified.
$nameEnd = $phpcsFile->findPrevious(\T_STRING, ($helpersClassNamePtr + 1));
$nameStart = ($phpcsFile->findPrevious(
[\T_STRING, \T_NS_SEPARATOR, \T_NAMESPACE],
($nameEnd - 1),
null,
true,
null,
true
) + 1);
$className = GetTokensAsString::normal($phpcsFile, $nameStart, $nameEnd);
if ($importExists) {
// Fully qualified import, i.e. EightshiftLibs\Helpers\Helpers.
if ($importData['fullImportExists']) {
// Helpers name is ok, \Helpers is not ok, \Anything\Helpers is not ok FQCN is ok.
if (
$className === 'Helpers'
|| \strpos($className, 'EightshiftLibs\\Helpers\\Helpers') !== false
|| (! empty($this->overriddenClass) && \strpos($className, $this->overriddenClass) !== false)
) {
// Check the static method name.
$methodNamePtr = $phpcsFile->findNext(
\T_STRING,
($helpersClassNamePtr + 1),
null,
false,
null,
true
);
if (\in_array($tokens[$methodNamePtr]['content'], $this->allowedMethods, true)) {
return; // Skip sniffing allowed methods.
} else {
// Not allowed method, continue as usual.
$echoPtr = $this->getEchoToken($helpersClassNamePtr);
return parent::process_token($echoPtr);
}
} else {
// Some other class we don't care about.
$echoPtr = $this->getEchoToken($helpersClassNamePtr);
return parent::process_token($echoPtr);
}
} else {
// Partial import, Check if the last part of the import exists at the beginning of the class name.
$import = \explode('\\', $importData['importName'] ?? '');
$lastNamespacePart = \end($import);
$checkedClassName = \explode('\\', $className);
$firstNamespacePart = $checkedClassName[0];
// For partial imports, the className must contain multiple parts (e.g. Helpers\Helpers::method())
// A single-part className like Helpers::method() with a partial import to the namespace
// would resolve to the namespace, not the class.
if ($lastNamespacePart === $firstNamespacePart && \count($checkedClassName) > 1) {
// Correctly used class name.
$methodNamePtr = $phpcsFile->findNext(
\T_STRING,
($helpersClassNamePtr + 1),
null,
false,
null,
true
);
if (\in_array($tokens[$methodNamePtr]['content'], $this->allowedMethods, true)) {
return; // Skip sniffing allowed methods.
} else {
// Not allowed method, continue as usual.
$echoPtr = $this->getEchoToken($helpersClassNamePtr);
return parent::process_token($echoPtr);
}
} else {
// Wrongly imported, or class that is not related to the libs.
$echoPtr = $this->getEchoToken($helpersClassNamePtr);
return parent::process_token($echoPtr);
}
}
} else {
// Check if the class name is fully qualified and contains the helper part.
if (
\strpos($className, 'EightshiftLibs\\Helpers\\Helpers') !== false
|| (! empty($this->overriddenClass) && \strpos($className, $this->overriddenClass) !== false)
) {
$methodNamePtr = $phpcsFile->findNext(
\T_STRING,
($helpersClassNamePtr + 1),
null,
false,
null,
true
);
if (\in_array($tokens[$methodNamePtr]['content'], $this->allowedMethods, true)) {
return; // Skip sniffing allowed methods.
} else {
// Not allowed method, continue as usual.
$echoPtr = $this->getEchoToken($helpersClassNamePtr);
return parent::process_token($echoPtr);
}
} else {
$echoPtr = $this->getEchoToken($helpersClassNamePtr);
return parent::process_token($echoPtr);
}
}
}
// Process the sniff as usual.
return parent::process_token($stackPtr);
}
/**
* Checks if the import statement exists in the current file, for the given stack pointer.
*
* @since 1.4.0
*
* @param int $stackPtr The position of the current token in the stack.
* @param String[] $importData Import data array.
*
* @return array<string, bool|string> Information about the imports
*/
private function checkIfImportInformation(int $stackPtr, array $importData): array
{
$phpcsFile = $this->phpcsFile;
$overriddenClass = \str_replace('\\\\', '\\', $this->overriddenClass);
if (UseStatements::isImportUse($phpcsFile, $stackPtr)) {
$importInfo = UseStatements::splitImportUseStatement($phpcsFile, $stackPtr);
if (!empty($importInfo)) {
foreach ($importInfo['name'] as $fullyQualifiedClassNameImport) {
if (
\strpos($fullyQualifiedClassNameImport, 'EightshiftLibs\\Helpers') !== false
|| (! empty($overriddenClass) && \strpos($fullyQualifiedClassNameImport, $overriddenClass) !== false)
) {
$importData['importExists'] = true;
$importData['importName'] = $fullyQualifiedClassNameImport;
// Check for fully qualified import.
if (
\strpos($fullyQualifiedClassNameImport, 'EightshiftLibs\\Helpers\\Helpers') !== false
|| (! empty($overriddenClass) && \strpos($fullyQualifiedClassNameImport, $overriddenClass) !== false) // phpcs:ignore Generic.Files.LineLength.TooLong
) {
$importData['fullImportExists'] = true;
$importData['importName'] = $fullyQualifiedClassNameImport;
break;
}
break;
}
}
}
}
return $importData;
}
/**
* Return the position of the previous echo pointer.
*
* @since 1.4.0
*
* @param int $stackPtr The position of the current token in the stack.
*
* @return int Echo token pointer number.
*/
private function getEchoToken(int $stackPtr): int
{
return $this->phpcsFile->findPrevious(\T_ECHO, ($stackPtr - 1), null, false, null, true);
}
}