Skip to content

Commit af10c81

Browse files
authored
Merge pull request #841 from Automattic/feature/security-twig-sniff-improvements
2 parents a83f273 + b10aaeb commit af10c81

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

WordPressVIPMinimum/Sniffs/Security/TwigSniff.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace WordPressVIPMinimum\Sniffs\Security;
1010

11+
use PHP_CodeSniffer\Util\Tokens;
12+
use PHPCSUtils\Utils\TextStrings;
1113
use WordPressVIPMinimum\Sniffs\Sniff;
1214

1315
/**
@@ -28,11 +30,7 @@ class TwigSniff extends Sniff {
2830
* @return array<int|string>
2931
*/
3032
public function register() {
31-
return [
32-
T_CONSTANT_ENCAPSED_STRING,
33-
T_INLINE_HTML,
34-
T_HEREDOC,
35-
];
33+
return Tokens::$textStringTokens;
3634
}
3735

3836
/**
@@ -43,14 +41,21 @@ public function register() {
4341
* @return void
4442
*/
4543
public function process_token( $stackPtr ) {
44+
// Strip any potentially interpolated expressions.
45+
$only_text = $this->tokens[ $stackPtr ]['content'];
46+
if ( $this->tokens[ $stackPtr ]['code'] === T_DOUBLE_QUOTED_STRING
47+
|| $this->tokens[ $stackPtr ]['code'] === T_HEREDOC
48+
) {
49+
$only_text = TextStrings::stripEmbeds( $only_text );
50+
}
4651

47-
if ( preg_match( '/autoescape\s+false/', $this->tokens[ $stackPtr ]['content'] ) === 1 ) {
52+
if ( preg_match( '/autoescape\s+false/', $only_text ) === 1 ) {
4853
// Twig autoescape disabled.
4954
$message = 'Found Twig autoescape disabling notation.';
5055
$this->phpcsFile->addWarning( $message, $stackPtr, 'AutoescapeFalse' );
5156
}
5257

53-
if ( preg_match( '/\|\s*raw/', $this->tokens[ $stackPtr ]['content'] ) === 1 ) {
58+
if ( preg_match( '/\|\s*raw/', $only_text ) === 1 ) {
5459
// Twig default unescape filter.
5560
$message = 'Found Twig default unescape filter: "|raw".';
5661
$this->phpcsFile->addWarning( $message, $stackPtr, 'RawFound' );

WordPressVIPMinimum/Tests/Security/TwigUnitTest.inc

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,52 @@
99
{% autoescape %}
1010
{{ safe_value|raw }}
1111
{% endautoescape %}
12-
</script>
12+
</script>
13+
14+
<?php
15+
16+
echo '<script>
17+
{% autoescape false %}
18+
Everything will be outputted as is in this block
19+
{% endautoescape %}
20+
21+
{% autoescape %}
22+
{{ safe_value|raw }}
23+
{% endautoescape %}
24+
</script>';
25+
26+
echo "<script>
27+
{% autoescape false %}
28+
Everything will be $outputted as is in this {${$object->getBlock( SOME_FLAG | raw )}}
29+
{% endautoescape %}
30+
31+
{% autoescape %}
32+
{{ safe_value|raw }}
33+
{% endautoescape %}
34+
</script>
35+
";
36+
37+
echo <<<'EOD'
38+
<script>
39+
{% autoescape false %}
40+
Everything will be outputted as is in this block
41+
{% endautoescape %}
42+
43+
{% autoescape %}
44+
{{ safe_value|raw }}
45+
{% endautoescape %}
46+
</script>
47+
EOD;
48+
49+
echo <<<EOD
50+
<script>
51+
{% autoescape false %}
52+
Everything will be $outputted as is in this {$obj->blocks[SOME_FLAG | raw]->name}
53+
{% endautoescape %}
54+
EOD;
55+
echo <<<"EOD"
56+
{% autoescape %}
57+
{{ safe_value|raw }}
58+
{% endautoescape %}
59+
</script>
60+
EOD;

WordPressVIPMinimum/Tests/Security/TwigUnitTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ public function getWarningList() {
3434
return [
3535
5 => 1,
3636
10 => 1,
37+
17 => 1,
38+
22 => 1,
39+
27 => 1,
40+
32 => 1,
41+
39 => 1,
42+
44 => 1,
43+
51 => 1,
44+
57 => 1,
3745
];
3846
}
3947
}

0 commit comments

Comments
 (0)