Skip to content

Commit 27701fd

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

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

WordPress/Tests/Security/EscapeOutputUnitTest.1.inc

Lines changed: 67 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,68 @@ _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\wp_die( $message ); // Ok.
696+
\MyNamespace\vprintf( 'Hello %s', array( $foo ) ); // Ok.
697+
namespace\wp_dropdown_pages( $args ); // Ok. The sniff should start flagging this once it can resolve relative namespaces.
698+
namespace\Sub\_deprecated_function( __FUNCTION__, '1.3.0', $another_func ); // Ok.
699+
\printf( 'Hello %s', \esc_html( $foo ) ); // Ok.
700+
\wp_die( MyNamespace\number_format( $foo ) ); // Bad.
701+
\vprintf( 'Hello %s', array( \MyNamespace\sanitize_user_field( $foo ) ) ); // Bad.
702+
\wp_dropdown_pages( namespace\sanitize_key( $foo ) ); // Bad. The sniff should stop flagging this once it can resolve relative namespaces.
703+
\_deprecated_function( __FUNCTION__, '1.3.0', namespace\Sub\wp_kses( $another_func ) ); // Bad.
704+
705+
/*
706+
* Safeguard correct handling of namespaced auto-escaped functions.
707+
*/
708+
echo \bloginfo( $var ); // Ok.
709+
echo MyNamespace\count( $var ); // Bad.
710+
echo \MyNamespace\get_archives_link( $url, 'link' ); // Bad.
711+
echo namespace\get_search_form(); // Bad. The sniff should stop flagging this once it can resolve relative namespaces.
712+
echo namespace\Sub\the_author(); // Bad.
713+
714+
/*
715+
* Safeguard correct handling of namespaced unsafe printing functions.
716+
*/
717+
\_e( $text, 'my-domain' ); // Bad.
718+
MyNamespace\_ex( $text, 'context' ); // Ok.
719+
\MyNamespace\_e( $text, 'my-domain' ); // Ok.
720+
namespace\_ex( $text, 'context' ); // Ok. The sniff should start flagging this once it can resolve relative namespaces.
721+
namespace\Sub\_e( $text, 'my-domain' ); // Ok.
722+
723+
/*
724+
* Safeguard correct handling of namespaced formatting functions.
725+
*/
726+
echo \sprintf( '%s', $var ); // Bad.
727+
echo \sprintf( '%s', esc_html( $var ) ); // Ok.
728+
echo MyNamespace\antispambot( esc_html( $email ) ); // Bad.
729+
echo \MyNamespace\ent2ncr( esc_html( $_data ) ); // Bad.
730+
echo namespace\vsprintf( 'Hello %s', array( esc_html( $foo ) ) ); // Bad. The sniff should stop flagging this once it can resolve relative namespaces.
731+
echo namespace\Sub\wp_sprintf( 'Hello %s', array( esc_html( $foo ) ) ); // Bad.
732+
733+
/*
734+
* Safeguard correct handling of get_search_query() as the sniff has special logic to check the $escaped parameter.
735+
*/
736+
echo \get_search_query( true ); // Ok.
737+
echo \get_search_query( false ); // Bad.
738+
echo MyNamespace\get_search_query( true ); // Bad.
739+
echo \MyNamespace\get_search_query( true ); // Bad.
740+
echo namespace\get_search_query( true ); // Bad. The sniff should stop flagging this once it can resolve relative namespaces.
741+
echo namespace\Sub\get_search_query( true ); // Bad.
742+
743+
/*
744+
* Safeguard correct handling of fully qualified and namespace relative functions with special parameter handling.
745+
*/
746+
\trigger_error( esc_html( $message ), $second_param_should_be_ignored ); // Ok.
747+
\User_Error( $message ); // Bad.
748+
namespace\trigger_error( $message ); // Ok. The sniff should start flagging this once it can resolve relative namespaces.
749+
namespace\Sub\user_error( $message ); // Ok.
750+
\_deprecated_file( basename( __FILE__ ), '1.3.0' ); // Ok.
751+
\_deprecated_file( $file, '1.3.0' ); // Error.
752+
namespace\_deprecated_file( basename( __FILE__ ), '1.3.0' ); // Ok.
753+
namespace\_DEPRECATED_FILE( $file, '1.3.0' ); // Ok. The sniff should start flagging this once it can resolve relative namespaces.
754+
namespace\Sub\_deprecated_file( $file, '1.3.0' ); // Ok.

WordPress/Tests/Security/EscapeOutputUnitTest.php

Lines changed: 28 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,34 @@ 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+
700 => 1,
174+
701 => 1,
175+
702 => 1,
176+
703 => 1,
177+
709 => 1,
178+
710 => 1,
179+
711 => 1,
180+
712 => 1,
181+
717 => 1,
182+
726 => 1,
183+
728 => 1,
184+
729 => 1,
185+
730 => 1,
186+
731 => 1,
187+
737 => 1,
188+
738 => 1,
189+
739 => 1,
190+
740 => 1,
191+
741 => 1,
192+
747 => 1,
193+
751 => 1,
167194
);
168195

169196
case 'EscapeOutputUnitTest.6.inc':

0 commit comments

Comments
 (0)