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

[11.x] Prevent unintended serialization and compression #54337

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 16 additions & 1 deletion src/Illuminate/Cache/RedisStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ public function setPrefix($prefix)
protected function pack($value, $connection)
{
if ($connection instanceof PhpRedisConnection) {
if ($this->storePlainValue($value)) {
return $value;
}

if ($connection->serialized()) {
return $connection->pack([$value])[0];
}
Expand All @@ -444,6 +448,17 @@ protected function pack($value, $connection)
return $this->serialize($value);
}

/**
* Determine if the given value should be stored as plain value or be serialized/compressed instead.
*
* @param mixed $value
* @return bool
*/
protected function storePlainValue($value): bool
{
return is_numeric($value) && ! in_array($value, [INF, -INF]) && ! is_nan($value);
Copy link
Contributor

@TheLevti TheLevti Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wait with this solution. I need to verify with the phpredis maintainer. You could break the cache component once again when serialization/compression is enabled.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can merge this one

}

/**
* Serialize the value.
*
Expand All @@ -452,7 +467,7 @@ protected function pack($value, $connection)
*/
protected function serialize($value)
{
return is_numeric($value) && ! in_array($value, [INF, -INF]) && ! is_nan($value) ? $value : serialize($value);
return $this->storePlainValue($value) ? $value : serialize($value);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/Cache/RedisStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,20 @@ public function testPutManyCallsPutWhenClustered()
'fizz' => 'buz',
], 10);
}

public function testIncrementWithSerializationEnabled()
{
/** @var \Illuminate\Cache\RedisStore $store */
$store = Cache::store('redis');
/** @var \Redis $client */
$client = $store->connection()->client();
$client->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);

$store->flush();
$store->add('foo', 1, 10);
$this->assertEquals(1, $store->get('foo'));

$store->increment('foo');
$this->assertEquals(2, $store->get('foo'));
}
}