NodeVisitor.php 10 KB

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