[PropertyAccessor] fix encoding of cache keys

This commit is contained in:
Nicolas Grekas 2018-11-25 11:59:00 +01:00
parent 7f310b4c77
commit 4568a0c904
2 changed files with 13 additions and 3 deletions

View File

@ -508,7 +508,7 @@ class PropertyAccessor implements PropertyAccessorInterface
*/ */
private function getReadAccessInfo($class, $property) private function getReadAccessInfo($class, $property)
{ {
$key = (false !== strpos($class, '@') ? rawurlencode($class) : $class).'..'.$property; $key = false !== strpbrk($key = $class.'..'.$property, '{}()/@:') ? rawurlencode($key) : $key;
if (isset($this->readPropertyCache[$key])) { if (isset($this->readPropertyCache[$key])) {
return $this->readPropertyCache[$key]; return $this->readPropertyCache[$key];
@ -687,7 +687,7 @@ class PropertyAccessor implements PropertyAccessorInterface
*/ */
private function getWriteAccessInfo($class, $property, $value) private function getWriteAccessInfo($class, $property, $value)
{ {
$key = (false !== strpos($class, '@') ? rawurlencode($class) : $class).'..'.$property; $key = false !== strpbrk($key = $class.'..'.$property, '{}()/@:') ? rawurlencode($key) : $key;
if (isset($this->writePropertyCache[$key])) { if (isset($this->writePropertyCache[$key])) {
return $this->writePropertyCache[$key]; return $this->writePropertyCache[$key];
@ -868,7 +868,8 @@ class PropertyAccessor implements PropertyAccessorInterface
} }
if ($this->cacheItemPool) { if ($this->cacheItemPool) {
$item = $this->cacheItemPool->getItem(self::CACHE_PREFIX_PROPERTY_PATH.$propertyPath); $key = false !== strpbrk($propertyPath, '{}()/@:') ? rawurlencode($propertyPath) : $propertyPath;
$item = $this->cacheItemPool->getItem(self::CACHE_PREFIX_PROPERTY_PATH.str_replace('\\', '.', $key));
if ($item->isHit()) { if ($item->isHit()) {
return $this->propertyPathCache[$propertyPath] = $item->get(); return $this->propertyPathCache[$propertyPath] = $item->get();
} }

View File

@ -582,6 +582,15 @@ class PropertyAccessorTest extends TestCase
$this->assertEquals('baz', $propertyAccessor->getValue($obj, 'publicGetSetter')); $this->assertEquals('baz', $propertyAccessor->getValue($obj, 'publicGetSetter'));
} }
public function testAttributeWithSpecialChars()
{
$obj = new \stdClass();
$obj->{'@foo'} = 'bar';
$propertyAccessor = new PropertyAccessor(false, false, new ArrayAdapter());
$this->assertSame('bar', $propertyAccessor->getValue($obj, '@foo'));
}
/** /**
* @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidArgumentException * @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidArgumentException
* @expectedExceptionMessage Expected argument of type "Countable", "string" given * @expectedExceptionMessage Expected argument of type "Countable", "string" given