Skip to content

Commit 85a02ca

Browse files
authored
Encode cache values for SQLite with base64 (#54178)
1 parent 2096aaf commit 85a02ca

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/Illuminate/Cache/DatabaseStore.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\ConnectionInterface;
99
use Illuminate\Database\PostgresConnection;
1010
use Illuminate\Database\QueryException;
11+
use Illuminate\Database\SQLiteConnection;
1112
use Illuminate\Database\SqlServerConnection;
1213
use Illuminate\Support\Arr;
1314
use Illuminate\Support\Collection;
@@ -487,7 +488,7 @@ protected function serialize($value)
487488
{
488489
$result = serialize($value);
489490

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

@@ -502,7 +503,7 @@ protected function serialize($value)
502503
*/
503504
protected function unserialize($value)
504505
{
505-
if ($this->connection instanceof PostgresConnection && ! Str::contains($value, [':', ';'])) {
506+
if (($this->connection instanceof PostgresConnection || $this->connection instanceof SQLiteConnection) && ! Str::contains($value, [':', ';'])) {
506507
$value = base64_decode($value);
507508
}
508509

tests/Cache/CacheDatabaseStoreTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Cache\DatabaseStore;
77
use Illuminate\Database\Connection;
88
use Illuminate\Database\PostgresConnection;
9+
use Illuminate\Database\SQLiteConnection;
910
use Mockery as m;
1011
use PHPUnit\Framework\TestCase;
1112
use stdClass;
@@ -68,6 +69,17 @@ public function testValueIsReturnedOnPostgres()
6869
$this->assertSame('bar', $store->get('foo'));
6970
}
7071

72+
public function testValueIsReturnedOnSqlite()
73+
{
74+
$store = $this->getSqliteStore();
75+
$table = m::mock(stdClass::class);
76+
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
77+
$table->shouldReceive('whereIn')->once()->with('key', ['prefixfoo'])->andReturn($table);
78+
$table->shouldReceive('get')->once()->andReturn(collect([(object) ['key' => 'prefixfoo', 'value' => base64_encode(serialize("\0bar\0")), 'expiration' => 999999999999999]]));
79+
80+
$this->assertSame("\0bar\0", $store->get('foo'));
81+
}
82+
7183
public function testValueIsUpserted()
7284
{
7385
$store = $this->getMockBuilder(DatabaseStore::class)->onlyMethods(['getTime'])->setConstructorArgs($this->getMocks())->getMock();
@@ -92,6 +104,18 @@ public function testValueIsUpsertedOnPostgres()
92104
$this->assertTrue($result);
93105
}
94106

107+
public function testValueIsUpsertedOnSqlite()
108+
{
109+
$store = $this->getMockBuilder(DatabaseStore::class)->onlyMethods(['getTime'])->setConstructorArgs($this->getSqliteMocks())->getMock();
110+
$table = m::mock(stdClass::class);
111+
$store->getConnection()->shouldReceive('table')->once()->with('table')->andReturn($table);
112+
$store->expects($this->once())->method('getTime')->willReturn(1);
113+
$table->shouldReceive('upsert')->once()->with([['key' => 'prefixfoo', 'value' => base64_encode(serialize("\0")), 'expiration' => 61]], 'key')->andReturn(1);
114+
115+
$result = $store->put('foo', "\0", 60);
116+
$this->assertTrue($result);
117+
}
118+
95119
public function testForeverCallsStoreItemWithReallyLongTime()
96120
{
97121
$store = $this->getMockBuilder(DatabaseStore::class)->onlyMethods(['put'])->setConstructorArgs($this->getMocks())->getMock();
@@ -210,6 +234,11 @@ protected function getPostgresStore()
210234
return new DatabaseStore(m::mock(PostgresConnection::class), 'table', 'prefix');
211235
}
212236

237+
protected function getSqliteStore()
238+
{
239+
return new DatabaseStore(m::mock(SQLiteConnection::class), 'table', 'prefix');
240+
}
241+
213242
protected function getMocks()
214243
{
215244
return [m::mock(Connection::class), 'table', 'prefix'];
@@ -219,4 +248,9 @@ protected function getPostgresMocks()
219248
{
220249
return [m::mock(PostgresConnection::class), 'table', 'prefix'];
221250
}
251+
252+
protected function getSqliteMocks()
253+
{
254+
return [m::mock(SQLiteConnection::class), 'table', 'prefix'];
255+
}
222256
}

0 commit comments

Comments
 (0)