8000 [Cache] Handle unserialize() failures gracefully by nicolas-grekas · Pull Request #19567 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Handle unserialize() failures gracefully #19567

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

Merged
merged 1 commit into from
Aug 13, 2016
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
46 changes: 43 additions & 3 deletions src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,33 @@ public function __destruct()
}
}

/**
* Like the native unserialize() function but throws an exception if anything goes wrong.
*
* @param string $value
*
* @return mixed
*
* @throws \Exception
*/
protected static function unserialize($value)
{
if ('b:0;' === $value) {
return false;
}
$unserializeCallbackHandler = ini_set('unserialize_callback_func', __CLASS__.'::handleUnserializeCallback');
try {
if (false !== $value = unserialize($value)) {
return $value;
}
throw new \DomainException('Failed to unserialize cached value');
} catch (\Error $e) {
throw new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine());
} finally {
ini_set('unserialize_callback_func', $unserializeCallbackHandler);
}
}

private function getId($key)
{
CacheItem::validateKey($key);
Expand All @@ -361,13 +388,26 @@ private function generateItems($items, &$keys)
{
$f = $this->createCacheItem;

foreach ($items as $id => $value) {
yield $keys[$id] => $f($keys[$id], $value, true);
unset($keys[$id]);
try {
foreach ($items as $id => $value) {
$key = $keys[$id];
unset($keys[$id]);
yield $key => $f($key, $value, true);
}
} catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to fetch requested items', array('keys' => array_values($keys), 'exception' => $e));
}

foreach ($keys as $key) {
yield $key => $f($key, null, false);
}
}

/**
* @internal
*/
public static function handleUnserializeCallback($class)
{
throw new \DomainException('Class not found: '.$class);
}
}
6 changes: 5 additions & 1 deletion src/Symfony/Component/Cache/Adapter/ApcuAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public function __construct($namespace = '', $defaultLifetime = 0, $version = nu
*/
protected function doFetch(array $ids)
{
return apcu_fetch($ids);
try {
return apcu_fetch($ids);
} catch (\Error $e) {
throw new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine());
}
}

/**
Expand Down
46 changes: 35 additions & 11 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,22 @@ function ($key, $value, $isHit) use ($defaultLifetime) {
*/
public function getItem($key)
{
if (!$isHit = $this->hasItem($key)) {
$isHit = $this->hasItem($key);
try {
if (!$isHit) {
$value = null;
} elseif (!$this->storeSerialized) {
$value = $this->values[$key];
} elseif ('b:0;' === $value = $this->values[$key]) {
$value = false;
} elseif (false === $value = unserialize($value)) {
$value = null;
$isHit = false;
}
} catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to unserialize key "{key}"', array('key' => $key, 'exception' => $e));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not happen as this adapter is not cross requests (so something not unserializable won't be cached as it is not serializable as well).
If you want to keep this anyway then i guess you should use parent::unserialize below ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok got it it might break in case of a custom unserializer.
At least using parent::unserialize would allow you to remove the case managing false.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no parent here, and calling the same implementation would call ini_set twice for something that can't happen because as you said, this is not cross reqs

Copy link
Contributor
@GuilhemN GuilhemN Aug 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right sorry, i should definitely stop reviewing on my phone...

$value = null;
} elseif ($this->storeSerialized) {
$value = unserialize($this->values[$key]);
} else {
$value = $this->values[$key];
$isHit = false;
}
$f = $this->createCacheItem;

Expand Down Expand Up @@ -181,16 +191,30 @@ private function generateItems(array $keys, $now)
{
$f = $this->createCacheItem;

foreach ($keys as $key) {
if (!$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] >= $now || !$this->deleteItem($key))) {
foreach ($keys as $i => $key) {
try {
if (!$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] >= $now || !$this->deleteItem($key))) {
$value = null;
} elseif (!$this->storeSerialized) {
$value = $this->values[$key];
} elseif ('b:0;' === $value = $this->values[$key]) {
$value = false;
} elseif (false === $value = unserialize($value)) {
$value = null;
$isHit = false;
}
} catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to unserialize key "{key}"', array('key' => $key, 'exception' => $e));
$value = null;
} elseif ($this->storeSerialized) {
$value = unserialize($this->values[$key]);
} else {
$value = $this->values[$key];
$isHit = false;
}
unset($keys[$i]);

