Skip to content

Commit

Permalink
Encode cache values for SQLite with base64 (#54178)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkiss authored Jan 13, 2025
1 parent 2096aaf commit 85a02ca
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
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'];
}
}

0 comments on commit 85a02ca

Please sign in to comment.