Skip to content

Commit 490c471

Browse files
committed
Security/EscapeOutput: add tests for namespaced names
1 parent 93c8284 commit 490c471

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

WordPress/Tests/Security/EscapeOutputUnitTest.1.inc

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ echo esc_html_x( $some_nasty_var, 'context' ); // Ok.
258258
<input type="hidden" name="some-action" value="<?php echo esc_attr_x( 'none', 'context' ); ?>" /><!-- OK. -->
259259
<?php
260260

261-
echo PHP_VERSION_ID, PHP_VERSION, PHP_EOL, PHP_EXTRA_VERSION; // OK.
261+
echo PHP_VERSION_ID, PHP_VERSION, \PHP_EOL, PHP_EXTRA_VERSION; // OK.
262262

263263
trigger_error( 'DEBUG INFO - ' . __METHOD__ . '::internal_domains: domain = ' . $domain ); // Bad.
264264
Trigger_ERROR( $domain ); // Bad.
@@ -661,7 +661,7 @@ exit( status: esc_html( $foo ) ); // Ok.
661661
die( status: esc_html( $foo ) ); // Ok.
662662

663663
exit( status: $foo ); // Bad.
664-
die( status: $foo ); // Bad.
664+
\die( status: $foo ); // Bad.
665665

666666
/*
667667
* Issue https://github.com/WordPress/WordPress-Coding-Standards/issues/2552
@@ -687,3 +687,58 @@ _deprecated_function( __METHOD__, 'x.x.x', \ClassName::class ); // OK.
687687
die( \MyNamespace\ClassName::class . ' has been abandoned' ); // OK.
688688
echo 'Do not use ' . MyNamespace\ClassName::class; // OK.
689689
_deprecated_function( __METHOD__, 'x.x.x', namespace\ClassName::class ); // OK.
690+
691+
/*
692+
* Safeguard correct handling of all types of namespaced escaping and printing function calls.
693+
*/
694+
\printf( 'Hello %s', $foo ); // Bad.
695+
MyNamespace\printf( 'Hello %s', $foo ); // Ok.
696+
\MyNamespace\printf( 'Hello %s', $foo ); // Ok.
697+
namespace\printf( 'Hello %s', $foo ); // Ok.
698+
\printf( 'Hello %s', \esc_html( $foo ) ); // Ok.
699+
\printf( 'Hello %s', MyNamespace\esc_html( $foo ) ); // Bad.
700+
\printf( 'Hello %s', \MyNamespace\esc_html( $foo ) ); // Bad.
701+
\printf( 'Hello %s', namespace\esc_html( $foo ) ); // Bad.
702+
703+
/*
704+
* Safeguard correct handling of namespaced auto-escaped functions.
705+
*/
706+
echo \bloginfo( $var ); // Ok.
707+
echo MyNamespace\bloginfo( $var ); // Bad.
708+
echo \MyNamespace\bloginfo( $var ); // Bad.
709+
echo namespace\bloginfo( $var ); // Bad.
710+
711+
/*
712+
* Safeguard correct handling of namespaced unsafe printing functions.
713+
*/
714+
\_e( $text, 'my-domain' ); // Bad.
715+
MyNamespace\_e( $text, 'my-domain' ); // Ok.
716+
\MyNamespace\_e( $text, 'my-domain' ); // Ok.
717+
namespace\_e( $text, 'my-domain' ); // Ok.
718+
719+
/*
720+
* Safeguard correct handling of namespaced formatting functions.
721+
*/
722+
echo \sprintf( '%s', $var ); // Bad.
723+
echo \sprintf( '%s', esc_html( $var ) ); // Ok.
724+
echo MyNamespace\sprintf( '%s', esc_html( $var ) ); // Bad.
725+
echo \MyNamespace\sprintf( '%s', esc_html( $var ) ); // Bad.
726+
echo namespace\sprintf( '%s', esc_html( $var ) ); // Bad.
727+
728+
/*
729+
* Safeguard correct handling of get_search_query() as the sniff has special logic to check the $escaped parameter.
730+
*/
731+
echo \get_search_query( true ); // Ok.
732+
echo \get_search_query( false ); // Bad.
733+
echo MyNamespace\get_search_query( true ); // Bad.
734+
echo \MyNamespace\get_search_query( true ); // Bad.
735+
echo namespace\get_search_query( true ); // Bad.
736+
737+
/*
738+
* Safeguard correct handling of fully qualified functions with special parameter handling.
739+
* These should still be recognized as WordPress functions and use their special logic.
740+
*/
741+
\trigger_error( 'This is fine' ); // Ok.
742+
\trigger_error( error_level: E_USER_NOTICE ); // Ok from the sniff perspective (required $message parameter missing, but that's not our concern)
743+
\trigger_error( esc_html( $message ) ); // Ok.
744+
\trigger_error( $message ); // Bad.

WordPress/Tests/Security/EscapeOutputUnitTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace WordPressCS\WordPress\Tests\Security;
1111

1212
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
13+
use PHPCSUtils\BackCompat\Helper;
1314

1415
/**
1516
* Unit test class for the EscapeOutput sniff.
@@ -37,6 +38,8 @@ final class EscapeOutputUnitTest extends AbstractSniffUnitTest {
3738
public function getErrorList( $testFile = '' ) {
3839
switch ( $testFile ) {
3940
case 'EscapeOutputUnitTest.1.inc':
41+
$phpcs_version = Helper::getVersion();
42+
4043
return array(
4144
17 => 1,
4245
19 => 1,
@@ -160,10 +163,29 @@ public function getErrorList( $testFile = '' ) {
160163
655 => 1,
161164
657 => 1,
162165
663 => 1,
163-
664 => 1,
166+
// PHPCS 3.13.3 changed the tokenization of FQN exit/die it impacts directly how this test case
167+
// behaves (see https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1201).
168+
664 => version_compare( $phpcs_version, '3.13.3', '>=' ) ? 1 : 0,
164169
672 => 1,
165170
673 => 1,
166171
678 => 1,
172+
694 => 1,
173+
699 => 1,
174+
700 => 1,
175+
701 => 1,
176+
707 => 1,
177+
708 => 1,
178+
709 => 1,
179+
714 => 1,
180+
722 => 1,
181+
724 => 1,
182+
725 => 1,
183+
726 => 1,
184+
732 => 1,
185+
733 => 1,
186+
734 => 1,
187+
735 => 1,
188+
744 => 1,
167189
);
168190

169191
case 'EscapeOutputUnitTest.6.inc':

0 commit comments

Comments
 (0)