|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\DevTools\PHPStan; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\MethodCall; |
| 9 | +use PhpParser\NodeTraverser; |
| 10 | +use PhpParser\NodeVisitorAbstract; |
| 11 | +use PHPStan\Analyser\Scope; |
| 12 | +use PHPStan\Node\InClassMethodNode; |
| 13 | +use PHPStan\Rules\Rule; |
| 14 | +use PHPStan\Rules\RuleError; |
| 15 | +use PHPStan\Rules\RuleErrorBuilder; |
| 16 | +use ReflectionAttribute; |
| 17 | +use ReflectionException; |
| 18 | +use ReflectionMethod; |
| 19 | +use Symfony\Component\Routing\Annotation\Route; |
| 20 | +use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 21 | + |
| 22 | +use function array_filter; |
| 23 | +use function array_values; |
| 24 | + |
| 25 | +/** @implements Rule<InClassMethodNode> */ |
| 26 | +class RouteSecurityChecker implements Rule |
| 27 | +{ |
| 28 | + public function getNodeType(): string |
| 29 | + { |
| 30 | + return InClassMethodNode::class; |
| 31 | + } |
| 32 | + |
| 33 | + /** @inheritdoc */ |
| 34 | + public function processNode(Node $node, Scope $scope): array |
| 35 | + { |
| 36 | + \assert($node instanceof InClassMethodNode); |
| 37 | + $className = $scope->getClassReflection()?->getName(); |
| 38 | + $functionName = $scope->getFunctionName(); |
| 39 | + if (! $className || ! $functionName) { |
| 40 | + return []; |
| 41 | + } |
| 42 | + |
| 43 | + try { |
| 44 | + $reflection = new ReflectionMethod($className, $functionName); |
| 45 | + } catch (ReflectionException) { |
| 46 | + return []; |
| 47 | + } |
| 48 | + |
| 49 | + $attributes = $reflection->getAttributes(); |
| 50 | + |
| 51 | + // Parse only Routes |
| 52 | + $routeAttribute = $this->getAttribute(Route::class, $attributes); |
| 53 | + if (! $routeAttribute) { |
| 54 | + return []; |
| 55 | + } |
| 56 | + |
| 57 | + if ($this->functionAttributesContain(IKnowWhatImDoingThisIsAPublicRoute::class, $attributes)) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + |
| 61 | + $isGrantedAttribute = $this->getAttribute(IsGranted::class, $attributes); |
| 62 | + |
| 63 | + // LVL 1 NO PROTECTION AT ALL :: Missing vertical controls : no IsGranted and no denyAccessUnlessGranted (even without subject) |
| 64 | + if ($isGrantedAttribute === null && ! $this->isDenyAccessUnlessGrantedCalledInRouteFunction($node, false)) { |
| 65 | + return $this->buildError( |
| 66 | + sprintf('🛑🔓 SECURITY: Route %s::%s is public !', $className, $functionName), |
| 67 | + "Add an #[IsGranted] attribute or use the `denyAccessUnlessGranted` function. |
| 68 | + If you are sure that this route should remain public, add the " . IKnowWhatImDoingThisIsAPublicRoute::class . ' attribute', |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + if ($this->functionAttributesContain(ThisRouteDoesntNeedAVoter::class, $attributes)){ |
| 73 | + return []; |
| 74 | + } |
| 75 | + |
| 76 | + // LVL 2 VERTICAL ACCESS ONLY :: IsGranted is present BUT no voter is called |
| 77 | + if ($isGrantedAttribute === null && ! $this->isDenyAccessUnlessGrantedCalledInRouteFunction($node, true)) { |
| 78 | + return $this->buildError( |
| 79 | + sprintf('🛑🔓 SECURITY: Route %s::%s is insufficiently protected !', $className, $functionName), |
| 80 | + "Pass the 'subject' argument to the \$this->denyAccessUnlessGranted() call. |
| 81 | + If you are sure that this route's protection should only on user's permissions, add a ".ThisRouteDoesntNeedAVoter::class." attribute.", |
| 82 | + ); |
| 83 | + } |
| 84 | + if ($isGrantedAttribute !== null){ |
| 85 | + $isGrantedAttributeInstance = $isGrantedAttribute->newInstance(); |
| 86 | + \assert($isGrantedAttributeInstance instanceof IsGranted); |
| 87 | + if ($isGrantedAttributeInstance->subject === null) { |
| 88 | + return $this->buildError( |
| 89 | + sprintf('🛑🔓 SECURITY: Route %s::%s is insufficiently protected !', $className, $functionName), |
| 90 | + "Pass the 'subject' argument to the 'IsGranted' attribute. |
| 91 | + If you are sure that this route's protection should only on user's permissions, add a ".ThisRouteDoesntNeedAVoter::class.' attribute.', |
| 92 | + ); |
| 93 | + } |
| 94 | + } |
| 95 | + return []; |
| 96 | + } |
| 97 | + |
| 98 | + /** @param array<ReflectionAttribute<object>> $attributes */ |
| 99 | + private function functionAttributesContain(string $attributeClass, array $attributes): bool |
| 100 | + { |
| 101 | + return \count($this->getAttributes($attributeClass, $attributes)) > 0; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @param array<ReflectionAttribute<object>> $attributes |
| 106 | + * |
| 107 | + * @return ReflectionAttribute<object>|null |
| 108 | + */ |
| 109 | + private function getAttribute(string $attributeClass, array $attributes): ReflectionAttribute|null |
| 110 | + { |
| 111 | + $attributes = $this->getAttributes($attributeClass, $attributes); |
| 112 | + |
| 113 | + return \count($attributes) > 0 ? $attributes[0] : null; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @param array<ReflectionAttribute<object>> $attributes |
| 118 | + * |
| 119 | + * @return array<ReflectionAttribute<object>> |
| 120 | + */ |
| 121 | + private function getAttributes(string $attributeClass, array $attributes): array |
| 122 | + { |
| 123 | + return array_values(array_filter( |
| 124 | + $attributes, |
| 125 | + static fn (ReflectionAttribute $attr) => $attr->getName() === $attributeClass |
| 126 | + )); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @return array<RuleError> |
| 131 | + */ |
| 132 | + private function buildError(string $message, string $tip): array |
| 133 | + { |
| 134 | + return [ |
| 135 | + RuleErrorBuilder::message($message . "\n") |
| 136 | + ->tip($tip) |
| 137 | + ->build(), |
| 138 | + ]; |
| 139 | + } |
| 140 | + |
| 141 | + private function isDenyAccessUnlessGrantedCalledInRouteFunction(InClassMethodNode $node, bool $requireSubject): bool |
| 142 | + { |
| 143 | + $visitor = new class ($requireSubject) extends NodeVisitorAbstract { |
| 144 | + private bool $isSecurityCheckFunctionCalled = false; |
| 145 | + |
| 146 | + public function __construct(private readonly bool $requireSubject) |
| 147 | + { |
| 148 | + } |
| 149 | + |
| 150 | + public function enterNode(Node $node): int|null |
| 151 | + { |
| 152 | + if ( |
| 153 | + $node instanceof MethodCall && $node->name instanceof Node\Identifier |
| 154 | + && $node->name->toString() === 'denyAccessUnlessGranted' |
| 155 | + && (!$this->requireSubject || isset($node->args[1])) |
| 156 | + ) { |
| 157 | + $this->isSecurityCheckFunctionCalled = true; |
| 158 | + |
| 159 | + return NodeTraverser::STOP_TRAVERSAL; |
| 160 | + } |
| 161 | + |
| 162 | + return null; |
| 163 | + } |
| 164 | + |
| 165 | + public function isIsSecurityCheckFunctionCalled(): bool |
| 166 | + { |
| 167 | + return $this->isSecurityCheckFunctionCalled; |
| 168 | + } |
| 169 | + }; |
| 170 | + |
| 171 | + $traverser = new NodeTraverser(); |
| 172 | + $traverser->addVisitor($visitor); |
| 173 | + |
| 174 | + $traverser->traverse($node->getOriginalNode()->stmts ?? []); |
| 175 | + |
| 176 | + return $visitor->isIsSecurityCheckFunctionCalled(); |
| 177 | + } |
| 178 | +} |
0 commit comments