Skip to content

Commit cfc853a

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 daf17f0 commit cfc853a

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

WordPressVIPMinimum/Sniffs/Security/MustacheSniff.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,19 @@ 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+
$end_delimiter_change = strpos( $this->tokens[ $stackPtr ]['content'], '=}}' );
63+
if ( $end_delimiter_change !== false && $start_delimiter_change < $end_delimiter_change ) {
64+
$start_new_delimiter = $start_delimiter_change + 3;
65+
$end_new_delimiter = $end_delimiter_change - ( $start_delimiter_change + 3 );
66+
$new_delimiter = substr( $this->tokens[ $stackPtr ]['content'], $start_new_delimiter, $end_new_delimiter );
67+
68+
$message = 'Found Mustache delimiter change notation. New delimiter is: %s.';
69+
$data = [ $new_delimiter ];
70+
$this->phpcsFile->addWarning( $message, $stackPtr, 'DelimiterChange', $data );
71+
}
6572
}
6673

6774
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
@@ -42,3 +42,10 @@ echo <<<'NOW'
4242
{{=<% %>=}} <!-- NOK: delimiter change -->
4343
</script>
4444
NOW;
45+
46+
// Don't throw false positives on incorrect/incomplete mustache delimiter change.
47+
$m = '{{=<%'; // Missing end of delimiter change.
48+
$m = '%>=}} {{=<%'; // Incorrect order, not a delimiter change.
49+
50+
// Correctly recognize mid-line delimiter change.
51+
$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
40 => 1,
4646
41 => 1,
4747
42 => 1,
48+
51 => 1,
4849
];
4950
}
5051
}

0 commit comments

Comments
 (0)