-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathTwigSniff.php
More file actions
65 lines (57 loc) · 1.74 KB
/
TwigSniff.php
File metadata and controls
65 lines (57 loc) · 1.74 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
<?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 Twig templating engine.
*/
class TwigSniff extends Sniff {
/**
* 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() {
return Tokens::$textStringTokens;
}
/**
* 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 ) {
// Strip any potentially interpolated expressions.
$only_text = $this->tokens[ $stackPtr ]['content'];
if ( $this->tokens[ $stackPtr ]['code'] === T_DOUBLE_QUOTED_STRING
|| $this->tokens[ $stackPtr ]['code'] === T_HEREDOC
) {
$only_text = TextStrings::stripEmbeds( $only_text );
}
if ( preg_match( '/autoescape\s+false/', $only_text ) === 1 ) {
// Twig autoescape disabled.
$message = 'Found Twig autoescape disabling notation.';
$this->phpcsFile->addWarning( $message, $stackPtr, 'AutoescapeFalse' );
}
if ( preg_match( '/\|\s*raw/', $only_text ) === 1 ) {
// Twig default unescape filter.
$message = 'Found Twig default unescape filter: "|raw".';
$this->phpcsFile->addWarning( $message, $stackPtr, 'RawFound' );
}
}
}