-
Notifications
You must be signed in to change notification settings - Fork 62
Error on unserialize() without allowed_classes option
#323
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
base: 2.0.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Functions; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use function count; | ||
| use function in_array; | ||
| use function strtolower; | ||
|
|
||
| /** @implements Rule<FuncCall> */ | ||
| class UnserializeAllowedClassesRule implements Rule | ||
| { | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return FuncCall::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if (!$node->name instanceof Node\Name) { | ||
| return []; | ||
| } | ||
|
|
||
| if (!in_array(strtolower((string) $node->name), ['unserialize'], true)) { | ||
| return []; | ||
| } | ||
|
|
||
| $args = $node->getArgs(); | ||
|
|
||
| if (count($args) < 2) { | ||
| return [ | ||
| RuleErrorBuilder::message( | ||
| 'unserialize() called without the $options argument. Always pass ' | ||
| . "['allowed_classes' => false] or an explicit list of safe classes. " | ||
| . "If you truly need to allow all classes, pass ['allowed_classes' => true] " | ||
| . 'to make that decision explicit.', | ||
| ) | ||
| ->identifier('unserialize.allowedClasses') | ||
| ->build(), | ||
| ]; | ||
| } | ||
|
|
||
| $optionsArg = $args[1]->value; | ||
|
|
||
| if (!$optionsArg instanceof Node\Expr\Array_) { | ||
| return []; | ||
| } | ||
|
|
||
| foreach ($optionsArg->items as $item) { | ||
| if ($item === null) { | ||
|
Check failure on line 55 in src/Rules/Functions/UnserializeAllowedClassesRule.php
|
||
| continue; | ||
| } | ||
|
|
||
| $key = $item->key; | ||
| if (!$key instanceof Node\Scalar\String_) { | ||
| continue; | ||
| } | ||
|
|
||
| if ($key->value === 'allowed_classes') { | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| return [ | ||
| RuleErrorBuilder::message( | ||
| "unserialize() called without the 'allowed_classes' key in \$options. " | ||
| . "Always pass ['allowed_classes' => false] or an explicit list of safe classes. " | ||
| . "If you truly need to allow all classes, pass ['allowed_classes' => true] " | ||
| . 'to make that decision explicit.', | ||
| ) | ||
| ->identifier('unserialize.allowedClasses') | ||
| ->build(), | ||
| ]; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Functions; | ||
|
|
||
| use PHPStan\Testing\RuleTestCase; | ||
|
|
||
| /** @extends RuleTestCase<UnserializeAllowedClassesRule> */ | ||
| class UnserializeAllowedClassesRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): UnserializeAllowedClassesRule | ||
| { | ||
| return new UnserializeAllowedClassesRule(); | ||
| } | ||
|
|
||
| public function testViolations(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/unserialize-usages.php'], [ | ||
| [ | ||
| "unserialize() called without the \$options argument. Always pass ['allowed_classes' => false] or an explicit list of safe classes. If you truly need to allow all classes, pass ['allowed_classes' => true] to make that decision explicit.", | ||
| 10, | ||
| ], | ||
| [ | ||
| "unserialize() called without the 'allowed_classes' key in \$options. Always pass ['allowed_classes' => false] or an explicit list of safe classes. If you truly need to allow all classes, pass ['allowed_classes' => true] to make that decision explicit.", | ||
| 15, | ||
| ], | ||
| [ | ||
| "unserialize() called without the 'allowed_classes' key in \$options. Always pass ['allowed_classes' => false] or an explicit list of safe classes. If you truly need to allow all classes, pass ['allowed_classes' => true] to make that decision explicit.", | ||
| 20, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| public function testNoErrors(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/unserialize-good-usages.php'], []); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace UnserializeAllowedClasses; | ||
|
|
||
| final class UnserializeUsagesOk | ||
| { | ||
|
|
||
| /** @return array<string, mixed> */ | ||
| public function allowedClassesFalse(string $data): array | ||
| { | ||
| /** @var array<string, mixed> $result */ | ||
| $result = unserialize($data, ['allowed_classes' => false]); | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| public function allowedClassesExplicitTrue(string $data): mixed | ||
| { | ||
| return unserialize($data, ['allowed_classes' => true]); | ||
| } | ||
|
|
||
| public function allowedClassesList(string $data): mixed | ||
| { | ||
| return unserialize($data, ['allowed_classes' => [\stdClass::class]]); | ||
| } | ||
|
|
||
| public function dynamicOptions(string $data, mixed $options): mixed | ||
| { | ||
| return unserialize($data, $options); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace UnserializeAllowedClasses; | ||
|
|
||
| final class UnserializeUsages | ||
| { | ||
|
|
||
| public function noOptions(string $data): mixed | ||
| { | ||
| return unserialize($data); | ||
| } | ||
|
|
||
| public function emptyOptions(string $data): mixed | ||
| { | ||
| return unserialize($data, []); | ||
| } | ||
|
|
||
| public function optionsWithoutAllowedClasses(string $data): mixed | ||
| { | ||
| return unserialize($data, ['some_other_key' => true]); | ||
| } | ||
|
|
||
| /** @return array<string, mixed> */ | ||
| public function allowedClassesFalse(string $data): array | ||
| { | ||
| /** @var array<string, mixed> $result */ | ||
| $result = unserialize($data, ['allowed_classes' => false]); | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| /** @return array<string, mixed> */ | ||
| public function allowedClassesExplicitTrue(string $data): array | ||
| { | ||
| /** @var array<string, mixed> $result */ | ||
| $result = unserialize($data, ['allowed_classes' => true]); | ||
|
|
||
| return $result; | ||
| } | ||
|
|
||
| public function allowedClassesList(string $data): mixed | ||
| { | ||
| return unserialize($data, ['allowed_classes' => [\stdClass::class]]); | ||
| } | ||
|
|
||
| public function dynamicOptions(string $data, mixed $options): mixed | ||
| { | ||
| return unserialize($data, $options); | ||
| } | ||
|
|
||
| } |
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 need help here. This check seems to be necessary with the oldest supported PHPStan version, but for PHPStan 2.2, it's dead code. I'm having a hard time making both PHPStans happy at the same time in your CI. 🙈