From f0c72ec9bcbdecb7e6267f7ec8f7ecbf8169a388 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 19 Apr 2017 09:25:34 -0500 Subject: [PATCH] stop encrypting database cache values. we dont encrypt for other stores --- src/Illuminate/Cache/CacheManager.php | 2 +- src/Illuminate/Cache/DatabaseStore.php | 34 ++++------------------- tests/Cache/CacheDatabaseStoreTest.php | 37 ++++++++++---------------- 3 files changed, 20 insertions(+), 53 deletions(-) diff --git a/src/Illuminate/Cache/CacheManager.php b/src/Illuminate/Cache/CacheManager.php index eb06cf4215d7..37a26878a9dd 100755 --- a/src/Illuminate/Cache/CacheManager.php +++ b/src/Illuminate/Cache/CacheManager.php @@ -209,7 +209,7 @@ protected function createDatabaseDriver(array $config) return $this->repository( new DatabaseStore( - $connection, $this->app['encrypter'], $config['table'], $this->getPrefix($config) + $connection, $config['table'], $this->getPrefix($config) ) ); } diff --git a/src/Illuminate/Cache/DatabaseStore.php b/src/Illuminate/Cache/DatabaseStore.php index c120c97582a7..52fcd180411c 100755 --- a/src/Illuminate/Cache/DatabaseStore.php +++ b/src/Illuminate/Cache/DatabaseStore.php @@ -7,7 +7,6 @@ use Carbon\Carbon; use Illuminate\Contracts\Cache\Store; use Illuminate\Database\ConnectionInterface; -use Illuminate\Contracts\Encryption\Encrypter as EncrypterContract; class DatabaseStore implements Store { @@ -20,13 +19,6 @@ class DatabaseStore implements Store */ protected $connection; - /** - * The encrypter instance. - * - * @var \Illuminate\Contracts\Encryption\Encrypter - */ - protected $encrypter; - /** * The name of the cache table. * @@ -45,17 +37,14 @@ class DatabaseStore implements Store * Create a new database store. * * @param \Illuminate\Database\ConnectionInterface $connection - * @param \Illuminate\Contracts\Encryption\Encrypter $encrypter * @param string $table * @param string $prefix * @return void */ - public function __construct(ConnectionInterface $connection, EncrypterContract $encrypter, - $table, $prefix = '') + public function __construct(ConnectionInterface $connection, $table, $prefix = '') { $this->table = $table; $this->prefix = $prefix; - $this->encrypter = $encrypter; $this->connection = $connection; } @@ -89,7 +78,7 @@ public function get($key) return; } - return $this->encrypter->decrypt($cache->value); + return unserialize($cache->value); } /** @@ -104,10 +93,7 @@ public function put($key, $value, $minutes) { $key = $this->prefix.$key; - // All of the cached values in the database are encrypted in case this is used - // as a session data store by the consumer. We'll also calculate the expire - // time and place that on the table so we will check it on our retrieval. - $value = $this->encrypter->encrypt($value); + $value = serialize($value); $expiration = $this->getTime() + (int) ($minutes * 60); @@ -171,7 +157,7 @@ protected function incrementOrDecrement($key, $value, Closure $callback) $cache = is_array($cache) ? (object) $cache : $cache; - $current = $this->encrypter->decrypt($cache->value); + $current = unserialize($cache->value); // Here we'll call this callback function that was given to the function which // is used to either increment or decrement the function. We use a callback @@ -186,7 +172,7 @@ protected function incrementOrDecrement($key, $value, Closure $callback) // since database cache values are encrypted by default with secure storage // that can't be easily read. We will return the new value after storing. $this->table()->where('key', $prefixed)->update([ - 'value' => $this->encrypter->encrypt($new), + 'value' => serialize($new), ]); return $new; @@ -258,16 +244,6 @@ public function getConnection() return $this->connection; } - /** - * Get the encrypter instance. - * - * @return \Illuminate\Contracts\Encryption\Encrypter - */ - public function getEncrypter() - { - return $this->encrypter; - } - /** * Get the cache key prefix. * diff --git a/tests/Cache/CacheDatabaseStoreTest.php b/tests/Cache/CacheDatabaseStoreTest.php index 0ddac655bc43..bfc1cf49a2b3 100755 --- a/tests/Cache/CacheDatabaseStoreTest.php +++ b/tests/Cache/CacheDatabaseStoreTest.php @@ -43,36 +43,33 @@ public function testDecryptedValueIsReturnedWhenItemIsValid() $table = m::mock('StdClass'); $store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table); $table->shouldReceive('where')->once()->with('key', '=', 'prefixfoo')->andReturn($table); - $table->shouldReceive('first')->once()->andReturn((object) ['value' => 'bar', 'expiration' => 999999999999999]); - $store->getEncrypter()->shouldReceive('decrypt')->once()->with('bar')->andReturn('bar'); + $table->shouldReceive('first')->once()->andReturn((object) ['value' => serialize('bar'), 'expiration' => 999999999999999]); $this->assertEquals('bar', $store->get('foo')); } - public function testEncryptedValueIsInsertedWhenNoExceptionsAreThrown() + public function testValueIsInsertedWhenNoExceptionsAreThrown() { $store = $this->getMockBuilder('Illuminate\Cache\DatabaseStore')->setMethods(['getTime'])->setConstructorArgs($this->getMocks())->getMock(); $table = m::mock('StdClass'); $store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table); - $store->getEncrypter()->shouldReceive('encrypt')->once()->with('bar')->andReturn('bar'); $store->expects($this->once())->method('getTime')->will($this->returnValue(1)); - $table->shouldReceive('insert')->once()->with(['key' => 'prefixfoo', 'value' => 'bar', 'expiration' => 61]); + $table->shouldReceive('insert')->once()->with(['key' => 'prefixfoo', 'value' => serialize('bar'), 'expiration' => 61]); $store->put('foo', 'bar', 1); } - public function testEncryptedValueIsUpdatedWhenInsertThrowsException() + public function testValueIsUpdatedWhenInsertThrowsException() { $store = $this->getMockBuilder('Illuminate\Cache\DatabaseStore')->setMethods(['getTime'])->setConstructorArgs($this->getMocks())->getMock(); $table = m::mock('StdClass'); $store->getConnection()->shouldReceive('table')->with('table')->andReturn($table); - $store->getEncrypter()->shouldReceive('encrypt')->once()->with('bar')->andReturn('bar'); $store->expects($this->once())->method('getTime')->will($this->returnValue(1)); - $table->shouldReceive('insert')->once()->with(['key' => 'prefixfoo', 'value' => 'bar', 'expiration' => 61])->andReturnUsing(function () { + $table->shouldReceive('insert')->once()->with(['key' => 'prefixfoo', 'value' => serialize('bar'), 'expiration' => 61])->andReturnUsing(function () { throw new Exception; }); $table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table); - $table->shouldReceive('update')->once()->with(['value' => 'bar', 'expiration' => 61]); + $table->shouldReceive('update')->once()->with(['value' => serialize('bar'), 'expiration' => 61]); $store->put('foo', 'bar', 1); } @@ -121,7 +118,7 @@ public function testIncrementReturnsCorrectValues() $table->shouldReceive('first')->once()->andReturn(null); $this->assertFalse($store->increment('foo')); - $cache->value = 'bar'; + $cache->value = serialize('bar'); $store->getConnection()->shouldReceive('transaction')->once()->with(m::type('Closure'))->andReturnUsing(function ($closure) { return $closure(); }); @@ -129,10 +126,9 @@ public function testIncrementReturnsCorrectValues() $table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table); $table->shouldReceive('lockForUpdate')->once()->andReturn($table); $table->shouldReceive('first')->once()->andReturn($cache); - $store->getEncrypter()->shouldReceive('decrypt')->once()->with('bar')->andReturn('bar'); $this->assertFalse($store->increment('foo')); - $cache->value = 2; + $cache->value = serialize(2); $store->getConnection()->shouldReceive('transaction')->once()->with(m::type('Closure'))->andReturnUsing(function ($closure) { return $closure(); }); @@ -140,11 +136,9 @@ public function testIncrementReturnsCorrectValues() $table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table); $table->shouldReceive('lockForUpdate')->once()->andReturn($table); $table->shouldReceive('first')->once()->andReturn($cache); - $store->getEncrypter()->shouldReceive('decrypt')->once()->with(2)->andReturn(2); - $store->getEncrypter()->shouldReceive('encrypt')->once()->with(3)->andReturn(3); $store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table); $table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table); - $table->shouldReceive('update')->once()->with(['value' => 3]); + $table->shouldReceive('update')->once()->with(['value' => serialize(3)]); $this->assertEquals(3, $store->increment('foo')); } @@ -163,7 +157,7 @@ public function testDecrementReturnsCorrectValues() $table->shouldReceive('first')->once()->andReturn(null); $this->assertFalse($store->decrement('foo')); - $cache->value = 'bar'; + $cache->value = serialize('bar'); $store->getConnection()->shouldReceive('transaction')->once()->with(m::type('Closure'))->andReturnUsing(function ($closure) { return $closure(); }); @@ -171,10 +165,9 @@ public function testDecrementReturnsCorrectValues() $table->shouldReceive('where')->once()->with('key', 'prefixfoo')->andReturn($table); $table->shouldReceive('lockForUpdate')->once()->andReturn($table); $table->shouldReceive('first')->once()->andReturn($cache); - $store->getEncrypter()->shouldReceive('decrypt')->once()->with('bar')->andReturn('bar'); $this->assertFalse($store->decrement('foo')); - $cache->value = 3; + $cache->value = serialize(3); $store->getConnection()->shouldReceive('transaction')->once()->with(m::type('Closure'))->andReturnUsing(function ($closure) { return $closure(); }); @@ -182,21 +175,19 @@ public function testDecrementReturnsCorrectValues() $table->shouldReceive('where')->once()->with('key', 'prefixbar')->andReturn($table); $table->shouldReceive('lockForUpdate')->once()->andReturn($table); $table->shouldReceive('first')->once()->andReturn($cache); - $store->getEncrypter()->shouldReceive('decrypt')->once()->with(3)->andReturn(3); - $store->getEncrypter()->shouldReceive('encrypt')->once()->with(2)->andReturn(2); $store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table); $table->shouldReceive('where')->once()->with('key', 'prefixbar')->andReturn($table); - $table->shouldReceive('update')->once()->with(['value' => 2]); + $table->shouldReceive('update')->once()->with(['value' => serialize(2)]); $this->assertEquals(2, $store->decrement('bar')); } protected function getStore() { - return new DatabaseStore(m::mock('Illuminate\Database\Connection'), m::mock('Illuminate\Contracts\Encryption\Encrypter'), 'table', 'prefix'); + return new DatabaseStore(m::mock('Illuminate\Database\Connection'), 'table', 'prefix'); } protected function getMocks() { - return [m::mock('Illuminate\Database\Connection'), m::mock('Illuminate\Contracts\Encryption\Encrypter'), 'table', 'prefix']; + return [m::mock('Illuminate\Database\Connection'), 'table', 'prefix']; } }