2015-01-28 21:08:50 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-10-05 18:54:56 +00:00
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
2016-01-12 14:02:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2015-03-26 10:44:34 +00:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2015-01-28 21:08:50 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2015-07-07 10:08:12 +00:00
|
|
|
namespace OC\App\CodeChecker;
|
2015-01-28 21:08:50 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Name;
|
|
|
|
use PhpParser\NodeVisitorAbstract;
|
|
|
|
|
2015-07-07 10:08:12 +00:00
|
|
|
class NodeVisitor extends NodeVisitorAbstract {
|
2015-07-16 09:40:32 +00:00
|
|
|
/** @var ICheck */
|
|
|
|
protected $list;
|
|
|
|
|
2015-06-15 10:51:22 +00:00
|
|
|
/** @var string */
|
|
|
|
protected $blackListDescription;
|
|
|
|
/** @var string[] */
|
|
|
|
protected $blackListedClassNames;
|
2015-06-15 15:20:38 +00:00
|
|
|
/** @var string[] */
|
|
|
|
protected $blackListedConstants;
|
2015-06-15 15:50:37 +00:00
|
|
|
/** @var string[] */
|
|
|
|
protected $blackListedFunctions;
|
2015-06-15 16:36:04 +00:00
|
|
|
/** @var string[] */
|
|
|
|
protected $blackListedMethods;
|
2015-06-15 10:51:22 +00:00
|
|
|
/** @var bool */
|
|
|
|
protected $checkEqualOperatorUsage;
|
|
|
|
/** @var string[] */
|
|
|
|
protected $errorMessages;
|
2015-01-28 21:08:50 +00:00
|
|
|
|
2015-06-15 10:51:22 +00:00
|
|
|
/**
|
2015-07-07 13:37:56 +00:00
|
|
|
* @param ICheck $list
|
2015-06-15 10:51:22 +00:00
|
|
|
*/
|
2015-07-07 13:37:56 +00:00
|
|
|
public function __construct(ICheck $list) {
|
2015-07-16 09:40:32 +00:00
|
|
|
$this->list = $list;
|
2015-06-15 13:24:45 +00:00
|
|
|
|
|
|
|
$this->blackListedClassNames = [];
|
2015-07-07 10:08:12 +00:00
|
|
|
foreach ($list->getClasses() as $class => $blackListInfo) {
|
2015-06-15 14:08:54 +00:00
|
|
|
if (is_numeric($class) && is_string($blackListInfo)) {
|
|
|
|
$class = $blackListInfo;
|
|
|
|
$blackListInfo = null;
|
|
|
|
}
|
|
|
|
|
2015-06-15 13:24:45 +00:00
|
|
|
$class = strtolower($class);
|
|
|
|
$this->blackListedClassNames[$class] = $class;
|
|
|
|
}
|
2015-06-15 15:20:38 +00:00
|
|
|
|
|
|
|
$this->blackListedConstants = [];
|
2015-07-07 10:08:12 +00:00
|
|
|
foreach ($list->getConstants() as $constantName => $blackListInfo) {
|
2015-06-15 15:50:37 +00:00
|
|
|
$constantName = strtolower($constantName);
|
|
|
|
$this->blackListedConstants[$constantName] = $constantName;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->blackListedFunctions = [];
|
2015-07-07 10:08:12 +00:00
|
|
|
foreach ($list->getFunctions() as $functionName => $blackListInfo) {
|
2015-06-15 15:50:37 +00:00
|
|
|
$functionName = strtolower($functionName);
|
|
|
|
$this->blackListedFunctions[$functionName] = $functionName;
|
2015-06-15 15:20:38 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 16:36:04 +00:00
|
|
|
$this->blackListedMethods = [];
|
2015-07-07 10:08:12 +00:00
|
|
|
foreach ($list->getMethods() as $functionName => $blackListInfo) {
|
2015-06-15 16:36:04 +00:00
|
|
|
$functionName = strtolower($functionName);
|
|
|
|
$this->blackListedMethods[$functionName] = $functionName;
|
|
|
|
}
|
|
|
|
|
2015-07-07 10:08:12 +00:00
|
|
|
$this->checkEqualOperatorUsage = $list->checkStrongComparisons();
|
2015-06-15 10:51:22 +00:00
|
|
|
|
|
|
|
$this->errorMessages = [
|
2015-07-16 09:40:32 +00:00
|
|
|
CodeChecker::CLASS_EXTENDS_NOT_ALLOWED => "%s class must not be extended",
|
|
|
|
CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED => "%s interface must not be implemented",
|
|
|
|
CodeChecker::STATIC_CALL_NOT_ALLOWED => "Static method of %s class must not be called",
|
|
|
|
CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED => "Constant of %s class must not not be fetched",
|
|
|
|
CodeChecker::CLASS_NEW_NOT_ALLOWED => "%s class must not be instantiated",
|
|
|
|
CodeChecker::CLASS_USE_NOT_ALLOWED => "%s class must not be imported with a use statement",
|
|
|
|
CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED => "Method of %s class must not be called",
|
2015-06-15 10:51:22 +00:00
|
|
|
|
|
|
|
CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED => "is discouraged",
|
|
|
|
];
|
2015-01-28 21:08:50 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 10:51:22 +00:00
|
|
|
/** @var array */
|
2015-01-28 21:08:50 +00:00
|
|
|
public $errors = [];
|
|
|
|
|
|
|
|
public function enterNode(Node $node) {
|
2015-06-15 10:51:22 +00:00
|
|
|
if ($this->checkEqualOperatorUsage && $node instanceof Node\Expr\BinaryOp\Equal) {
|
2015-05-05 10:59:33 +00:00
|
|
|
$this->errors[]= [
|
|
|
|
'disallowedToken' => '==',
|
|
|
|
'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED,
|
|
|
|
'line' => $node->getLine(),
|
|
|
|
'reason' => $this->buildReason('==', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED)
|
|
|
|
];
|
|
|
|
}
|
2015-06-15 10:51:22 +00:00
|
|
|
if ($this->checkEqualOperatorUsage && $node instanceof Node\Expr\BinaryOp\NotEqual) {
|
2015-05-05 10:59:33 +00:00
|
|
|
$this->errors[]= [
|
|
|
|
'disallowedToken' => '!=',
|
|
|
|
'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED,
|
|
|
|
'line' => $node->getLine(),
|
|
|
|
'reason' => $this->buildReason('!=', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED)
|
|
|
|
];
|
|
|
|
}
|
2015-01-28 21:08:50 +00:00
|
|
|
if ($node instanceof Node\Stmt\Class_) {
|
|
|
|
if (!is_null($node->extends)) {
|
|
|
|
$this->checkBlackList($node->extends->toString(), CodeChecker::CLASS_EXTENDS_NOT_ALLOWED, $node);
|
|
|
|
}
|
|
|
|
foreach ($node->implements as $implements) {
|
|
|
|
$this->checkBlackList($implements->toString(), CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED, $node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($node instanceof Node\Expr\StaticCall) {
|
|
|
|
if (!is_null($node->class)) {
|
|
|
|
if ($node->class instanceof Name) {
|
|
|
|
$this->checkBlackList($node->class->toString(), CodeChecker::STATIC_CALL_NOT_ALLOWED, $node);
|
2015-06-15 16:36:04 +00:00
|
|
|
|
|
|
|
$this->checkBlackListFunction($node->class->toString(), $node->name, $node);
|
|
|
|
$this->checkBlackListMethod($node->class->toString(), $node->name, $node);
|
2015-01-28 21:08:50 +00:00
|
|
|
}
|
2015-06-15 16:36:04 +00:00
|
|
|
|
2015-01-28 21:08:50 +00:00
|
|
|
if ($node->class instanceof Node\Expr\Variable) {
|
|
|
|
/**
|
|
|
|
* TODO: find a way to detect something like this:
|
|
|
|
* $c = "OC_API";
|
2015-06-15 16:36:04 +00:00
|
|
|
* $n = $c::call();
|
2015-01-28 21:08:50 +00:00
|
|
|
*/
|
2015-06-15 16:36:04 +00:00
|
|
|
// $this->checkBlackListMethod($node->class->..., $node->name, $node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($node instanceof Node\Expr\MethodCall) {
|
|
|
|
if (!is_null($node->var)) {
|
|
|
|
if ($node->var instanceof Node\Expr\Variable) {
|
|
|
|
/**
|
|
|
|
* TODO: find a way to detect something like this:
|
|
|
|
* $c = new OC_API();
|
|
|
|
* $n = $c::call();
|
|
|
|
* $n = $c->call();
|
|
|
|
*/
|
|
|
|
// $this->checkBlackListMethod($node->var->..., $node->name, $node);
|
2015-01-28 21:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($node instanceof Node\Expr\ClassConstFetch) {
|
|
|
|
if (!is_null($node->class)) {
|
|
|
|
if ($node->class instanceof Name) {
|
|
|
|
$this->checkBlackList($node->class->toString(), CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED, $node);
|
|
|
|
}
|
|
|
|
if ($node->class instanceof Node\Expr\Variable) {
|
|
|
|
/**
|
|
|
|
* TODO: find a way to detect something like this:
|
|
|
|
* $c = "OC_API";
|
|
|
|
* $n = $i::ADMIN_AUTH;
|
|
|
|
*/
|
2015-10-14 11:16:58 +00:00
|
|
|
} else {
|
|
|
|
$this->checkBlackListConstant($node->class->toString(), $node->name, $node);
|
2015-01-28 21:08:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($node instanceof Node\Expr\New_) {
|
|
|
|
if (!is_null($node->class)) {
|
|
|
|
if ($node->class instanceof Name) {
|
2015-07-16 09:40:32 +00:00
|
|
|
$this->checkBlackList($node->class->toString(), CodeChecker::CLASS_NEW_NOT_ALLOWED, $node);
|
2015-01-28 21:08:50 +00:00
|
|
|
}
|
|
|
|
if ($node->class instanceof Node\Expr\Variable) {
|
|
|
|
/**
|
|
|
|
* TODO: find a way to detect something like this:
|
|
|
|
* $c = "OC_API";
|
|
|
|
* $n = new $i;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-15 13:24:45 +00:00
|
|
|
if ($node instanceof Node\Stmt\UseUse) {
|
|
|
|
$this->checkBlackList($node->name->toString(), CodeChecker::CLASS_USE_NOT_ALLOWED, $node);
|
|
|
|
if ($node->alias) {
|
|
|
|
$this->addUseNameToBlackList($node->name->toString(), $node->alias);
|
|
|
|
} else {
|
|
|
|
$this->addUseNameToBlackList($node->name->toString(), $node->name->getLast());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether an alias was introduced for a namespace of a blacklisted class
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* - Blacklist entry: OCP\AppFramework\IApi
|
|
|
|
* - Name: OCP\AppFramework
|
|
|
|
* - Alias: OAF
|
|
|
|
* => new blacklist entry: OAF\IApi
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $alias
|
|
|
|
*/
|
|
|
|
private function addUseNameToBlackList($name, $alias) {
|
|
|
|
$name = strtolower($name);
|
|
|
|
$alias = strtolower($alias);
|
|
|
|
|
|
|
|
foreach ($this->blackListedClassNames as $blackListedAlias => $blackListedClassName) {
|
|
|
|
if (strpos($blackListedClassName, $name . '\\') === 0) {
|
|
|
|
$aliasedClassName = str_replace($name, $alias, $blackListedClassName);
|
|
|
|
$this->blackListedClassNames[$aliasedClassName] = $blackListedClassName;
|
|
|
|
}
|
|
|
|
}
|
2015-06-15 15:20:38 +00:00
|
|
|
|
|
|
|
foreach ($this->blackListedConstants as $blackListedAlias => $blackListedConstant) {
|
|
|
|
if (strpos($blackListedConstant, $name . '\\') === 0 || strpos($blackListedConstant, $name . '::') === 0) {
|
2015-06-15 15:50:37 +00:00
|
|
|
$aliasedConstantName = str_replace($name, $alias, $blackListedConstant);
|
|
|
|
$this->blackListedConstants[$aliasedConstantName] = $blackListedConstant;
|
2015-06-15 15:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 15:50:37 +00:00
|
|
|
foreach ($this->blackListedFunctions as $blackListedAlias => $blackListedFunction) {
|
|
|
|
if (strpos($blackListedFunction, $name . '\\') === 0 || strpos($blackListedFunction, $name . '::') === 0) {
|
|
|
|
$aliasedFunctionName = str_replace($name, $alias, $blackListedFunction);
|
|
|
|
$this->blackListedFunctions[$aliasedFunctionName] = $blackListedFunction;
|
|
|
|
}
|
|
|
|
}
|
2015-06-15 16:36:04 +00:00
|
|
|
|
|
|
|
foreach ($this->blackListedMethods as $blackListedAlias => $blackListedMethod) {
|
|
|
|
if (strpos($blackListedMethod, $name . '\\') === 0 || strpos($blackListedMethod, $name . '::') === 0) {
|
|
|
|
$aliasedMethodName = str_replace($name, $alias, $blackListedMethod);
|
|
|
|
$this->blackListedMethods[$aliasedMethodName] = $blackListedMethod;
|
|
|
|
}
|
|
|
|
}
|
2015-01-28 21:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function checkBlackList($name, $errorCode, Node $node) {
|
2015-06-15 15:20:38 +00:00
|
|
|
$lowerName = strtolower($name);
|
|
|
|
|
|
|
|
if (isset($this->blackListedClassNames[$lowerName])) {
|
2015-01-28 21:08:50 +00:00
|
|
|
$this->errors[]= [
|
|
|
|
'disallowedToken' => $name,
|
|
|
|
'errorCode' => $errorCode,
|
|
|
|
'line' => $node->getLine(),
|
2015-06-15 15:20:38 +00:00
|
|
|
'reason' => $this->buildReason($this->blackListedClassNames[$lowerName], $errorCode)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 15:50:37 +00:00
|
|
|
private function checkBlackListConstant($class, $constantName, Node $node) {
|
|
|
|
$name = $class . '::' . $constantName;
|
2015-06-15 15:20:38 +00:00
|
|
|
$lowerName = strtolower($name);
|
|
|
|
|
|
|
|
if (isset($this->blackListedConstants[$lowerName])) {
|
|
|
|
$this->errors[]= [
|
|
|
|
'disallowedToken' => $name,
|
|
|
|
'errorCode' => CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED,
|
|
|
|
'line' => $node->getLine(),
|
|
|
|
'reason' => $this->buildReason($this->blackListedConstants[$lowerName], CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED)
|
2015-01-28 21:08:50 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 15:50:37 +00:00
|
|
|
private function checkBlackListFunction($class, $functionName, Node $node) {
|
|
|
|
$name = $class . '::' . $functionName;
|
|
|
|
$lowerName = strtolower($name);
|
|
|
|
|
|
|
|
if (isset($this->blackListedFunctions[$lowerName])) {
|
|
|
|
$this->errors[]= [
|
|
|
|
'disallowedToken' => $name,
|
|
|
|
'errorCode' => CodeChecker::STATIC_CALL_NOT_ALLOWED,
|
|
|
|
'line' => $node->getLine(),
|
|
|
|
'reason' => $this->buildReason($this->blackListedFunctions[$lowerName], CodeChecker::STATIC_CALL_NOT_ALLOWED)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 16:36:04 +00:00
|
|
|
private function checkBlackListMethod($class, $functionName, Node $node) {
|
|
|
|
$name = $class . '::' . $functionName;
|
|
|
|
$lowerName = strtolower($name);
|
|
|
|
|
|
|
|
if (isset($this->blackListedMethods[$lowerName])) {
|
|
|
|
$this->errors[]= [
|
|
|
|
'disallowedToken' => $name,
|
|
|
|
'errorCode' => CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED,
|
|
|
|
'line' => $node->getLine(),
|
|
|
|
'reason' => $this->buildReason($this->blackListedMethods[$lowerName], CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 21:08:50 +00:00
|
|
|
private function buildReason($name, $errorCode) {
|
2015-06-15 10:51:22 +00:00
|
|
|
if (isset($this->errorMessages[$errorCode])) {
|
2015-07-16 09:40:32 +00:00
|
|
|
$desc = $this->list->getDescription($errorCode, $name);
|
|
|
|
return sprintf($this->errorMessages[$errorCode], $desc);
|
2015-01-28 21:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return "$name usage not allowed - error: $errorCode";
|
|
|
|
}
|
|
|
|
}
|