Skip to content

Commit a1dc1b7

Browse files
authored
Merge pull request #257 from rebeccahum/rebecca/StripTagsSniff
Add StripTags sniff and amend rulesets and existing sniffs
2 parents e4bbbdb + 81254be commit a1dc1b7

File tree

8 files changed

+126
-14
lines changed

8 files changed

+126
-14
lines changed

WordPress-VIP-Go/ruleset.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,9 @@
147147
<type>warning</type>
148148
<severity>7</severity>
149149
</rule>
150-
<rule ref="WordPressVIPMinimum.VIP.RestrictedFunctions.strip_tags_strip_tags">
150+
<rule ref="WordPressVIPMinimum.Functions.StripTags">
151151
<type>warning</type>
152152
<severity>5</severity>
153-
<message>We recommend using wp_kses() or wp_kses_post() instead https://codex.wordpress.org/Function_Reference/wp_kses</message>
154153
</rule>
155154
<rule ref="WordPress.WP.PostsPerPage.posts_per_page_posts_per_page">
156155
<type>warning</type>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* WordPressVIPMinimum Coding Standard.
4+
*
5+
* @package VIPCS\WordPressVIPMinimum
6+
*/
7+
8+
namespace WordPressVIPMinimum\Sniffs\Functions;
9+
10+
use WordPress\AbstractFunctionParameterSniff;
11+
12+
/**
13+
* This sniff ensures proper tag stripping.
14+
*
15+
* @package VIPCS\WordPressVIPMinimum
16+
*
17+
* @since 0.4.0
18+
*/
19+
class StripTagsSniff extends AbstractFunctionParameterSniff {
20+
21+
/**
22+
* The group name for this group of functions.
23+
*
24+
* @var string
25+
*/
26+
protected $group_name = 'strip_functions';
27+
28+
/**
29+
* Functions this sniff is looking for.
30+
*
31+
* @var array The only requirement for this array is that the top level
32+
* array keys are the names of the functions you're looking for.
33+
* Other than that, the array can have arbitrary content
34+
* depending on your needs.
35+
*/
36+
protected $target_functions = [
37+
'strip_tags' => true,
38+
];
39+
40+
/**
41+
* Process the parameters of a matched function.
42+
*
43+
* @param int $stackPtr The position of the current token in the stack.
44+
* @param array $group_name The name of the group which was matched.
45+
* @param string $matched_content The token content (function name) which was matched.
46+
* @param array $parameters Array with information about the parameters.
47+
* @return int|void Integer stack pointer to skip forward or void to continue
48+
* normal file processing.
49+
*/
50+
public function process_parameters( $stackPtr, $group_name, $matched_content, $parameters ) {
51+
if ( 1 === count( $parameters ) ) {
52+
$this->phpcsFile->addWarning(
53+
sprintf( '`strip_tags()` does not strip CSS and JS in between the script and style tags. Use `wp_strip_all_tags()` to strip all tags.', $matched_content ),
54+
$stackPtr,
55+
'StripTagsOneParameter'
56+
);
57+
} elseif ( isset( $parameters[2] ) ) {
58+
$this->phpcsFile->addWarning(
59+
sprintf( '`strip_tags()` does not strip CSS and JS in between the script and style tags. Use `wp_kses()` instead to allow only the HTML you need.', $matched_content ),
60+
$stackPtr,
61+
'StripTagsTwoParameters'
62+
);
63+
}
64+
}
65+
}

WordPressVIPMinimum/Sniffs/VIP/RestrictedFunctionsSniff.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,6 @@ public function getGroups() {
7777
'attachment_url_to_postid',
7878
),
7979
),
80-
// TODO: Add strip_tags sniff that checks based on parameters.
81-
'strip_tags' => array(
82-
'type' => 'error',
83-
'message' => '`%s()` does not strip CSS and JS in between the script and style tags. `wp_strip_all_tags()` should be used instead.',
84-
'functions' => array(
85-
'strip_tags',
86-
),
87-
),
8880
'dbDelta' => array(
8981
'type' => 'error',
9082
'message' => 'All database modifications have to approved by the WordPress.com VIP team.',
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
$string = '<script>haxx0red</script>';
4+
$html = '<br><a><b><i>';
5+
6+
strip_tag( 'Test', $html ); // Ok - similarly-named function.
7+
wp_strip_all_tags( $string ); // Ok.
8+
9+
strip_tags( 'Testing' ); // Warning.
10+
strip_tags( 'Test', $html ); // Warning.
11+
strip_tags( 'Test' . ', ' . 'HTML' ); // Warning - concatenation on first parameter.
12+
strip_tags( 'Test, String', $html ); // Warning - comma in first parameter.
13+
strip_tags( $string ); // Warning.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Unit test class for WordPressVIPMinimum Coding Standard.
4+
*
5+
* @package VIPCS\WordPressVIPMinimum
6+
*/
7+
8+
namespace WordPressVIPMinimum\Tests\Functions;
9+
10+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
11+
12+
/**
13+
* Unit test class for the StripTags sniff.
14+
*
15+
* @package VIPCS\WordPressVIPMinimum
16+
*/
17+
class StripTagsUnitTest extends AbstractSniffUnitTest {
18+
19+
/**
20+
* Returns the lines where errors should occur.
21+
*
22+
* @return array <int line number> => <int number of errors>
23+
*/
24+
public function getErrorList() {
25+
return [];
26+
}
27+
28+
/**
29+
* Returns the lines where warnings should occur.
30+
*
31+
* @return array <int line number> => <int number of warnings>
32+
*/
33+
public function getWarningList() {
34+
return [
35+
9 => 1,
36+
10 => 1,
37+
11 => 1,
38+
12 => 1,
39+
13 => 1,
40+
];
41+
}
42+
43+
}

WordPressVIPMinimum/Tests/VIP/RestrictedFunctionsUnitTest.inc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ flush_rewrite_rules(); // Error.
4545
wpcom_vip_attachment_url_to_postid( $url ); // Ok - VIP recommended version of attachment_url_to_postid().
4646
attachment_url_to_postid( $url ); // Error.
4747

48-
wp_strip_tags( $test ); // Ok - VIP recommended version of strip_tags().
49-
wp_kses( $test_string, $allowed_html ); // Ok - VIP recommended version of strip_tags().
50-
strip_tags( $test_string ); // Error.
48+
49+
50+
5151

5252
db_delta(); // Ok - similarly-named function to dbDelta().
5353
dbDelta(); // Error.

WordPressVIPMinimum/Tests/VIP/RestrictedFunctionsUnitTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public function getErrorList() {
3636
40 => 1,
3737
43 => 1,
3838
46 => 1,
39-
50 => 1,
4039
53 => 1,
4140
56 => 1,
4241
59 => 1,

WordPressVIPMinimum/ruleset.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
<!-- VIP-Go: https://vip.wordpress.com/documentation/vip-go/code-review-blockers-warnings-notices/#remote-calls -->
116116
<rule ref="WordPress.WP.AlternativeFunctions">
117117
<exclude name="WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents"/>
118+
<exclude name="WordPress.WP.AlternativeFunctions.strip_tags_strip_tags"/>
118119
</rule>
119120
<!-- VIP recommends other functions -->
120121
<rule ref="WordPress.WP.AlternativeFunctions.curl">

0 commit comments

Comments
 (0)