-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathUnderscorejsSniff.php
More file actions
161 lines (138 loc) · 4.59 KB
/
UnderscorejsSniff.php
File metadata and controls
161 lines (138 loc) · 4.59 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
<?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\Utils\TextStrings;
use WordPressVIPMinimum\Sniffs\Sniff;
/**
* Looks for instances of unescaped output for Underscore.js templating engine.
*/
class UnderscorejsSniff extends Sniff {
/**
* Regex to match unescaped output notations containing variable interpolation
* and retrieve a code snippet.
*
* @var string
*/
const UNESCAPED_INTERPOLATE_REGEX = '`<%=\s*(?:.+?%>|$)`';
/**
* Regex to match execute notations containing a print command
* and retrieve a code snippet.
*
* @var string
*/
const UNESCAPED_PRINT_REGEX = '`<%\s*(?:print\s*\(.+?\)\s*;|__p\s*\+=.+?)\s*%>`';
/**
* Regex to match the "interpolate" keyword when used to overrule the ERB-style delimiters.
*
* @var string
*/
const INTERPOLATE_KEYWORD_REGEX = '`(?:templateSettings\.interpolate|\.interpolate\s*=\s*/|interpolate\s*:\s*/)`';
/**
* A list of tokenizers this sniff supports.
*
* @var string[]
*/
public $supportedTokenizers = [ 'JS', 'PHP' ];
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array<int|string>
*/
public function register() {
$targets = Tokens::$textStringTokens;
$targets[] = T_PROPERTY;
$targets[] = T_STRING;
return $targets;
}
/**
* Processes 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 ) {
/*
* Ignore Gruntfile.js files as they are configuration, not code.
*/
$file_name = TextStrings::stripQuotes( $this->phpcsFile->getFileName() );
$file_name = strtolower( basename( $file_name ) );
if ( $file_name === 'gruntfile.js' ) {
return;
}
/*
* Check for delimiter change in JS files.
*/
if ( $this->tokens[ $stackPtr ]['code'] === T_STRING
|| $this->tokens[ $stackPtr ]['code'] === T_PROPERTY
) {
if ( $this->phpcsFile->tokenizerType !== 'JS' ) {
// These tokens are only relevant for JS files.
return;
}
if ( $this->tokens[ $stackPtr ]['content'] !== 'interpolate' ) {
return;
}
// Check the context to prevent false positives.
if ( $this->tokens[ $stackPtr ]['code'] === T_STRING ) {
$prev = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true );
if ( $prev === false || $this->tokens[ $prev ]['code'] !== T_OBJECT_OPERATOR ) {
return;
}
$prevPrev = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $stackPtr - 1 ), null, true );
$next = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $stackPtr + 1 ), null, true );
if ( ( $prevPrev === false
|| $this->tokens[ $prevPrev ]['code'] !== T_STRING
|| $this->tokens[ $prevPrev ]['content'] !== 'templateSettings' )
&& ( $next === false
|| $this->tokens[ $next ]['code'] !== T_EQUAL )
) {
return;
}
}
// Underscore.js delimiter change.
$message = 'Found Underscore.js delimiter change notation.';
$this->phpcsFile->addWarning( $message, $stackPtr, 'InterpolateFound' );
return;
}
$content = TextStrings::stripQuotes( $this->tokens[ $stackPtr ]['content'] );
$match_count = preg_match_all( self::UNESCAPED_INTERPOLATE_REGEX, $content, $matches );
if ( $match_count > 0 ) {
foreach ( $matches[0] as $match ) {
if ( strpos( $match, '_.escape(' ) !== false ) {
continue;
}
// Underscore.js unescaped output.
$message = 'Found Underscore.js unescaped output notation: "%s".';
$data = [ $match ];
$this->phpcsFile->addWarning( $message, $stackPtr, 'OutputNotation', $data );
}
}
$match_count = preg_match_all( self::UNESCAPED_PRINT_REGEX, $content, $matches );
if ( $match_count > 0 ) {
foreach ( $matches[0] as $match ) {
if ( strpos( $match, '_.escape(' ) !== false ) {
continue;
}
// Underscore.js unescaped output.
$message = 'Found Underscore.js unescaped print execution: "%s".';
$data = [ $match ];
$this->phpcsFile->addWarning( $message, $stackPtr, 'PrintExecution', $data );
}
}
if ( $this->phpcsFile->tokenizerType !== 'JS'
&& preg_match( self::INTERPOLATE_KEYWORD_REGEX, $content ) > 0
) {
// Underscore.js delimiter change.
$message = 'Found Underscore.js delimiter change notation.';
$this->phpcsFile->addWarning( $message, $stackPtr, 'InterpolateFound' );
}
}
}