|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Joas Schilling <[email protected]> |
|
4
|
|
|
* @author Morris Jobke <[email protected]> |
|
5
|
|
|
* @author Thomas Müller <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|
8
|
|
|
* @license AGPL-3.0 |
|
9
|
|
|
* |
|
10
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
12
|
|
|
* as published by the Free Software Foundation. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OC\App\CodeChecker; |
|
25
|
|
|
|
|
26
|
|
|
use PhpParser\Node; |
|
27
|
|
|
use PhpParser\Node\Name; |
|
28
|
|
|
use PhpParser\NodeVisitorAbstract; |
|
29
|
|
|
|
|
30
|
|
|
class NodeVisitor extends NodeVisitorAbstract { |
|
31
|
|
|
/** @var ICheck */ |
|
32
|
|
|
protected $list; |
|
33
|
|
|
|
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
protected $blackListDescription; |
|
36
|
|
|
/** @var string[] */ |
|
37
|
|
|
protected $blackListedClassNames; |
|
38
|
|
|
/** @var string[] */ |
|
39
|
|
|
protected $blackListedConstants; |
|
40
|
|
|
/** @var string[] */ |
|
41
|
|
|
protected $blackListedFunctions; |
|
42
|
|
|
/** @var string[] */ |
|
43
|
|
|
protected $blackListedMethods; |
|
44
|
|
|
/** @var bool */ |
|
45
|
|
|
protected $checkEqualOperatorUsage; |
|
46
|
|
|
/** @var string[] */ |
|
47
|
|
|
protected $errorMessages; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param ICheck $list |
|
51
|
|
|
*/ |
|
52
|
45 |
|
public function __construct(ICheck $list) { |
|
53
|
45 |
|
$this->list = $list; |
|
54
|
|
|
|
|
55
|
45 |
|
$this->blackListedClassNames = []; |
|
56
|
45 |
|
foreach ($list->getClasses() as $class => $blackListInfo) { |
|
57
|
32 |
|
if (is_numeric($class) && is_string($blackListInfo)) { |
|
58
|
|
|
$class = $blackListInfo; |
|
59
|
|
|
$blackListInfo = null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
32 |
|
$class = strtolower($class); |
|
63
|
32 |
|
$this->blackListedClassNames[$class] = $class; |
|
64
|
45 |
|
} |
|
65
|
|
|
|
|
66
|
45 |
|
$this->blackListedConstants = []; |
|
67
|
45 |
|
foreach ($list->getConstants() as $constantName => $blackListInfo) { |
|
68
|
25 |
|
$constantName = strtolower($constantName); |
|
69
|
25 |
|
$this->blackListedConstants[$constantName] = $constantName; |
|
70
|
45 |
|
} |
|
71
|
|
|
|
|
72
|
45 |
|
$this->blackListedFunctions = []; |
|
73
|
45 |
|
foreach ($list->getFunctions() as $functionName => $blackListInfo) { |
|
74
|
25 |
|
$functionName = strtolower($functionName); |
|
75
|
25 |
|
$this->blackListedFunctions[$functionName] = $functionName; |
|
76
|
45 |
|
} |
|
77
|
|
|
|
|
78
|
45 |
|
$this->blackListedMethods = []; |
|
79
|
45 |
|
foreach ($list->getMethods() as $functionName => $blackListInfo) { |
|
80
|
25 |
|
$functionName = strtolower($functionName); |
|
81
|
25 |
|
$this->blackListedMethods[$functionName] = $functionName; |
|
82
|
45 |
|
} |
|
83
|
|
|
|
|
84
|
45 |
|
$this->checkEqualOperatorUsage = $list->checkStrongComparisons(); |
|
85
|
|
|
|
|
86
|
45 |
|
$this->errorMessages = [ |
|
|
|
|
|
|
87
|
45 |
|
CodeChecker::CLASS_EXTENDS_NOT_ALLOWED => "%s class must not be extended", |
|
88
|
45 |
|
CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED => "%s interface must not be implemented", |
|
89
|
45 |
|
CodeChecker::STATIC_CALL_NOT_ALLOWED => "Static method of %s class must not be called", |
|
90
|
45 |
|
CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED => "Constant of %s class must not not be fetched", |
|
91
|
45 |
|
CodeChecker::CLASS_NEW_NOT_ALLOWED => "%s class must not be instantiated", |
|
92
|
45 |
|
CodeChecker::CLASS_USE_NOT_ALLOWED => "%s class must not be imported with a use statement", |
|
93
|
45 |
|
CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED => "Method of %s class must not be called", |
|
94
|
|
|
|
|
95
|
45 |
|
CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED => "is discouraged", |
|
96
|
|
|
]; |
|
97
|
45 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** @var array */ |
|
100
|
|
|
public $errors = []; |
|
101
|
|
|
|
|
102
|
45 |
|
public function enterNode(Node $node) { |
|
103
|
45 |
View Code Duplication |
if ($this->checkEqualOperatorUsage && $node instanceof Node\Expr\BinaryOp\Equal) { |
|
|
|
|
|
|
104
|
1 |
|
$this->errors[]= [ |
|
105
|
1 |
|
'disallowedToken' => '==', |
|
106
|
1 |
|
'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED, |
|
107
|
1 |
|
'line' => $node->getLine(), |
|
108
|
1 |
|
'reason' => $this->buildReason('==', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED) |
|
109
|
1 |
|
]; |
|
110
|
1 |
|
} |
|
111
|
45 |
View Code Duplication |
if ($this->checkEqualOperatorUsage && $node instanceof Node\Expr\BinaryOp\NotEqual) { |
|
|
|
|
|
|
112
|
1 |
|
$this->errors[]= [ |
|
113
|
1 |
|
'disallowedToken' => '!=', |
|
114
|
1 |
|
'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED, |
|
115
|
1 |
|
'line' => $node->getLine(), |
|
116
|
1 |
|
'reason' => $this->buildReason('!=', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED) |
|
117
|
1 |
|
]; |
|
118
|
1 |
|
} |
|
119
|
45 |
|
if ($node instanceof Node\Stmt\Class_) { |
|
|
|
|
|
|
120
|
37 |
|
if (!is_null($node->extends)) { |
|
121
|
3 |
|
$this->checkBlackList($node->extends->toString(), CodeChecker::CLASS_EXTENDS_NOT_ALLOWED, $node); |
|
122
|
3 |
|
} |
|
123
|
37 |
|
foreach ($node->implements as $implements) { |
|
124
|
15 |
|
$this->checkBlackList($implements->toString(), CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED, $node); |
|
125
|
37 |
|
} |
|
126
|
37 |
|
} |
|
127
|
45 |
|
if ($node instanceof Node\Expr\StaticCall) { |
|
|
|
|
|
|
128
|
7 |
|
if (!is_null($node->class)) { |
|
129
|
7 |
|
if ($node->class instanceof Name) { |
|
|
|
|
|
|
130
|
7 |
|
$this->checkBlackList($node->class->toString(), CodeChecker::STATIC_CALL_NOT_ALLOWED, $node); |
|
131
|
|
|
|
|
132
|
7 |
|
$this->checkBlackListFunction($node->class->toString(), $node->name, $node); |
|
133
|
7 |
|
$this->checkBlackListMethod($node->class->toString(), $node->name, $node); |
|
134
|
7 |
|
} |
|
135
|
|
|
|
|
136
|
7 |
|
if ($node->class instanceof Node\Expr\Variable) { |
|
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* TODO: find a way to detect something like this: |
|
139
|
|
|
* $c = "OC_API"; |
|
140
|
|
|
* $n = $c::call(); |
|
141
|
|
|
*/ |
|
142
|
|
|
// $this->checkBlackListMethod($node->class->..., $node->name, $node); |
|
143
|
|
|
} |
|
144
|
7 |
|
} |
|
145
|
7 |
|
} |
|
146
|
45 |
|
if ($node instanceof Node\Expr\MethodCall) { |
|
|
|
|
|
|
147
|
|
|
if (!is_null($node->var)) { |
|
148
|
|
|
if ($node->var instanceof Node\Expr\Variable) { |
|
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* TODO: find a way to detect something like this: |
|
151
|
|
|
* $c = new OC_API(); |
|
152
|
|
|
* $n = $c::call(); |
|
153
|
|
|
* $n = $c->call(); |
|
154
|
|
|
*/ |
|
155
|
|
|
// $this->checkBlackListMethod($node->var->..., $node->name, $node); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
45 |
|
if ($node instanceof Node\Expr\ClassConstFetch) { |
|
|
|
|
|
|
160
|
7 |
|
if (!is_null($node->class)) { |
|
161
|
7 |
|
if ($node->class instanceof Name) { |
|
|
|
|
|
|
162
|
7 |
|
$this->checkBlackList($node->class->toString(), CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED, $node); |
|
163
|
7 |
|
} |
|
164
|
7 |
|
if ($node->class instanceof Node\Expr\Variable) { |
|
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* TODO: find a way to detect something like this: |
|
167
|
|
|
* $c = "OC_API"; |
|
168
|
|
|
* $n = $i::ADMIN_AUTH; |
|
169
|
|
|
*/ |
|
170
|
|
|
} else { |
|
171
|
7 |
|
$this->checkBlackListConstant($node->class->toString(), $node->name, $node); |
|
172
|
|
|
} |
|
173
|
7 |
|
} |
|
174
|
7 |
|
} |
|
175
|
45 |
|
if ($node instanceof Node\Expr\New_) { |
|
|
|
|
|
|
176
|
6 |
|
if (!is_null($node->class)) { |
|
177
|
6 |
|
if ($node->class instanceof Name) { |
|
|
|
|
|
|
178
|
6 |
|
$this->checkBlackList($node->class->toString(), CodeChecker::CLASS_NEW_NOT_ALLOWED, $node); |
|
179
|
6 |
|
} |
|
180
|
6 |
|
if ($node->class instanceof Node\Expr\Variable) { |
|
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* TODO: find a way to detect something like this: |
|
183
|
|
|
* $c = "OC_API"; |
|
184
|
|
|
* $n = new $i; |
|
185
|
|
|
*/ |
|
186
|
|
|
} |
|
187
|
6 |
|
} |
|
188
|
6 |
|
} |
|
189
|
45 |
|
if ($node instanceof Node\Stmt\UseUse) { |
|
|
|
|
|
|
190
|
21 |
|
$this->checkBlackList($node->name->toString(), CodeChecker::CLASS_USE_NOT_ALLOWED, $node); |
|
191
|
21 |
|
if ($node->alias) { |
|
192
|
21 |
|
$this->addUseNameToBlackList($node->name->toString(), $node->alias); |
|
193
|
21 |
|
} else { |
|
194
|
|
|
$this->addUseNameToBlackList($node->name->toString(), $node->name->getLast()); |
|
195
|
|
|
} |
|
196
|
21 |
|
} |
|
197
|
45 |
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Check whether an alias was introduced for a namespace of a blacklisted class |
|
201
|
|
|
* |
|
202
|
|
|
* Example: |
|
203
|
|
|
* - Blacklist entry: OCP\AppFramework\IApi |
|
204
|
|
|
* - Name: OCP\AppFramework |
|
205
|
|
|
* - Alias: OAF |
|
206
|
|
|
* => new blacklist entry: OAF\IApi |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $name |
|
209
|
|
|
* @param string $alias |
|
210
|
|
|
*/ |
|
211
|
21 |
|
private function addUseNameToBlackList($name, $alias) { |
|
212
|
21 |
|
$name = strtolower($name); |
|
213
|
21 |
|
$alias = strtolower($alias); |
|
214
|
|
|
|
|
215
|
21 |
|
foreach ($this->blackListedClassNames as $blackListedAlias => $blackListedClassName) { |
|
216
|
16 |
|
if (strpos($blackListedClassName, $name . '\\') === 0) { |
|
217
|
4 |
|
$aliasedClassName = str_replace($name, $alias, $blackListedClassName); |
|
218
|
4 |
|
$this->blackListedClassNames[$aliasedClassName] = $blackListedClassName; |
|
219
|
4 |
|
} |
|
220
|
21 |
|
} |
|
221
|
|
|
|
|
222
|
21 |
View Code Duplication |
foreach ($this->blackListedConstants as $blackListedAlias => $blackListedConstant) { |
|
|
|
|
|
|
223
|
15 |
|
if (strpos($blackListedConstant, $name . '\\') === 0 || strpos($blackListedConstant, $name . '::') === 0) { |
|
224
|
6 |
|
$aliasedConstantName = str_replace($name, $alias, $blackListedConstant); |
|
225
|
6 |
|
$this->blackListedConstants[$aliasedConstantName] = $blackListedConstant; |
|
226
|
6 |
|
} |
|
227
|
21 |
|
} |
|
228
|
|
|
|
|
229
|
21 |
View Code Duplication |
foreach ($this->blackListedFunctions as $blackListedAlias => $blackListedFunction) { |
|
|
|
|
|
|
230
|
15 |
|
if (strpos($blackListedFunction, $name . '\\') === 0 || strpos($blackListedFunction, $name . '::') === 0) { |
|
231
|
6 |
|
$aliasedFunctionName = str_replace($name, $alias, $blackListedFunction); |
|
232
|
6 |
|
$this->blackListedFunctions[$aliasedFunctionName] = $blackListedFunction; |
|
233
|
6 |
|
} |
|
234
|
21 |
|
} |
|
235
|
|
|
|
|
236
|
21 |
View Code Duplication |
foreach ($this->blackListedMethods as $blackListedAlias => $blackListedMethod) { |
|
|
|
|
|
|
237
|
15 |
|
if (strpos($blackListedMethod, $name . '\\') === 0 || strpos($blackListedMethod, $name . '::') === 0) { |
|
238
|
8 |
|
$aliasedMethodName = str_replace($name, $alias, $blackListedMethod); |
|
239
|
8 |
|
$this->blackListedMethods[$aliasedMethodName] = $blackListedMethod; |
|
240
|
8 |
|
} |
|
241
|
21 |
|
} |
|
242
|
21 |
|
} |
|
243
|
|
|
|
|
244
|
38 |
|
private function checkBlackList($name, $errorCode, Node $node) { |
|
245
|
38 |
|
$lowerName = strtolower($name); |
|
246
|
|
|
|
|
247
|
38 |
|
if (isset($this->blackListedClassNames[$lowerName])) { |
|
248
|
14 |
|
$this->errors[]= [ |
|
249
|
14 |
|
'disallowedToken' => $name, |
|
250
|
14 |
|
'errorCode' => $errorCode, |
|
251
|
14 |
|
'line' => $node->getLine(), |
|
252
|
14 |
|
'reason' => $this->buildReason($this->blackListedClassNames[$lowerName], $errorCode) |
|
253
|
14 |
|
]; |
|
254
|
14 |
|
} |
|
255
|
38 |
|
} |
|
256
|
|
|
|
|
257
|
7 |
View Code Duplication |
private function checkBlackListConstant($class, $constantName, Node $node) { |
|
258
|
7 |
|
$name = $class . '::' . $constantName; |
|
259
|
7 |
|
$lowerName = strtolower($name); |
|
260
|
|
|
|
|
261
|
7 |
|
if (isset($this->blackListedConstants[$lowerName])) { |
|
262
|
5 |
|
$this->errors[]= [ |
|
263
|
5 |
|
'disallowedToken' => $name, |
|
264
|
5 |
|
'errorCode' => CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED, |
|
265
|
5 |
|
'line' => $node->getLine(), |
|
266
|
5 |
|
'reason' => $this->buildReason($this->blackListedConstants[$lowerName], CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED) |
|
267
|
5 |
|
]; |
|
268
|
5 |
|
} |
|
269
|
7 |
|
} |
|
270
|
|
|
|
|
271
|
7 |
View Code Duplication |
private function checkBlackListFunction($class, $functionName, Node $node) { |
|
272
|
7 |
|
$name = $class . '::' . $functionName; |
|
273
|
7 |
|
$lowerName = strtolower($name); |
|
274
|
|
|
|
|
275
|
7 |
|
if (isset($this->blackListedFunctions[$lowerName])) { |
|
276
|
4 |
|
$this->errors[]= [ |
|
277
|
4 |
|
'disallowedToken' => $name, |
|
278
|
4 |
|
'errorCode' => CodeChecker::STATIC_CALL_NOT_ALLOWED, |
|
279
|
4 |
|
'line' => $node->getLine(), |
|
280
|
4 |
|
'reason' => $this->buildReason($this->blackListedFunctions[$lowerName], CodeChecker::STATIC_CALL_NOT_ALLOWED) |
|
281
|
4 |
|
]; |
|
282
|
4 |
|
} |
|
283
|
7 |
|
} |
|
284
|
|
|
|
|
285
|
7 |
View Code Duplication |
private function checkBlackListMethod($class, $functionName, Node $node) { |
|
286
|
7 |
|
$name = $class . '::' . $functionName; |
|
287
|
7 |
|
$lowerName = strtolower($name); |
|
288
|
|
|
|
|
289
|
7 |
|
if (isset($this->blackListedMethods[$lowerName])) { |
|
290
|
4 |
|
$this->errors[]= [ |
|
291
|
4 |
|
'disallowedToken' => $name, |
|
292
|
4 |
|
'errorCode' => CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED, |
|
293
|
4 |
|
'line' => $node->getLine(), |
|
294
|
4 |
|
'reason' => $this->buildReason($this->blackListedMethods[$lowerName], CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED) |
|
295
|
4 |
|
]; |
|
296
|
4 |
|
} |
|
297
|
7 |
|
} |
|
298
|
|
|
|
|
299
|
25 |
|
private function buildReason($name, $errorCode) { |
|
300
|
25 |
|
if (isset($this->errorMessages[$errorCode])) { |
|
301
|
25 |
|
$desc = $this->list->getDescription($errorCode, $name); |
|
302
|
25 |
|
return sprintf($this->errorMessages[$errorCode], $desc); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
return "$name usage not allowed - error: $errorCode"; |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..