Skip to content

Commit

Permalink
stop encrypting database cache values. we dont encrypt for other stores
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Apr 19, 2017
1 parent 91f5357 commit f0c72ec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
);
}
Expand Down
34 changes: 5 additions & 29 deletions src/Illuminate/Cache/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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.
*
Expand All @@ -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;
}

Expand Down Expand Up @@ -89,7 +78,7 @@ public function get($key)
return;
}

return $this->encrypter->decrypt($cache->value);
return unserialize($cache->value);
}

/**
Expand All @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
37 changes: 14 additions & 23 deletions tests/Cache/CacheDatabaseStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -121,30 +118,27 @@ 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();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$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();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$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'));
}

Expand All @@ -163,40 +157,37 @@ 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();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$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();
});
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$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'];
}
}

0 comments on commit f0c72ec

Please sign in to comment.