Repository navigation
Expand file tree
/
Copy pathUnserializeFunctionThrowTypeExtension.php
More file actions
182 lines (147 loc) · 4.84 KB
/
Copy pathUnserializeFunctionThrowTypeExtension.php
File metadata and controls
182 lines (147 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php declare(strict_types = 1);
namespace PHPStan\Type\Php;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\VoidType;
use Throwable;
use TypeError;
use function count;
use function preg_match;
/**
* unserialize() can propagate throwables from __wakeup() or __unserialize()
* methods of classes instantiated during deserialization.
*
* It also throws TypeError and ValueError for invalid $options, and TypeError
* when the data does not fit a typed property. Objects of classes that are not
* allowed are unserialized as __PHP_Incomplete_Class.
*/
#[AutowiredService]
final class UnserializeFunctionThrowTypeExtension implements DynamicFunctionThrowTypeExtension
{
public function __construct(
#[AutowiredParameter(ref: '%exceptions.implicitThrows%')]
private bool $implicitThrows,
)
{
}
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'unserialize';
}
public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): ?Type
{
$throwsValueErrorForInternalFunctions = !$scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->no();
$args = $funcCall->getArgs();
foreach ($args as $arg) {
if ($arg->unpack || $arg->name !== null) {
if ($this->implicitThrows) {
return new ObjectType(Throwable::class);
}
if (!$throwsValueErrorForInternalFunctions) {
return new VoidType();
}
return $functionReflection->getThrowType();
}
}
if (count($args) < 2) {
if ($this->implicitThrows) {
return new ObjectType(Throwable::class);
}
if (!$throwsValueErrorForInternalFunctions) {
return new VoidType();
}
return new ObjectType(TypeError::class);
}
$optionsType = $scope->getNativeType($args[1]->value);
$constantArrays = $optionsType->getConstantArrays();
if (!$optionsType->isArray()->yes() || count($constantArrays) === 0) {
if ($this->implicitThrows) {
return new ObjectType(Throwable::class);
}
if (!$throwsValueErrorForInternalFunctions) {
return new VoidType();
}
return $functionReflection->getThrowType();
}
$allowsNoClasses = true;
foreach ($constantArrays as $constantArray) {
if (!$this->areOptionsValid($constantArray)) {
if (!$throwsValueErrorForInternalFunctions) {
return new VoidType();
}
return $functionReflection->getThrowType();
}
if ($this->allowsNoClasses($constantArray)) {
continue;
}
$allowsNoClasses = false;
}
if ($allowsNoClasses) {
return new VoidType();
}
if ($this->implicitThrows) {
return new ObjectType(Throwable::class);
}
if (!$throwsValueErrorForInternalFunctions) {
return new VoidType();
}
return new ObjectType(TypeError::class);
}
private function areOptionsValid(ConstantArrayType $options): bool
{
$allowedClassesKey = new ConstantStringType('allowed_classes');
if (!$options->hasOffsetValueType($allowedClassesKey)->no()) {
$allowedClasses = $options->getOffsetValueType($allowedClassesKey);
if (!$allowedClasses->isBoolean()->yes() && !$this->isListOfClassNames($allowedClasses)) {
return false;
}
}
$maxDepthKey = new ConstantStringType('max_depth');
if (!$options->hasOffsetValueType($maxDepthKey)->no()) {
return IntegerRangeType::fromInterval(0, null)->isSuperTypeOf($options->getOffsetValueType($maxDepthKey))->yes();
}
return true;
}
private function isListOfClassNames(Type $type): bool
{
if (!$type->isArray()->yes()) {
return false;
}
if ($type->isIterableAtLeastOnce()->no()) {
return true;
}
$classNames = $type->getIterableValueType()->getConstantStrings();
if (count($classNames) === 0 || !$type->getIterableValueType()->isString()->yes()) {
return false;
}
foreach ($classNames as $className) {
if (preg_match('~^[0-9A-Za-z_\\\\\x80-\xff]*$~', $className->getValue()) !== 1) {
return false;
}
}
return true;
}
/**
* With no allowed classes, objects become __PHP_Incomplete_Class and no
* typed property can reject the data.
*/
private function allowsNoClasses(ConstantArrayType $options): bool
{
$allowedClassesKey = new ConstantStringType('allowed_classes');
if (!$options->hasOffsetValueType($allowedClassesKey)->yes()) {
return false;
}
$allowedClasses = $options->getOffsetValueType($allowedClassesKey);
return $allowedClasses->isFalse()->yes()
|| ($allowedClasses->isArray()->yes() && $allowedClasses->isIterableAtLeastOnce()->no());
}
}