Skip to content

Commit fd041e4

Browse files
authored
Merge pull request #854 from Automattic/feature/functions-striptags-sniff-review
2 parents bef6b9a + 914a244 commit fd041e4

File tree

3 files changed

+105
-17
lines changed

3 files changed

+105
-17
lines changed

WordPressVIPMinimum/Sniffs/Functions/StripTagsSniff.php

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace WordPressVIPMinimum\Sniffs\Functions;
1111

12+
use PHPCSUtils\Utils\PassedParameters;
1213
use WordPressCS\WordPress\AbstractFunctionParameterSniff;
1314

1415
/**
@@ -43,16 +44,60 @@ class StripTagsSniff extends AbstractFunctionParameterSniff {
4344
* in lowercase.
4445
* @param array $parameters Array with information about the parameters.
4546
*
46-
* @return int|void Integer stack pointer to skip forward or void to continue
47-
* normal file processing.
47+
* @return void
4848
*/
4949
public function process_parameters( $stackPtr, $group_name, $matched_content, $parameters ) {
50-
if ( count( $parameters ) === 1 ) {
51-
$message = '`strip_tags()` does not strip CSS and JS in between the script and style tags. Use `wp_strip_all_tags()` to strip all tags.';
52-
$this->phpcsFile->addWarning( $message, $stackPtr, 'StripTagsOneParameter' );
53-
} elseif ( isset( $parameters[2] ) ) {
50+
$string_param = PassedParameters::getParameterFromStack( $parameters, 1, 'string' );
51+
$allowed_tags_param = PassedParameters::getParameterFromStack( $parameters, 2, 'allowed_tags' );
52+
53+
if ( $string_param !== false && $allowed_tags_param === false ) {
54+
$this->add_warning( $stackPtr, 'StripTagsOneParameter' );
55+
} elseif ( $allowed_tags_param !== false ) {
5456
$message = '`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.';
5557
$this->phpcsFile->addWarning( $message, $stackPtr, 'StripTagsTwoParameters' );
58+
} else {
59+
$this->add_warning( $stackPtr );
5660
}
5761
}
62+
63+
/**
64+
* Process the function if no parameters were found.
65+
*
66+
* @param int $stackPtr The position of the current token in the stack.
67+
* @param string $group_name The name of the group which was matched.
68+
* @param string $matched_content The token content (function name) which was matched
69+
* in lowercase.
70+
*
71+
* @return void
72+
*/
73+
public function process_no_parameters( $stackPtr, $group_name, $matched_content ) {
74+
$this->add_warning( $stackPtr );
75+
}
76+
77+
/**
78+
* Process the function if it is used as a first class callable.
79+
*
80+
* @param int $stackPtr The position of the current token in the stack.
81+
* @param string $group_name The name of the group which was matched.
82+
* @param string $matched_content The token content (function name) which was matched
83+
* in lowercase.
84+
*
85+
* @return void
86+
*/
87+
public function process_first_class_callable( $stackPtr, $group_name, $matched_content ) {
88+
$this->add_warning( $stackPtr );
89+
}
90+
91+
/**
92+
* Add a warning if the function is used at all.
93+
*
94+
* @param int $stackPtr The position of the current token in the stack.
95+
* @param string $error_code Error code to use for the warning.
96+
*
97+
* @return void
98+
*/
99+
private function add_warning( $stackPtr, $error_code = 'Used' ) {
100+
$message = '`strip_tags()` does not strip CSS and JS in between the script and style tags. Use `wp_strip_all_tags()` to strip all tags.';
101+
$this->phpcsFile->addWarning( $message, $stackPtr, $error_code );
102+
}
58103
}
Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,48 @@
11
<?php
22

3-
$string = '<script>haxx0red</script>';
4-
$html = '<br><a><b><i>';
3+
/*
4+
* Not the sniff target.
5+
*/
6+
use strip_tags;
7+
use MyNs\{
8+
function strip_tags,
9+
};
10+
11+
my\ns\strip_tags($a, $b);
12+
$this->strip_tags($a, $b);
13+
$this?->strip_tags($a, $b);
14+
MyClass::strip_tags($a, $b);
15+
echo STRIP_TAGS;
16+
namespace\strip_tags($a, $b);
17+
18+
// Looks like a function call, but is a PHP 8.0+ class instantiation via an attribute.
19+
#[Strip_tags('text')]
20+
function foo() {}
521

622
strip_tag( 'Test', $html ); // Ok - similarly-named function.
723
wp_strip_all_tags( $string ); // Ok.
824

25+
26+
/*
27+
* These should all be flagged with a warning.
28+
*/
929
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.
30+
STRIP_TAGS( 'Test', $html ); // Warning.
31+
\strip_tags( 'Test' . ', ' . 'HTML', ); // Warning.
32+
strip_tags( 'Test, String', $html, ); // Warning.
33+
Strip_Tags( $string ); // Warning.
34+
35+
// The function should always be flagged, even during live coding (missing required parameter).
36+
strip_tags();
37+
38+
strip_tags(...$params); // PHP 5.6 argument unpacking.
39+
40+
// Safeguard correct handling of function calls using PHP 8.0+ named parameters.
41+
strip_tags(allowed_tags: $allowed, string: $html ); // Warning.
42+
strip_tags(string: $html); // Warning.
43+
strip_tags(allowed_tags: $allowed); // Warning. Invalid function call, but that's not the concern of this sniff.
44+
45+
\strip_tags(string: $html, allowed_tag: $allowed); // Warning (mind: deliberate typo in param name).
46+
strip_tags(html: $html); // Warning (mind: deliberately using incorrect param name).
47+
48+
add_action('my_action', strip_tags(...)); // PHP 8.1 first class callable.

WordPressVIPMinimum/Tests/Functions/StripTagsUnitTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,19 @@ public function getErrorList() {
3232
*/
3333
public function getWarningList() {
3434
return [
35-
9 => 1,
36-
10 => 1,
37-
11 => 1,
38-
12 => 1,
39-
13 => 1,
35+
29 => 1,
36+
30 => 1,
37+
31 => 1,
38+
32 => 1,
39+
33 => 1,
40+
36 => 1,
41+
38 => 1,
42+
41 => 1,
43+
42 => 1,
44+
43 => 1,
45+
45 => 1,
46+
46 => 1,
47+
48 => 1,
4048
];
4149
}
4250
}

0 commit comments

Comments
 (0)