Skip to content

Commit bcab6ad

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

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,23 @@ public function getErrorList( $testFile = '' ) {
164164
672 => 1,
165165
673 => 1,
166166
678 => 1,
167+
694 => 1,
168+
699 => 1,
169+
700 => 1,
170+
701 => 1,
171+
707 => 1,
172+
708 => 1,
173+
709 => 1,
174+
714 => 1,
175+
722 => 1,
176+
724 => 1,
177+
725 => 1,
178+
726 => 1,
179+
732 => 1,
180+
733 => 1,
181+
734 => 1,
182+
735 => 1,
183+
744 => 1,
167184
);
168185

169186
case 'EscapeOutputUnitTest.6.inc':

0 commit comments

Comments
 (0)