-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathProperEscapingFunctionSniff.php
More file actions
291 lines (258 loc) · 8.63 KB
/
ProperEscapingFunctionSniff.php
File metadata and controls
291 lines (258 loc) · 8.63 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
<?php
/**
* WordPressVIPMinimum Coding Standard.
*
* @package VIPCS\WordPressVIPMinimum
* @link https://github.com/Automattic/VIP-Coding-Standards
* @license https://opensource.org/license/gpl-2-0 GPL-2.0
*/
namespace WordPressVIPMinimum\Sniffs\Security;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\BackCompat\BCFile;
use PHPCSUtils\Utils\TextStrings;
use WordPressVIPMinimum\Sniffs\Sniff;
/**
* Checks whether proper escaping function is used.
*/
class ProperEscapingFunctionSniff extends Sniff {
/**
* Regular expression to match the end of HTML attributes.
*
* @var string
*/
const ATTR_END_REGEX = '`(?<attrname>href|src|url|(^|\s+)action)?(?<=[a-z0-9_-])=(?:\\\\)?["\']*$`i';
/**
* List of escaping functions which are being tested.
*
* @var array<string, string>
*/
protected $escaping_functions = [
'esc_url' => 'url',
'esc_attr' => 'attr',
'esc_attr__' => 'attr',
'esc_attr_x' => 'attr',
'esc_attr_e' => 'attr',
'esc_html' => 'html',
'esc_html__' => 'html',
'esc_html_x' => 'html',
'esc_html_e' => 'html',
];
/**
* List of tokens we can skip.
*
* @var array<int|string, int|string>
*/
private $echo_or_concat_tokens =
[
T_ECHO => T_ECHO,
T_OPEN_TAG => T_OPEN_TAG,
T_OPEN_TAG_WITH_ECHO => T_OPEN_TAG_WITH_ECHO,
T_STRING_CONCAT => T_STRING_CONCAT,
T_NS_SEPARATOR => T_NS_SEPARATOR,
];
/**
* List of attributes associated with url outputs.
*
* @deprecated 2.3.1 Currently unused by the sniff, but needed for
* for public methods which extending sniffs may be
* relying on.
*
* @var array<string>
*/
private $url_attrs = [
'href',
'src',
'url',
'action',
];
/**
* List of syntaxes for inside attribute detection.
*
* @deprecated 2.3.1 Currently unused by the sniff, but needed for
* for public methods which extending sniffs may be
* relying on.
*
* @var array<string>
*/
private $attr_endings = [
'=',
'="',
"='",
"=\\'",
'=\\"',
];
/**
* Keep track of whether or not we're currently in the first statement of a short open echo tag.
*
* @var int|false Integer stack pointer to the end of the first statement in the current
* short open echo tag or false when not in a short open echo tag.
*/
private $in_short_echo = false;
/**
* Keep track of the current file, so we can reset $in_short_echo for each new file.
*
* @var string Absolute file name of the file being processed. Defaults to an empty string.
*/
private $current_file = '';
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register() {
$this->echo_or_concat_tokens += Tokens::$emptyTokens;
return [
T_STRING,
T_OPEN_TAG_WITH_ECHO,
];
}
/**
* Process this test when one of its tokens is encountered
*
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
*
* @return void
*/
public function process_token( $stackPtr ) {
// Reset short echo context tracking variable for a new file.
if ( $this->phpcsFile->getFilename() !== $this->current_file ) {
$this->in_short_echo = false;
$this->current_file = $this->phpcsFile->getFilename();
}
/*
* Short open echo tags will act as an echo for the first expression and
* allow for passing multiple comma-separated parameters.
* However, short open echo tags also allow for additional statements after, but
* those have to be full PHP statements, not expressions.
*
* This snippet of code will keep track of whether or not we're in the first
* expression in a short open echo tag.
* $phpcsFile->findStartOfStatement() unfortunately is useless, as it will return
* the first token in the statement, which can be anything - variable, text string -
* without any indication of whether this is the start of a normal statement or
* a short open echo expression.
* So, if we used that, we'd need to walk back from every start of statement to
* the previous non-empty to see if it is the short open echo tag.
*/
if ( $this->tokens[ $stackPtr ]['code'] === T_OPEN_TAG_WITH_ECHO ) {
$end_of_echo = $this->phpcsFile->findNext( [ T_SEMICOLON, T_CLOSE_TAG ], ( $stackPtr + 1 ) );
if ( $end_of_echo === false ) {
$this->in_short_echo = $this->phpcsFile->numTokens;
} else {
$this->in_short_echo = $end_of_echo;
}
return;
}
if ( $this->in_short_echo !== false && $this->in_short_echo < $stackPtr ) {
$this->in_short_echo = false;
}
$function_name = strtolower( $this->tokens[ $stackPtr ]['content'] );
if ( isset( $this->escaping_functions[ $function_name ] ) === false ) {
return;
}
$next_non_empty = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true );
if ( $next_non_empty === false || $this->tokens[ $next_non_empty ]['code'] !== T_OPEN_PARENTHESIS ) {
// Not a function call.
return;
}
$ignore = $this->echo_or_concat_tokens;
if ( $this->in_short_echo !== false ) {
$ignore[ T_COMMA ] = T_COMMA;
} else {
$start_of_statement = BCFile::findStartOfStatement( $this->phpcsFile, $stackPtr, T_COMMA );
if ( $this->tokens[ $start_of_statement ]['code'] === T_ECHO ) {
$ignore[ T_COMMA ] = T_COMMA;
}
}
$html = $this->phpcsFile->findPrevious( $ignore, $stackPtr - 1, null, true );
// Use $textStringTokens b/c heredoc and nowdoc tokens will never be encountered in this context anyways..
if ( $html === false || isset( Tokens::$textStringTokens[ $this->tokens[ $html ]['code'] ] ) === false ) {
return;
}
$data = [ $function_name ];
$content = $this->tokens[ $html ]['content'];
if ( isset( Tokens::$stringTokens[ $this->tokens[ $html ]['code'] ] ) === true ) {
$content = TextStrings::stripQuotes( $content );
}
$escaping_type = $this->escaping_functions[ $function_name ];
if ( $escaping_type === 'attr' && $this->is_outside_html_attr_context( $content ) ) {
$message = 'Wrong escaping function, using `%s()` in a context outside of HTML attributes may not escape properly.';
$this->phpcsFile->addError( $message, $html, 'notAttrEscAttr', $data );
return;
}
if ( preg_match( self::ATTR_END_REGEX, $content, $matches ) !== 1 ) {
return;
}
if ( $escaping_type !== 'url' && empty( $matches['attrname'] ) === false ) {
$message = 'Wrong escaping function. href, src, and action attributes should be escaped by `esc_url()`, not by `%s()`.';
$this->phpcsFile->addError( $message, $stackPtr, 'hrefSrcEscUrl', $data );
return;
}
if ( $escaping_type === 'html' ) {
$message = 'Wrong escaping function. HTML attributes should be escaped by `esc_attr()`, not by `%s()`.';
$this->phpcsFile->addError( $message, $stackPtr, 'htmlAttrNotByEscHTML', $data );
return;
}
}
/**
* Tests whether provided string ends with open attribute which expects a URL value.
*
* @deprecated 2.3.1
*
* @param string $content Haystack in which we look for an open attribute which exects a URL value.
*
* @return bool True if string ends with open attribute which expects a URL value.
*/
public function attr_expects_url( $content ) {
$attr_expects_url = false;
foreach ( $this->url_attrs as $attr ) {
foreach ( $this->attr_endings as $ending ) {
if ( $this->endswith( $content, $attr . $ending ) === true ) {
$attr_expects_url = true;
break;
}
}
}
return $attr_expects_url;
}
/**
* Tests whether provided string ends with open HMTL attribute.
*
* @deprecated 2.3.1
*
* @param string $content Haystack in which we look for open HTML attribute.
*
* @return bool True if string ends with open HTML attribute.
*/
public function is_html_attr( $content ) {
$is_html_attr = false;
foreach ( $this->attr_endings as $ending ) {
if ( $this->endswith( $content, $ending ) === true ) {
$is_html_attr = true;
break;
}
}
return $is_html_attr;
}
/**
* Tests whether an attribute escaping function is being used outside of an HTML tag.
*
* @param string $content Haystack where we look for the end of a HTML tag.
*
* @return bool True if the passed string ends a HTML tag.
*/
public function is_outside_html_attr_context( $content ) {
return $this->endswith( trim( $content ), '>' );
}
/**
* A helper function which tests whether string ends with some other.
*
* @param string $haystack String which is being tested.
* @param string $needle The substring, which we try to locate on the end of the $haystack.
*
* @return bool True if haystack ends with needle.
*/
public function endswith( $haystack, $needle ) {
return substr( $haystack, -strlen( $needle ) ) === $needle;
}
}