8000 [Serializer] Prevent `GetSetMethodNormalizer` from creating invalid magic method call by klaussilveira · Pull Request #48233 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Serializer] Prevent GetSetMethodNormalizer from creating invalid mag…
…ic method call
  • Loading branch information
klaussilveira authored and nicolas-grekas committed Dec 13, 2022
commit d4e1edd64d8d53379eb940f244739ca52c9f4b37
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,17 @@ protected function getAttributeValue(object $object, string $attribute, string $
$ucfirsted = ucfirst($attribute);
8000
$getter = 'get'.$ucfirsted;
if (\is_callable([$object, $getter])) {
if (method_exists($object, $getter) && \is_callable([$object, $getter])) {
return $object->$getter();
}

$isser = 'is'.$ucfirsted;
if (\is_callable([$object, $isser])) {
if (method_exists($object, $isser) && \is_callable([$object, $isser])) {
return $object->$isser();
}

$haser = 'has'.$ucfirsted;
if (\is_callable([$object, $haser])) {
if (method_exists($object, $haser) && \is_callable([$object, $haser])) {
return $object->$haser();
}

Expand All @@ -152,7 +152,14 @@ protected function setAttributeValue(object $object, string $attribute, $value,
$key = \get_class($object).':'.$setter;

if (!isset(self::$setterAccessibleCache[$key])) {
self::$setterAccessibleCache[$key] = \is_callable([$object, $setter]) && !(new \ReflectionMethod($object, $setter))->isStatic();
try {
// We have to use is_callable() here since method_exists()
// does not "see" protected/private methods
self::$setterAccessibleCache[$key] = \is_callable([$object, $setter]) && !(new \ReflectionMethod($object, $setter))->isStatic();
} catch (\ReflectionException $e) {
// Method does not exist in the class, probably a magic method
self::$setterAccessibleCache[$key] = false;
}
}

if (self::$setterAccessibleCache[$key]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,22 @@ public function testHasGetterNormalize()
);
}

public function testCallMagicMethodDenormalize()
{
$obj = $this->normalizer->denormalize(['active' => true], ObjectWithMagicMethod::class);
$this->assertTrue($obj->isActive());
}

public function testCallMagicMethodNormalize()
{
$obj = new ObjectWithMagicMethod();

$this->assertSame(
['active' => true],
$this->normalizer->normalize($obj, 'any')
);
}

protected function getObjectCollectionWithExpectedArray(): array
{
return [[
Expand Down Expand Up @@ -722,3 +738,18 @@ public function hasFoo()
return $this->foo;
}
}

class ObjectWithMagicMethod
{
private $active = true;

public function isActive()
{
return $this->active;
}

public function __call($key, $value)
{
throw new \RuntimeException('__call should not be called. Called with: '.$key);
}
}
0