-
-
Notifications
You must be signed in to change notification settings - Fork 493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding missing escaping check for <?=
#858
Changes from 4 commits
f8241bc
98df766
a31b188
3fcbdc7
37dfe31
e7d7477
fac73b2
73fd553
415a8b1
5b6e892
ba862f9
382e66f
2a912a5
dd1b6f0
0698ae1
a361850
b52d30c
fde244e
c8e48ec
60d3bf1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,13 +111,24 @@ class WordPress_Sniffs_XSS_EscapeOutputSniff extends WordPress_Sniff { | |
* @return array | ||
*/ | ||
public function register() { | ||
return array( | ||
$tokens = array( | ||
T_ECHO, | ||
T_PRINT, | ||
T_EXIT, | ||
T_STRING, | ||
); | ||
|
||
// We can successfully report on unescaped open_short_tags only if they are enabled. | ||
if ( true === (bool) ini_get( 'short_open_tag' ) ) { | ||
$tokens[] = T_OPEN_TAG_WITH_ECHO; | ||
} else { | ||
/** | ||
* In case open_short_tag are turned off, we can attempt to regex T_INLINE_HTML | ||
* which is how open_short_tags are being handled in that case. | ||
*/ | ||
$tokens[] = T_INLINE_HTML; | ||
} | ||
return $tokens; | ||
} | ||
|
||
/** | ||
|
@@ -152,6 +163,24 @@ public function process_token( $stackPtr ) { | |
if ( in_array( $function, array( 'trigger_error', 'user_error' ), true ) ) { | ||
$end_of_statement = $this->phpcsFile->findEndOfStatement( $open_paren + 1 ); | ||
} | ||
} else if ( false === (bool) ini_get( 'short_open_tag' ) && T_INLINE_HTML === $this->tokens[ $stackPtr ]['code'] ) { | ||
// Skip if no PHP short_open_tag is in the string. | ||
if ( false === strpos( $this->tokens[ $stackPtr ]['content'], '<?=' ) ) { | ||
return; | ||
} | ||
|
||
// Report on what very likely is a PHP short open tag outputing variable | ||
if ( preg_match( '/\<\?\=[\s\t]*(\$[a-zA-Z_][[:alnum:]_]+)[\s\t]*\;?[\s\t]*\?\>/', $this->tokens[ $stackPtr ]['content'], $matches ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Looks like the regex does not detect the echo-ing out of array based values, i.e. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very good call on the array based values. Outputting object properties might suffer from the same issue. I'll look into those cases later in the day. Thanks for pointing that out! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my suggestion below |
||
$this->phpcsFile->addError( "Expected next thing to be an escaping function, not %s.", $stackPtr, 'OutputNotEscaped', $matches[1] ); | ||
return; | ||
} | ||
|
||
// Throw warning in case the T_INLINE_HTML looks like a open_short_tag | ||
if ( false !== strpos( $this->tokens[$stackPtr]['content'], '<?=' ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a separate sniff for this, so no need to throw that warning here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jrfnl what Sniff do you have in mind, please? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both short open tags as well as alternative open tags are sniffed for in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jrfnl I'm happy to get rid of his Warning, but I have noticed that in PHP 5.3 with disabled short_open_tag the referenced sniff is not producing any warnings (as it's hitting the same issue with So as long as we already have something in place for checking on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just checked and you're right. I thought I'd pulled that upstream, but seems I only included that logic when I pulled the So, yes. let's leave the warning in place for now until the upstream sniff has been improved. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Upstream issue opened: squizlabs/PHP_CodeSniffer#1398 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've opened an upstream PR to fix the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh and before I forget: the upstream PR was merged and will be included in the next version of PHPCS, so the additional message can be removed once we up the minimum PHPCS version. |
||
$this->phpcsFile->addWarning( 'Possible use of PHP short open tag ( "<?=" ) detected. Needs manual inspection.', $stackPtr, 'PossibleShortOpenTag' ); | ||
return; | ||
} | ||
return; | ||
} | ||
|
||
// Checking for the ignore comment, ex: //xss ok. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@david-binda This is the issue for PHP 5.4+ as the
T_OPEN_TAG_WITH_ECHO
is supported independently of theshort_open_tag
setting from PHP 5.4+ upwards, so basically, leave the token in the base array and only use theelse
clause of this part with the reversed condition and you should be fine.See changelog here: http://php.net/manual/en/language.basic-syntax.phptags.php