-
Notifications
You must be signed in to change notification settings - Fork 548
Implement rule for unserialize
#4754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
eliashaeussler
wants to merge
1
commit into
phpstan:2.1.x
Choose a base branch
from
eliashaeussler:feature/unserialize
base: 2.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+250
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,148 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Functions; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PHPStan\Analyser\ArgumentsNormalizer; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\AutowiredParameter; | ||
| use PHPStan\DependencyInjection\RegisteredRule; | ||
| use PHPStan\Php\PhpVersion; | ||
| use PHPStan\Reflection\ParametersAcceptorSelector; | ||
| use PHPStan\Reflection\ReflectionProvider; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use PHPStan\Type\VerbosityLevel; | ||
| use function count; | ||
| use function sprintf; | ||
|
|
||
| /** | ||
| * @implements Rule<Node\Expr\FuncCall> | ||
| */ | ||
| #[RegisteredRule(level: 5)] | ||
| final class UnserializeRule implements Rule | ||
| { | ||
|
|
||
| public function __construct( | ||
| private readonly PhpVersion $phpVersion, | ||
| private readonly ReflectionProvider $reflectionProvider, | ||
| #[AutowiredParameter] | ||
| private readonly bool $checkInsecureUnserialize, | ||
| ) | ||
| { | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return FuncCall::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if (!($node->name instanceof Node\Name)) { | ||
| return []; | ||
| } | ||
|
|
||
| if (!$this->reflectionProvider->hasFunction($node->name, $scope)) { | ||
| return []; | ||
| } | ||
|
|
||
| $functionReflection = $this->reflectionProvider->getFunction($node->name, $scope); | ||
| if ($functionReflection->getName() !== 'unserialize') { | ||
| return []; | ||
| } | ||
|
|
||
| $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( | ||
| $scope, | ||
| $node->getArgs(), | ||
| $functionReflection->getVariants(), | ||
| $functionReflection->getNamedArgumentsVariants(), | ||
| ); | ||
|
|
||
| $normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node); | ||
| if ($normalizedFuncCall === null) { | ||
| return []; | ||
| } | ||
|
|
||
| $args = $normalizedFuncCall->getArgs(); | ||
| if (count($args) !== 2) { | ||
| if ($this->checkInsecureUnserialize) { | ||
| return [ | ||
| RuleErrorBuilder::message( | ||
| 'Calling unserialize() without parameter $2 options and "allowed_classes" set to false or a list of allowed class names is insecure.', | ||
| )->identifier('unserialize.options.missing')->build(), | ||
| ]; | ||
| } | ||
| return []; | ||
| } | ||
|
|
||
| $type = $scope->getType($args[1]->value); | ||
| $constantArrays = $type->getConstantArrays(); | ||
| if ($constantArrays === []) { | ||
| return []; | ||
| } | ||
|
|
||
| $allowedClassesChecked = false; | ||
| $errors = []; | ||
| foreach ($constantArrays[0]->getValueTypes() as $i => $valueType) { | ||
| $key = $constantArrays[0]->getKeyTypes()[$i]->getValue(); | ||
| switch ($key) { | ||
| case 'allowed_classes': | ||
| $allowedClassesChecked = true; | ||
| if ($valueType->isBoolean()->yes()) { | ||
| if ($this->checkInsecureUnserialize && $valueType->isTrue()->yes()) { | ||
| $errors[] = RuleErrorBuilder::message( | ||
| 'Parameter #2 $options to function unserialize must either be false or a list of allowed class names.', | ||
| )->identifier('unserialize.allowedClasses.insecure')->build(); | ||
| } | ||
| continue 2; | ||
| } | ||
| $optionConstantArrays = $valueType->getConstantArrays(); | ||
| if ($valueType->isBoolean()->no() && $optionConstantArrays !== []) { | ||
| foreach ($optionConstantArrays[0]->getValueTypes() as $j => $itemType) { | ||
| $constantStrings = $itemType->getConstantStrings(); | ||
| if ($constantStrings !== []) { | ||
| continue; | ||
| } | ||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Parameter #2 $options to function unserialize contains an invalid value for "allowed_classes" item #%d.', | ||
| $j + 1, | ||
| ))->identifier('unserialize.allowedClasses.invalidType')->build(); | ||
| } | ||
| } else { | ||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Parameter #2 $options to function unserialize contains an invalid value %s for "allowed_classes".', | ||
| $valueType->describe(VerbosityLevel::value()), | ||
| ))->identifier('unserialize.allowedClasses.invalidType')->build(); | ||
| } | ||
| break; | ||
| case 'max_depth': | ||
| if (!$this->phpVersion->supportsUnserializeMaxDepthOption()) { | ||
| $errors[] = RuleErrorBuilder::message( | ||
| 'Parameter #2 $options to function unserialize contains an option "max_depth" which is not supported by this PHP version.', | ||
| )->identifier('unserialize.maxDepth.unsupported')->build(); | ||
| } elseif ($valueType->isInteger()->no()) { | ||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Parameter #2 $options to function unserialize contains an invalid value %s for "max_depth".', | ||
| $valueType->describe(VerbosityLevel::value()), | ||
| ))->identifier('unserialize.maxDepth.invalidType')->build(); | ||
| } | ||
| break; | ||
| default: | ||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Parameter #2 $options to function unserialize contains unsupported option "%s".', | ||
| $key, | ||
| ))->identifier('unserialize.unsupported')->build(); | ||
| } | ||
| } | ||
| if ($this->checkInsecureUnserialize && !$allowedClassesChecked) { | ||
| $errors[] = RuleErrorBuilder::message( | ||
| 'Parameter #2 $options to function unserialize must be present with "allowed_classes" set to false or a list of allowed class names.', | ||
| )->identifier('unserialize.allowedClasses.missing')->build(); | ||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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,75 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Functions; | ||
|
|
||
| use PHPStan\Php\PhpVersion; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use PHPUnit\Framework\Attributes\RequiresPhp; | ||
| use const PHP_VERSION_ID; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<UnserializeRule> | ||
| */ | ||
| class UnserializeRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return new UnserializeRule(new PhpVersion(PHP_VERSION_ID), self::createReflectionProvider(), true); | ||
| } | ||
|
|
||
| public function testFile(): void | ||
| { | ||
| $expectedErrors = [ | ||
| [ | ||
| 'Parameter #2 $options to function unserialize contains an invalid value for "allowed_classes" item #1.', | ||
| 5, | ||
| ], | ||
| [ | ||
| 'Parameter #2 $options to function unserialize contains an invalid value null for "allowed_classes".', | ||
| 7, | ||
| ], | ||
| [ | ||
| 'Parameter #2 $options to function unserialize contains an invalid value null for "max_depth".', | ||
| 9, | ||
| ], | ||
| [ | ||
| 'Parameter #2 $options to function unserialize must be present with "allowed_classes" set to false or a list of allowed class names.', | ||
| 9, | ||
| ], | ||
| [ | ||
| 'Parameter #2 $options to function unserialize contains unsupported option "foo".', | ||
| 11, | ||
| ], | ||
| [ | ||
| 'Parameter #2 $options to function unserialize must be present with "allowed_classes" set to false or a list of allowed class names.', | ||
| 11, | ||
| ], | ||
| [ | ||
| 'Parameter #2 $options to function unserialize must either be false or a list of allowed class names.', | ||
| 13, | ||
| ], | ||
| [ | ||
| 'Calling unserialize() without parameter $2 options and "allowed_classes" set to false or a list of allowed class names is insecure.', | ||
| 15, | ||
| ], | ||
| ]; | ||
|
|
||
| $this->analyse([__DIR__ . '/data/unserialize.php'], $expectedErrors); | ||
| } | ||
|
|
||
| #[RequiresPhp('< 7.4')] | ||
| public function testMaxDepth(): void | ||
| { | ||
| $expectedErrors = [ | ||
| [ | ||
| 'Parameter #2 $options to function unserialize contains an option "max_depth" which is not supported by this PHP version.', | ||
| 5, | ||
| ], | ||
| ]; | ||
|
|
||
| $this->analyse([__DIR__ . '/data/unserialize_max_depth.php'], $expectedErrors); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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,15 @@ | ||
| <?php | ||
|
|
||
| $payload = 'b:0;'; | ||
|
|
||
| unserialize($payload, ['allowed_classes' => [null]]); | ||
|
|
||
| unserialize($payload, ['allowed_classes' => null]); | ||
|
|
||
| unserialize($payload, ['max_depth' => null]); | ||
|
|
||
| unserialize($payload, ['foo' => null]); | ||
|
|
||
| unserialize($payload, ['allowed_classes' => true]); | ||
|
|
||
| unserialize($payload); |
This file contains hidden or 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,5 @@ | ||
| <?php | ||
|
|
||
| $payload = 'b:0;'; | ||
|
|
||
| unserialize($payload, ['allowed_classes' => false, 'max_depth' => 3]); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure whether to keep this test case, since it does not seem like PHP < 7.4 is actually included in the test matrix.