-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathAddHtmlEscaperToOutput.php
186 lines (154 loc) · 4.99 KB
/
AddHtmlEscaperToOutput.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* Copyright 2021 Adobe
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento2\Rector\Src;
use PhpParser\Node;
use PhpParser\Node\Stmt\Echo_;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
class AddHtmlEscaperToOutput extends AbstractRector
{
/**
* @var string[]
*/
private $_phpFunctionsToIgnore = ['\count','\strip_tags'];
/**
* @inheritDoc
*/
public function getNodeTypes(): array
{
return [Echo_::class];
}
/**
* @inheritDoc
*/
public function refactor(Node $node): ?Node
{
$echoContent = $node->exprs[0];
if ($echoContent instanceof Node\Expr\Ternary) {
if (!$this->hasNoEscapeAttribute($echoContent->if) && $this->canEscapeOutput($echoContent->if)) {
$node->exprs[0]->if = new Node\Expr\MethodCall(
new Node\Expr\Variable('escaper'),
'escapeHtml',
[new Node\Arg($echoContent->if)]
);
}
if (!$this->hasNoEscapeAttribute($echoContent->else) && $this->canEscapeOutput($echoContent->else)) {
$node->exprs[0]->else = new Node\Expr\MethodCall(
new Node\Expr\Variable('escaper'),
'escapeHtml',
[new Node\Arg($echoContent->else)]
);
}
return $node;
}
if ($this->hasNoEscapeAttribute($echoContent)) {
return null;
}
if ($this->canEscapeOutput($echoContent)) {
$node->exprs[0] = new Node\Expr\MethodCall(
new Node\Expr\Variable('escaper'),
'escapeHtml',
[new Node\Arg($echoContent)]
);
return $node;
}
return null;
}
/**
* We check if content of the echo should be escaped
*
* @param Node $echoContent
* @return bool
*/
private function canEscapeOutput(Node $echoContent):bool
{
if ($echoContent instanceof Node\Expr\Variable) {
return true;
}
if ($echoContent instanceof Node\Expr\FuncCall
&& !$this->willFunctionReturnSafeOutput($echoContent)) {
// if string passed to __() contains html don't do anthing
if ($echoContent->name == '__' && $this->stringContainsHtml($echoContent->args[0]->value->value)) {
return false;
}
return true;
}
// if the echo already has escapeHtml called on $block or escaper, don't do anthing
if ($echoContent instanceof Node\Expr\MethodCall &&
!$this->methodReturnsValidHtmlOrUrl($echoContent)
) {
// if the method is part of secureRenderer don't do anything
if ($echoContent->var->name === 'secureRenderer') {
return false;
}
return true;
}
return false;
}
/**
* We do not want to escape __() output if the inner content contains html
*
* @param string $str
* @return bool
*/
private function stringContainsHtml(string $str)
{
return strlen($str) !== strlen(strip_tags($str));
}
/**
* If the developer has marked the output as noEscape by using the @noEscape we want to leave that code as it is
*
* @param Node $echoContent
*/
private function hasNoEscapeAttribute(Node $echoContent):bool
{
$comments = $echoContent->getComments();
foreach ($comments as $comment) {
if (stripos($comment->getText(), '@noEscape') !== false) {
return true;
}
}
return false;
}
/**
* If method contains the keyword HTML we assume developer intends to output html
*
* @param Node\Expr\MethodCall $echoContent
*/
private function methodReturnsValidHtmlOrUrl(Node\Expr\MethodCall $echoContent):bool
{
return stripos($echoContent->name->name, 'Html') !== false
|| stripos($echoContent->name->name, 'Url') !== false;
}
/**
* Some php function return safe output. count, strip_tags need not be escaped.
*
* @param Node\Expr\FuncCall $funcNode
*/
private function willFunctionReturnSafeOutput(Node\Expr\FuncCall $funcNode):bool
{
//@TODO did not handle things like $callback();
$funcName = $funcNode->name->toCodeString();
return in_array($funcName, $this->_phpFunctionsToIgnore);
}
/**
* @inheritDoc
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Add escaper methods like escapeHtml to html output',
[
new CodeSample(
'echo $productName',
'echo $escaper->escapeHtml($productName)'
),
]
);
}
}