-
-
Notifications
You must be signed in to change notification settings - Fork 17
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 #36 from spaze/spaze/detect-eval
Detect eval()
- Loading branch information
Showing
7 changed files
with
125 additions
and
11 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
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
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,56 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Spaze\PHPStan\Rules\Disallowed; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Eval_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\ShouldNotHappenException; | ||
|
||
/** | ||
* Reports on dynamically calling eval(). | ||
* | ||
* @package Spaze\PHPStan\Rules\Disallowed | ||
* @implements Rule<Eval_> | ||
*/ | ||
class EvalCalls implements Rule | ||
{ | ||
|
||
/** @var DisallowedHelper */ | ||
private $disallowedHelper; | ||
|
||
/** @var DisallowedCall[] */ | ||
private $disallowedCalls; | ||
|
||
|
||
/** | ||
* @param DisallowedHelper $disallowedHelper | ||
* @param array<array{function?:string, method?:string, message?:string, allowIn?:string[], allowParamsInAllowed?:array<integer, integer|boolean|string>, allowParamsAnywhere?:array<integer, integer|boolean|string>}> $forbiddenCalls | ||
* @throws ShouldNotHappenException | ||
*/ | ||
public function __construct(DisallowedHelper $disallowedHelper, array $forbiddenCalls) | ||
{ | ||
$this->disallowedHelper = $disallowedHelper; | ||
$this->disallowedCalls = $this->disallowedHelper->createCallsFromConfig($forbiddenCalls); | ||
} | ||
|
||
|
||
public function getNodeType(): string | ||
{ | ||
return Eval_::class; | ||
} | ||
|
||
|
||
/** | ||
* @param Node $node | ||
* @param Scope $scope | ||
* @return string[] | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
return $this->disallowedHelper->getDisallowedMessage(null, $scope, 'eval', 'eval', $this->disallowedCalls); | ||
} | ||
|
||
} |
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,43 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Spaze\PHPStan\Rules\Disallowed; | ||
|
||
use PHPStan\File\FileHelper; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
class EvalCallsTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new EvalCalls( | ||
new DisallowedHelper(new FileHelper(__DIR__)), | ||
[ | ||
[ | ||
'function' => 'eval()', | ||
'allowIn' => [ | ||
'src/disallowed-allowed/*.php', | ||
'src/*-allow/*.*', | ||
], | ||
], | ||
] | ||
); | ||
} | ||
|
||
|
||
public function testRule(): void | ||
{ | ||
// Based on the configuration above, in this file: | ||
$this->analyse([__DIR__ . '/src/disallowed/functionCalls.php'], [ | ||
[ | ||
'Calling eval() is forbidden, because reasons', | ||
28, | ||
], | ||
]); | ||
// Based on the configuration above, no errors in this file: | ||
$this->analyse([__DIR__ . '/src/disallowed-allow/functionCalls.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 |
---|---|---|
|
@@ -23,3 +23,6 @@ | |
|
||
// allowed by path | ||
print_r('bar bar was', false); | ||
|
||
// a language construct, allowed by path | ||
eval('$foo="bar";'); |
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