From dbfadd4cc34769440376701e6c6c5e68f78e2ddc Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 7 Dec 2019 23:07:35 +0100 Subject: [PATCH] fix invalid value set/add --- DataStorage/Cache/Connection/MemCached.php | 12 ++++++++++ DataStorage/Cache/Connection/RedisCache.php | 12 ++++++++++ .../Cache/Connection/FileCacheTest.php | 16 +++++++++++-- .../Cache/Connection/MemCachedTest.php | 16 +++++++++++-- .../Cache/Connection/RedisCacheTest.php | 23 +++++++++++++++++++ 5 files changed, 75 insertions(+), 4 deletions(-) diff --git a/DataStorage/Cache/Connection/MemCached.php b/DataStorage/Cache/Connection/MemCached.php index 5fb75b55c..bf39a576c 100644 --- a/DataStorage/Cache/Connection/MemCached.php +++ b/DataStorage/Cache/Connection/MemCached.php @@ -76,6 +76,14 @@ class MemCached extends ConnectionAbstract */ public function set($key, $value, int $expire = -1) : void { + if ($this->status !== CacheStatus::OK) { + return; + } + + if (!(\is_scalar($value) || $value instanceof \JsonSerializable || $value instanceof \Serializable)) { + throw new \InvalidArgumentException(); + } + $this->con->set($key, $value, \max($expire, 0)); } @@ -88,6 +96,10 @@ class MemCached extends ConnectionAbstract return false; } + if (!(\is_scalar($value) || $value instanceof \JsonSerializable || $value instanceof \Serializable)) { + throw new \InvalidArgumentException(); + } + return $this->con->add($key, $value, \max($expire, 0)); } diff --git a/DataStorage/Cache/Connection/RedisCache.php b/DataStorage/Cache/Connection/RedisCache.php index 2b87049b9..54560c61c 100644 --- a/DataStorage/Cache/Connection/RedisCache.php +++ b/DataStorage/Cache/Connection/RedisCache.php @@ -91,6 +91,14 @@ class RedisCache extends ConnectionAbstract */ public function set($key, $value, int $expire = -1) : void { + if ($this->status !== CacheStatus::OK) { + return; + } + + if (!(\is_scalar($value) || $value instanceof \JsonSerializable || $value instanceof \Serializable)) { + throw new \InvalidArgumentException(); + } + if ($expire > 0) { $this->con->set($key, $value, $expire); } @@ -107,6 +115,10 @@ class RedisCache extends ConnectionAbstract return false; } + if (!(\is_scalar($value) || $value instanceof \JsonSerializable || $value instanceof \Serializable)) { + throw new \InvalidArgumentException(); + } + if ($expire > 0) { return $this->con->setNx($key, $value, $expire); } diff --git a/tests/DataStorage/Cache/Connection/FileCacheTest.php b/tests/DataStorage/Cache/Connection/FileCacheTest.php index 42f93a3ca..38b1286de 100644 --- a/tests/DataStorage/Cache/Connection/FileCacheTest.php +++ b/tests/DataStorage/Cache/Connection/FileCacheTest.php @@ -340,14 +340,26 @@ class FileCacheTest extends \PHPUnit\Framework\TestCase } /** - * @testdox A invalid data type will throw an InvalidArgumentException + * @testdox Adding a invalid data type will throw an InvalidArgumentException * @covers phpOMS\DataStorage\Cache\Connection\FileCache * @group framework */ - public function testInvalidDataType() : void + public function testInvalidDataTypeAdd() : void { self::expectException(\InvalidArgumentException::class); $this->cache->add('invalid', $this->cache); } + + /** + * @testdox Setting a invalid data type will throw an InvalidArgumentException + * @covers phpOMS\DataStorage\Cache\Connection\FileCache + * @group framework + */ + public function testInvalidDataTypeSet() : void + { + self::expectException(\InvalidArgumentException::class); + + $this->cache->set('invalid', $this->cache); + } } diff --git a/tests/DataStorage/Cache/Connection/MemCachedTest.php b/tests/DataStorage/Cache/Connection/MemCachedTest.php index 901e87cb5..7fa4e21bf 100644 --- a/tests/DataStorage/Cache/Connection/MemCachedTest.php +++ b/tests/DataStorage/Cache/Connection/MemCachedTest.php @@ -265,17 +265,29 @@ class MemCachedTest extends \PHPUnit\Framework\TestCase } /** - * @testdox A invalid data type will throw an InvalidArgumentException + * @testdox Adding a invalid data type will throw an InvalidArgumentException * @covers phpOMS\DataStorage\Cache\Connection\MemCached * @group framework */ - public function testInvalidDataType() : void + public function testInvalidDataTypeAdd() : void { self::expectException(\InvalidArgumentException::class); $this->cache->add('invalid', $this->cache); } + /** + * @testdox Setting a invalid data type will throw an InvalidArgumentException + * @covers phpOMS\DataStorage\Cache\Connection\MemCached + * @group framework + */ + public function testInvalidDataTypeSet() : void + { + self::expectException(\InvalidArgumentException::class); + + $this->cache->set('invalid', $this->cache); + } + /** * @testdox A invalid host throws a InvalidConnectionConfigException * @covers phpOMS\DataStorage\Cache\Connection\MemCached diff --git a/tests/DataStorage/Cache/Connection/RedisCacheTest.php b/tests/DataStorage/Cache/Connection/RedisCacheTest.php index 9d11dcca2..f3cf6d3c7 100644 --- a/tests/DataStorage/Cache/Connection/RedisCacheTest.php +++ b/tests/DataStorage/Cache/Connection/RedisCacheTest.php @@ -329,6 +329,29 @@ class RedisCacheTest extends \PHPUnit\Framework\TestCase self::assertEquals([], $this->cache->stats()); } + /** + * @testdox Adding a invalid data type will throw an InvalidArgumentException + * @covers phpOMS\DataStorage\Cache\Connection\MemCached + * @group framework + */ + public function testInvalidDataTypeAdd() : void + { + self::expectException(\InvalidArgumentException::class); + + $this->cache->add('invalid', $this->cache); + } + + /** + * @testdox Setting a invalid data type will throw an InvalidArgumentException + * @covers phpOMS\DataStorage\Cache\Connection\MemCached + * @group framework + */ + public function testInvalidDataTypeSet() : void + { + self::expectException(\InvalidArgumentException::class); + + $this->cache->set('invalid', $this->cache); + } /** * @testdox A invalid host throws a InvalidConnectionConfigException