Skip to content

Commit f4ecf92

Browse files
Prevent unintended serialization and compression
1 parent 79b44b1 commit f4ecf92

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Illuminate/Cache/RedisStore.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@ public function setPrefix($prefix)
432432
protected function pack($value, $connection)
433433
{
434434
if ($connection instanceof PhpRedisConnection) {
435+
if ($this->storePlainValue($value)) {
436+
return $value;
437+
}
438+
435439
if ($connection->serialized()) {
436440
return $connection->pack([$value])[0];
437441
}
@@ -444,6 +448,17 @@ protected function pack($value, $connection)
444448
return $this->serialize($value);
445449
}
446450

451+
/**
452+
* Determine if the given value should be stored as plain value or be serialized/compressed instead.
453+
*
454+
* @param mixed $value
455+
* @return bool
456+
*/
457+
protected function storePlainValue($value): bool
458+
{
459+
return is_numeric($value) && ! in_array($value, [INF, -INF]) && ! is_nan($value);
460+
}
461+
447462
/**
448463
* Serialize the value.
449464
*
@@ -452,7 +467,7 @@ protected function pack($value, $connection)
452467
*/
453468
protected function serialize($value)
454469
{
455-
return is_numeric($value) && ! in_array($value, [INF, -INF]) && ! is_nan($value) ? $value : serialize($value);
470+
return $this->storePlainValue($value) ? $value : serialize($value);
456471
}
457472

458473
/**

tests/Integration/Cache/RedisStoreTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,20 @@ public function testPutManyCallsPutWhenClustered()
249249
'fizz' => 'buz',
250250
], 10);
251251
}
252+
253+
public function testIncrementWithSerializationEnabled()
254+
{
255+
/** @var \Illuminate\Cache\RedisStore $store */
256+
$store = Cache::store('redis');
257+
/** @var \Redis $client */
258+
$client = $store->connection()->client();
259+
$client->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
260+
261+
$store->flush();
262+
$store->add('foo', 1, 10);
263+
$this->assertEquals(1, $store->get('foo'));
264+
265+
$store->increment('foo');
266+
$this->assertEquals(2, $store->get('foo'));
267+
}
252268
}

0 commit comments

Comments
 (0)