Skip to content

Commit 48b82c2

Browse files
[11.x] Prevent unintended serialization and compression (#54337)
* Prevent unintended serialization and compression * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent b030dbb commit 48b82c2

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->shouldBeStoredWithoutSerialization($value)) {
436+
return $value;
437+
}
438+
435439
if ($connection->serialized()) {
436440
return $connection->pack([$value])[0];
437441
}
@@ -452,7 +456,18 @@ protected function pack($value, $connection)
452456
*/
453457
protected function serialize($value)
454458
{
455-
return is_numeric($value) && ! in_array($value, [INF, -INF]) && ! is_nan($value) ? $value : serialize($value);
459+
return $this->shouldBeStoredWithoutSerialization($value) ? $value : serialize($value);
460+
}
461+
462+
/**
463+
* Determine if the given value should be stored as plain value.
464+
*
465+
* @param mixed $value
466+
* @return bool
467+
*/
468+
protected function shouldBeStoredWithoutSerialization($value): bool
469+
{
470+
return is_numeric($value) && ! in_array($value, [INF, -INF]) && ! is_nan($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)