AppFrameworkTainter.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Copyright (c) 2020 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. use Psalm\CodeLocation;
  25. use Psalm\Plugin\EventHandler\AfterFunctionLikeAnalysisInterface;
  26. use Psalm\Plugin\EventHandler\Event\AfterFunctionLikeAnalysisEvent;
  27. use Psalm\Type\TaintKindGroup;
  28. class AppFrameworkTainter implements AfterFunctionLikeAnalysisInterface {
  29. public static function afterStatementAnalysis(AfterFunctionLikeAnalysisEvent $event): ?bool {
  30. if ($event->getStatementsSource()->getFQCLN() === null) {
  31. return null;
  32. }
  33. if (!$event->getCodebase()->classExtendsOrImplements($event->getStatementsSource()->getFQCLN(), \OCP\AppFramework\Controller::class)) {
  34. return null;
  35. }
  36. if (!($event->getStmt() instanceof PhpParser\Node\Stmt\ClassMethod)) {
  37. return null;
  38. }
  39. if (!$event->getStmt()->isPublic() || $event->getStmt()->isMagic()) {
  40. return null;
  41. }
  42. foreach ($event->getStmt()->params as $i => $param) {
  43. $expr_type = new Psalm\Type\Union([new Psalm\Type\Atomic\TString()]);
  44. $expr_identifier = (strtolower($event->getStatementsSource()->getFQCLN()) . '::' . strtolower($event->getFunctionlikeStorage()->cased_name) . '#' . ($i + 1));
  45. $event->getCodebase()->addTaintSource(
  46. $expr_type,
  47. $expr_identifier,
  48. TaintKindGroup::ALL_INPUT,
  49. new CodeLocation($event->getStatementsSource(), $param)
  50. );
  51. }
  52. return null;
  53. }
  54. }