Skip to content

Commit efe8ce6

Browse files
committed
Security/Mustache: bug fix - potential false positives for delimiter change
The sniff didn't have enough guard code to safeguard that the test cases I've added in this commit would be handled correctly. Fixed now.
1 parent 221789c commit efe8ce6

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

WordPressVIPMinimum/Sniffs/Security/MustacheSniff.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,20 @@ public function process_token( $stackPtr ) {
5656
$this->phpcsFile->addWarning( $message, $stackPtr, 'VariableNotation' );
5757
}
5858

59-
if ( strpos( $this->tokens[ $stackPtr ]['content'], '{{=' ) !== false ) {
59+
$start_delimiter_change = strpos( $this->tokens[ $stackPtr ]['content'], '{{=' );
60+
if ( $start_delimiter_change !== false ) {
6061
// Mustache delimiter change.
61-
$new_delimiter = trim( str_replace( [ '{{=', '=}}' ], '', substr( $this->tokens[ $stackPtr ]['content'], 0, strpos( $this->tokens[ $stackPtr ]['content'], '=}}' ) + 3 ) ) );
62-
$message = 'Found Mustache delimiter change notation. New delimiter is: %s.';
63-
$data = [ $new_delimiter ];
64-
$this->phpcsFile->addWarning( $message, $stackPtr, 'DelimiterChange', $data );
62+
63+
$end_delimiter_change = strpos( $this->tokens[ $stackPtr ]['content'], '=}}' );
64+
if ( $end_delimiter_change !== false && $start_delimiter_change < $end_delimiter_change ) {
65+
$start_new_delimiter = $start_delimiter_change + 3;
66+
$end_new_delimiter = $end_delimiter_change - ( $start_delimiter_change + 3 );
67+
$new_delimiter = substr( $this->tokens[ $stackPtr ]['content'], $start_new_delimiter, $end_new_delimiter );
68+
69+
$message = 'Found Mustache delimiter change notation. New delimiter is: %s.';
70+
$data = [ $new_delimiter ];
71+
$this->phpcsFile->addWarning( $message, $stackPtr, 'DelimiterChange', $data );
72+
}
6573
}
6674

6775
if ( strpos( $this->tokens[ $stackPtr ]['content'], 'SafeString' ) !== false ) {

WordPressVIPMinimum/Tests/Security/MustacheUnitTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,10 @@ echo <<<'NOW'
4040
{{=<% %>=}} <!-- NOK: delimiter change -->
4141
</script>
4242
NOW;
43+
44+
// Don't throw false positives on incorrect/incomplete mustache delimiter change.
45+
$m = '{{=<%'; // Missing end of delimiter change.
46+
$m = '%>=}} {{=<%'; // Incorrect order, not a delimiter change.
47+
48+
// Correctly recognize mid-line delimiter change.
49+
$m = '{{default_tags}} {{=<% %>=}} <% erb_style_tags %> <%={{ }}=%> {{ default_tags_again }}'; // NOK: delimiter change.

WordPressVIPMinimum/Tests/Security/MustacheUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function getWarningList() {
4545
38 => 1,
4646
39 => 1,
4747
40 => 1,
48+
49 => 1,
4849
];
4950
}
5051
}

0 commit comments

Comments
 (0)