yield $key => $f($key, $value, $isHit);
}

foreach ($keys as $key) {
yield $key => $f($key, null, false);
}
}
}
20 changes: 19 additions & 1 deletion src/Symfony/Component/Cache/Adapter/DoctrineAdapter.php
< A3E2 td id="diff-d67a231c1ccb1e3dd1a22bd368cdf94c095f77425b30de6021f9b8da555dd2faR55" data-line-number="55" class="blob-num blob-num-context js-linkable-line-number js-blob-rnum">
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,25 @@ public function __construct(CacheProvider $provider, $namespace = '', $defaultLi
*/
protected function doFetch(array $ids)
{
return $this->provider->fetchMultiple($ids);
$unserializeCallbackHandler = ini_set('unserialize_callback_func', parent::class.'::handleUnserializeCallback');
try {
return $this->provider->fetchMultiple($ids);
} catch (\Error $e) {
$trace = $e->getTrace();

if (isset($trace[0]['function']) && !isset($trace[0]['class'])) {
switch ($trace[0]['function']) {
case 'unserialize':
case 'apcu_fetch':
case 'apc_fetch':
throw new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine());
}
}

throw $e;
} finally {
ini_set('unserialize_callback_func', $unserializeCallbackHandler);
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected function doFetch(array $ids)

foreach ($ids as $id) {
$file = $this->getFile($id);
if (!$h = @fopen($file, 'rb')) {
if (!file_exists($file) || !$h = @fopen($file, 'rb')) {
continue;
}
if ($now >= (int) $expiresAt = fgets($h)) {
Expand All @@ -73,7 +73,7 @@ protected function doFetch(array $ids)
$value = stream_get_contents($h);
fclose($h);
if ($i === $id) {
$values[$id] = unserialize($value);
$values[$id] = parent::unserialize($value);
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/Symfony/Component/Cache/Adapter/RedisAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,15 @@ public static function createConnection($dsn, array $options = array())
*/
protected function doFetch(array $ids)
{
$result = array();

if ($ids) {
$values = $this->redis->mGet($ids);
$index = 0;
foreach ($ids as $id) {
if ($value = $values[$index++]) {
$result[$id] = unserialize($value);
yield $id => parent::unserialize($value);
}
}
}

return $result;
}

/**
Expand Down
38 changes: 38 additions & 0 deletions src/Symfony/Component/Cache/Tests/Adapter/AdapterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,42 @@ public function testDefaultLifeTime()
$item = $cache->getItem('key.dlt');
$this->assertFalse($item->isHit());
}

public function testNotUnserializable()
{
if (isset($this->skippedTests[__FUNCTION__])) {
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);

return;
}

$cache = $this->createCachePool();

$item = $cache->getItem('foo');
$cache->save($item->set(new NotUnserializable()));

$item = $cache->getItem('foo');
$this->assertFalse($item->isHit());

foreach ($cache->getItems(array('foo')) as $item) {
}
$cache->save($item->set(new NotUnserializable()));

foreach ($cache->getItems(array('foo')) as $item) {
}
$this->assertFalse($item->isHit());
}
}

class NotUnserializable implements \Serializable
{
public function serialize()
{
return serialize(123);
}

public function unserialize($ser)
{
throw new \Exception(__CLASS__);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class DoctrineAdapterTest extends AdapterTestCase
protected $skippedTests = array(
'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayCache is not.',
'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayCache is not.',
'testNotUnserializable' => 'ArrayCache does not use serialize/unserialize',
);

public function createCachePool($defaultLifetime = 0)
Expand Down
0