Skip to content

Commit

Permalink
ProperEscapingFunction: Fix short tag detection
Browse files Browse the repository at this point in the history
The tracking variable `$in_short_echo` was never reset when checking different files, meaning that the property would carry over and provide the wrong context to the next file.

By adding a `process()` method to the ProperEscapingFunctionSniff, we can reset the tracking variable at the start of each file by comparing the currently processing file to the last one stored in a static variable.

Includes two unit test files, numbered in the order needed to trigger the bug if the fix wasn't present.

Fixes #739.
  • Loading branch information
GaryJones committed Feb 5, 2023
1 parent 3c5a8bb commit db63bd8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace WordPressVIPMinimum\Sniffs\Security;

use PHP_CodeSniffer\Files\File;
use WordPressVIPMinimum\Sniffs\Sniff;
use PHP_CodeSniffer\Util\Tokens;

Expand Down Expand Up @@ -111,6 +112,28 @@ public function register() {
];
}

/**
* Reset short echo context tracking variable for a new file.
*
* @since 2.3.4
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function process( File $phpcsFile, $stackPtr ) {
static $current_file;
if ( $phpcsFile !== $current_file ) {
$this->in_short_echo = false;
$current_file = $phpcsFile;
}

return parent::process( $phpcsFile, $stackPtr );
}

/**
* Process this test when one of its tokens is encountered
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
/*
* This is part one of a two-part test. It must be in a lower-numbered file
* than part two, to trigger the bug in
* https://github.com/Automattic/VIP-Coding-Standards/issues/739
*/
?>
<?= esc_attr('short_tag') ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/*
* This is part two of a two-part test. It must be in a higher-numbered file
* than part one, to trigger the bug in
* https://github.com/Automattic/VIP-Coding-Standards/issues/739
*/
printf(
'<div class="%1$s"><p>%2$s</p></div>',
esc_attr($class),
wp_kses_post($message)
);

0 comments on commit db63bd8

Please sign in to comment.