-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #378 from brambaud/issue/315
Report missing explicit access check on entity queries
- Loading branch information
Showing
14 changed files
with
270 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/Rules/Drupal/EntityQuery/EntityQueryHasAccessCheckRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Rules\Drupal\EntityQuery; | ||
|
||
use mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryExecuteWithoutAccessCheckCountType; | ||
use mglaman\PHPStanDrupal\Type\EntityQuery\EntityQueryExecuteWithoutAccessCheckType; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
final class EntityQueryHasAccessCheckRule implements Rule | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\MethodCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node instanceof Node\Expr\MethodCall) { | ||
return []; | ||
} | ||
|
||
$name = $node->name; | ||
if (!$name instanceof Node\Identifier) { | ||
return []; | ||
} | ||
if ($name->toString() !== 'execute') { | ||
return []; | ||
} | ||
|
||
$type = $scope->getType($node); | ||
|
||
if (!$type instanceof EntityQueryExecuteWithoutAccessCheckCountType && !$type instanceof EntityQueryExecuteWithoutAccessCheckType) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message( | ||
'Missing explicit access check on entity query.' | ||
)->tip('See https://www.drupal.org/node/3201242')->build(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/Type/EntityQuery/EntityQueryAccessCheckDynamicReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Type\EntityQuery; | ||
|
||
use Drupal\Core\Entity\Query\QueryInterface; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\Type; | ||
|
||
class EntityQueryAccessCheckDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
public function getClass(): string | ||
{ | ||
return QueryInterface::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return 'accessCheck' === $methodReflection->getName(); | ||
} | ||
|
||
public function getTypeFromMethodCall( | ||
MethodReflection $methodReflection, | ||
MethodCall $methodCall, | ||
Scope $scope | ||
): Type { | ||
$varType = $scope->getType($methodCall->var); | ||
|
||
if (!$varType instanceof EntityQueryType) { | ||
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); | ||
} | ||
|
||
return $varType->withAccessCheck(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Type/EntityQuery/EntityQueryExecuteWithoutAccessCheckCountType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Type\EntityQuery; | ||
|
||
use PHPStan\Type\IntegerType; | ||
|
||
final class EntityQueryExecuteWithoutAccessCheckCountType extends IntegerType | ||
{ | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/Type/EntityQuery/EntityQueryExecuteWithoutAccessCheckType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Type\EntityQuery; | ||
|
||
use PHPStan\Type\ArrayType; | ||
|
||
use PHPStan\Type\StringType; | ||
|
||
final class EntityQueryExecuteWithoutAccessCheckType extends ArrayType | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Type\EntityQuery; | ||
|
||
use PHPStan\Type\ObjectType; | ||
|
||
class EntityQueryType extends ObjectType | ||
{ | ||
private bool $hasAccessCheck = false; | ||
|
||
public function hasAccessCheck(): bool | ||
{ | ||
return $this->hasAccessCheck; | ||
} | ||
|
||
public function withAccessCheck(): self | ||
{ | ||
$type = clone $this; | ||
$type->hasAccessCheck = true; | ||
|
||
return $type; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/fixtures/drupal/modules/phpstan_fixtures/src/EntityQueryHasAccessRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace fixtures\drupal\modules\phpstan_fixtures\src; | ||
|
||
use function PHPStan\dumpType; | ||
|
||
final class EntityQueryHasAccessRule | ||
{ | ||
public function foo(): void | ||
{ | ||
\Drupal::entityTypeManager()->getStorage('node') | ||
->getQuery() | ||
->accessCheck(FALSE) | ||
->condition('field_test', 'foo', '=') | ||
->execute(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/fixtures/drupal/modules/phpstan_fixtures/src/EntityQueryWithAccessRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace fixtures\drupal\modules\phpstan_fixtures\src; | ||
|
||
final class EntityQueryWithAccessRule | ||
{ | ||
public function foo(): void | ||
{ | ||
\Drupal::entityTypeManager()->getStorage('node') | ||
->getQuery() | ||
->accessCheck(FALSE) | ||
->condition('field_test', 'foo', '=') | ||
->execute(); | ||
} | ||
|
||
public function bar(): void | ||
{ | ||
\Drupal::entityQuery('node') | ||
->accessCheck(FALSE) | ||
->condition('field_test', 'foo', '=') | ||
->execute(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
tests/fixtures/drupal/modules/phpstan_fixtures/src/EntityQueryWithoutAccessRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace fixtures\drupal\modules\phpstan_fixtures\src; | ||
|
||
final class EntityQueryWithoutAccessRule | ||
{ | ||
public function foo(): void | ||
{ | ||
\Drupal::entityTypeManager()->getStorage('node') | ||
->getQuery() | ||
->condition('field_test', 'foo', '=') | ||
->execute(); | ||
} | ||
|
||
public function bar(): void | ||
{ | ||
\Drupal::entityQuery('node') | ||
->condition('field_test', 'foo', '=') | ||
->execute(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace mglaman\PHPStanDrupal\Tests\Rules; | ||
|
||
use mglaman\PHPStanDrupal\Rules\Drupal\EntityQuery\EntityQueryHasAccessCheckRule; | ||
use mglaman\PHPStanDrupal\Tests\DrupalRuleTestCase; | ||
|
||
final class EntityQueryHasAccessCheckRuleTest extends DrupalRuleTestCase | ||
{ | ||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
return new EntityQueryHasAccessCheckRule(); | ||
} | ||
|
||
/** | ||
* @dataProvider cases | ||
*/ | ||
public function test(array $files, array $errors): void | ||
{ | ||
$this->analyse($files, $errors); | ||
} | ||
|
||
public function cases(): \Generator | ||
{ | ||
yield [ | ||
[__DIR__.'/../../fixtures/drupal/modules/phpstan_fixtures/src/EntityQueryWithAccessRule.php'], | ||
[], | ||
]; | ||
|
||
yield [ | ||
[__DIR__.'/../../fixtures/drupal/modules/phpstan_fixtures/src/EntityQueryWithoutAccessRule.php'], | ||
[ | ||
[ | ||
'Missing explicit access check on entity query.', | ||
11, | ||
'See https://www.drupal.org/node/3201242', | ||
], | ||
[ | ||
'Missing explicit access check on entity query.', | ||
19, | ||
'See https://www.drupal.org/node/3201242', | ||
], | ||
], | ||
]; | ||
} | ||
} |