Skip to content

Commit f0cd2b2

Browse files
[VarExporter] dont call userland code with uninitialized objects
1 parent dcd0f29 commit f0cd2b2

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

src/Symfony/Component/VarExporter/Internal/Registry.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,9 @@ public static function getClassReflector($class, $instantiableWithoutConstructor
9393
throw new NotInstantiableTypeException($class);
9494
}
9595
}
96-
if (null !== $proto && !$proto instanceof \Throwable) {
96+
if (null !== $proto && !$proto instanceof \Throwable && !$proto instanceof \Serializable && !\method_exists($class, '__sleep')) {
9797
try {
98-
if (!$proto instanceof \Serializable && !\method_exists($class, '__sleep')) {
99-
serialize($proto);
100-
} elseif ($instantiableWithoutConstructor) {
101-
serialize($reflector->newInstanceWithoutConstructor());
102-
} else {
103-
serialize(unserialize(($proto instanceof \Serializable ? 'C:' : 'O:').\strlen($class).':"'.$class.'":0:{}'));
104-
}
98+
serialize($proto);
10599
} catch (\Exception $e) {
106100
throw new NotInstantiableTypeException($class, $e);
107101
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
4+
$o = \Symfony\Component\VarExporter\Internal\Registry::unserialize([], [
5+
'C:51:"Symfony\\Component\\VarExporter\\Tests\\FooSerializable":20:{a:1:{i:0;s:3:"bar";}}',
6+
]),
7+
null,
8+
[],
9+
$o[0],
10+
[]
11+
);

src/Symfony/Component/VarExporter/Tests/VarExporterTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ public function provideExport()
194194
yield array('wakeup-refl', $value);
195195

196196
yield array('abstract-parent', new ConcreteClass());
197+
198+
yield array('foo-serializable', new FooSerializable('bar'));
197199
}
198200
}
199201

@@ -342,3 +344,28 @@ public function __construct()
342344
$this->setBar(234);
343345
}
344346
}
347+
348+
class FooSerializable implements \Serializable
349+
{
350+
private $foo;
351+
352+
public function __construct(string $foo)
353+
{
354+
$this->foo = $foo;
355+
}
356+
357+
public function getFoo(): string
358+
{
359+
return $this->foo;
360+
}
361+
362+
public function serialize(): string
363+
{
364+
return serialize(array($this->getFoo()));
365+
}
366+
367+
public function unserialize($str)
368+
{
369+
list($this->foo) = unserialize($str);
370+
}
371+
}

0 commit comments

Comments
 (0)