Skip to content
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

Fix: cache - base64 value if it contains \0 and the connection is sqlite3 #54084

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 3 additions & 2 deletions src/Illuminate/Cache/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\PostgresConnection;
use Illuminate\Database\QueryException;
use Illuminate\Database\SQLiteConnection;
use Illuminate\Database\SqlServerConnection;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -487,7 +488,7 @@ protected function serialize($value)
{
$result = serialize($value);

if ($this->connection instanceof PostgresConnection && str_contains($result, "\0")) {
if (($this->connection instanceof PostgresConnection || $this->connection instanceof SQLiteConnection) && str_contains($result, "\0")) {
$result = base64_encode($result);
}

Expand All @@ -502,7 +503,7 @@ protected function serialize($value)
*/
protected function unserialize($value)
{
if ($this->connection instanceof PostgresConnection && ! Str::contains($value, [':', ';'])) {
if (($this->connection instanceof PostgresConnection || $this->connection instanceof SQLiteConnection) && ! Str::contains($value, [':', ';'])) {
$value = base64_decode($value);
}

Expand Down
34 changes: 34 additions & 0 deletions tests/Cache/CacheDatabaseStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Cache\DatabaseStore;
use Illuminate\Database\Connection;
use Illuminate\Database\PostgresConnection;
use Illuminate\Database\SQLiteConnection;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use stdClass;
Expand Down Expand Up @@ -68,6 +69,17 @@ public function testValueIsReturnedOnPostgres()
$this->assertSame('bar', $store->get('foo'));
}

public function testValueIsReturnedOnSqlite()
{
$store = $this->getSqliteStore();
$table = m::mock(stdClass::class);
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$table->shouldReceive('whereIn')->once()->with('key', ['prefixfoo'])->andReturn($table);
$table->shouldReceive('get')->once()->andReturn(collect([(object) ['key' => 'prefixfoo', 'value' => base64_encode(serialize("\0bar\0")), 'expiration' => 999999999999999]]));

$this->assertSame("\0bar\0", $store->get('foo'));
}

public function testValueIsUpserted()
{
$store = $this->getMockBuilder(DatabaseStore::class)->onlyMethods(['getTime'])->setConstructorArgs($this->getMocks())->getMock();
Expand All @@ -92,6 +104,18 @@ public function testValueIsUpsertedOnPostgres()
$this->assertTrue($result);
}

public function testValueIsUpsertedOnSqlite()
{
$store = $this->getMockBuilder(DatabaseStore::class)->onlyMethods(['getTime'])->setConstructorArgs($this->getSqliteMocks())->getMock();
$table = m::mock(stdClass::class);
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
$store->expects($this->once())->method('getTime')->willReturn(1);
$table->shouldReceive('upsert')->once()->with([['key' => 'prefixfoo', 'value' => base64_encode(serialize("\0")), 'expiration' => 61]], 'key')->andReturn(1);

$result = $store->put('foo', "\0", 60);
$this->assertTrue($result);
}

public function testForeverCallsStoreItemWithReallyLongTime()
{
$store = $this->getMockBuilder(DatabaseStore::class)->onlyMethods(['put'])->setConstructorArgs($this->getMocks())->getMock();
Expand Down Expand Up @@ -210,6 +234,11 @@ protected function getPostgresStore()
return new DatabaseStore(m::mock(PostgresConnection::class), 'table', 'prefix');
}

protected function getSqliteStore()
{
return new DatabaseStore(m::mock(SQLiteConnection::class), 'table', 'prefix');
}

protected function getMocks()
{
return [m::mock(Connection::class), 'table', 'prefix'];
Expand All @@ -219,4 +248,9 @@ protected function getPostgresMocks()
{
return [m::mock(PostgresConnection::class), 'table', 'prefix'];
}

protected function getSqliteMocks()
{
return [m::mock(SQLiteConnection::class), 'table', 'prefix'];
}
}