Skip to content

Commit 17875ff

Browse files
authored
Merge pull request #2605 from rodrigoprimo/escape-output-fix-namespaced-class-detection
Security/EscapeOutput: fix namespaced ::class detection
2 parents d3a45da + 5b28561 commit 17875ff

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

WordPress/Sniffs/Security/EscapeOutputSniff.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,13 @@ protected function check_code_is_escaped( $start, $end, $code = 'OutputNotEscape
615615
if ( \T_STRING === $this->tokens[ $i ]['code']
616616
|| \T_VARIABLE === $this->tokens[ $i ]['code']
617617
|| isset( Collections::ooHierarchyKeywords()[ $this->tokens[ $i ]['code'] ] )
618+
|| \T_NAMESPACE === $this->tokens[ $i ]['code']
618619
) {
619-
$double_colon = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $i + 1 ), $end, true );
620+
$skip_tokens = Tokens::$emptyTokens;
621+
$skip_tokens[ \T_STRING ] = \T_STRING;
622+
$skip_tokens[ \T_NS_SEPARATOR ] = \T_NS_SEPARATOR;
623+
624+
$double_colon = $this->phpcsFile->findNext( $skip_tokens, ( $i + 1 ), $end, true );
620625
if ( false !== $double_colon
621626
&& \T_DOUBLE_COLON === $this->tokens[ $double_colon ]['code']
622627
) {

WordPress/Tests/Security/EscapeOutputUnitTest.1.inc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ _deprecated_function( __METHOD__, 'x.x.x', ClassName::class ); // OK.
538538
die( self::CLASS . ' has been abandoned' ); // OK.
539539
_deprecated_function( __METHOD__, 'x.x.x', parent::Class ); // OK.
540540
_deprecated_function( __METHOD__, 'x.x.x', static::class ); // OK.
541-
echo 'Do not use ' . $object::class ); // OK.
541+
echo 'Do not use ' . $object::class; // OK.
542542

543543
/*
544544
* Examine the parameters passed for exception creation via throw.
@@ -676,3 +676,14 @@ throw new
676676
/* some comment */
677677
#[Attribute2('text', 10)]
678678
readonly class( $unescaped ) {}; // Bad.
679+
680+
/*
681+
* Safeguard correct handling of all types of namespaced function calls when *::class is used.
682+
*
683+
* Note: using ::class on fully qualified or namespace relative class names doesn't provide
684+
* any real value in practice.
685+
*/
686+
_deprecated_function( __METHOD__, 'x.x.x', \ClassName::class ); // OK.
687+
die( \MyNamespace\ClassName::class . ' has been abandoned' ); // OK.
688+
echo 'Do not use ' . MyNamespace\ClassName::class; // OK.
689+
_deprecated_function( __METHOD__, 'x.x.x', namespace\ClassName::class ); // OK.

0 commit comments

Comments
 (0)