Skip to content

Commit 4154808

Browse files
authored
Merge pull request #388 from Automattic/feature/387-escapingvoidreturnfunctions
EscapingVoidReturnFunctions: Fix docs and logic
2 parents c8ed37a + 402ac50 commit 4154808

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

WordPress-VIP-Go/ruleset-test.inc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,20 @@ update_site_option( $bar, $foo, true ); // Ok.
322322
// WordPressVIPMinimum.Functions.RestrictedFunctions.site_option_add_site_option
323323
add_site_option( 'foo', $bar ); // Ok.
324324

325+
// WordPressVIPMinimum.Security.EscapingVoidReturnFunctions.Found
326+
esc_js( _deprecated_argument() ); // Error.
327+
esc_js( _deprecated_constructor() ); // Error.
328+
esc_js( _deprecated_file() ); // Error.
329+
esc_js( _deprecated_function() ); // Error.
330+
esc_js( _deprecated_hook() ); // Error.
331+
esc_js( _doing_it_wrong() ); // Error.
332+
esc_html( _e( 'foo', 'bar' ) ); // Error.
333+
esc_html( _ex( 'foo', 'bar' ) ); // Error.
334+
esc_attr( printf( 'foo', [] ) ); // Error.
335+
esc_attr( trigger_error( 'foo' ) ); // Error (+ warning due to trigger_error() call).
336+
esc_attr( user_error( 'foo', '' ) ); // Error.
337+
esc_attr( vprintf( 'foo', [] ) ); // Error.
338+
esc_attr( wp_die( 'foo' ) ); // Error.
339+
esc_attr( wp_dropdown_pages() ); // Error.
340+
325341
?> <!-- closing PHP tag should be omitted -->

WordPress-VIP-Go/ruleset-test.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@
6262
268 => 1,
6363
270 => 1,
6464
271 => 1,
65+
326 => 1,
66+
327 => 1,
67+
328 => 1,
68+
329 => 1,
69+
330 => 1,
70+
331 => 1,
71+
332 => 1,
72+
333 => 1,
73+
334 => 1,
74+
335 => 1,
75+
336 => 1,
76+
337 => 1,
77+
338 => 1,
78+
339 => 1,
6579
],
6680
'warnings' => [
6781
84 => 1,
@@ -116,6 +130,7 @@
116130
277 => 1,
117131
281 => 1,
118132
285 => 1,
133+
335 => 1,
119134
],
120135
'messages' => [
121136
4 => [

WordPressVIPMinimum/Sniffs/Security/EscapingVoidReturnFunctionsSniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use PHP_CodeSniffer\Util\Tokens;
1313

1414
/**
15-
* Flag suspicious WP_Query and get_posts params.
15+
* Flag functions that don't return anything, yet are wrapped in an escaping function call.
16+
*
17+
* E.g. esc_html( _e( 'foo' ) );
1618
*
1719
* @package VIPCS\WordPressVIPMinimum
1820
*/
@@ -57,7 +59,7 @@ public function process_token( $stackPtr ) {
5759
return;
5860
}
5961

60-
if ( 0 === strpos( $this->tokens[ $next_token ]['content'], '_e' ) ) {
62+
if ( isset( $this->printingFunctions[ $this->tokens[ $next_token ]['content'] ] ) ) {
6163
$message = 'Attempting to escape `%s()` which is printing its output.';
6264
$data = [ $this->tokens[ $next_token ]['content'] ];
6365
$this->phpcsFile->addError( $message, $stackPtr, 'Found', $data );

WordPressVIPMinimum/ruleset-test.inc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,20 @@ update_site_option( $bar, $foo, true ); // Error.
188188
// WordPressVIPMinimum.Functions.RestrictedFunctions.site_option_delete_site_option
189189
delete_site_option( $foo ); // Error.
190190

191+
// WordPressVIPMinimum.Security.EscapingVoidReturnFunctions.Found
192+
esc_js( _deprecated_argument() ); // Error.
193+
esc_js( _deprecated_constructor() ); // Error.
194+
esc_js( _deprecated_file() ); // Error.
195+
esc_js( _deprecated_function() ); // Error.
196+
esc_js( _deprecated_hook() ); // Error.
197+
esc_js( _doing_it_wrong() ); // Error.
198+
esc_html( _e( 'foo', 'bar' ) ); // Error.
199+
esc_html( _ex( 'foo', 'bar' ) ); // Error.
200+
esc_attr( printf( 'foo', [] ) ); // Error.
201+
esc_attr( trigger_error( 'foo' ) ); // Error (+ warning due to trigger_error() call).
202+
esc_attr( user_error( 'foo', '' ) ); // Error.
203+
esc_attr( vprintf( 'foo', [] ) ); // Error.
204+
esc_attr( wp_die( 'foo' ) ); // Error.
205+
esc_attr( wp_dropdown_pages() ); // Error.
206+
191207
?> <!-- closing PHP tag should be omitted -->

WordPressVIPMinimum/ruleset-test.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@
4949
183 => 1,
5050
186 => 1,
5151
189 => 1,
52+
192 => 1,
53+
193 => 1,
54+
194 => 1,
55+
195 => 1,
56+
196 => 1,
57+
197 => 1,
58+
198 => 1,
59+
199 => 1,
60+
200 => 1,
61+
201 => 1,
62+
202 => 1,
63+
203 => 1,
64+
204 => 1,
65+
205 => 1,
5266
],
5367
'warnings' => [
5468
18 => 1,
@@ -75,6 +89,7 @@
7589
166 => 1,
7690
170 => 1,
7791
177 => 1,
92+
201 => 1,
7893
],
7994
'messages' => [
8095
123 => [

0 commit comments

Comments
 (0)