Skip to content

Conversation

@JeppeKnockaert
Copy link
Contributor

This pull request fixes an issue when using serialization and compression combined with the increment function, introduced in #54221.

This is something used by the RateLimiter and causes it to fail since the v11.39.0.

This PR is intended to fix #54307.

@JeppeKnockaert JeppeKnockaert force-pushed the fix-cache-serialization branch from 6b30df9 to f4ecf92 Compare January 24, 2025 12:25
*/
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

@TheLevti
Copy link
Contributor

Is it possible to contribute here or should I open a new pull request on my own?

@taylorotwell
Copy link
Member

@TheLevti what needs to be changed?

@TheLevti
Copy link
Contributor

TheLevti commented Jan 24, 2025

@TheLevti what needs to be changed?

I just confirmed with the maintainers of phpredis, that this behaviour is by design. A value stored with serialization/compression, can not be incremented/decremented.

So for us, inside the rate limit component when we set the initial counter value, we need to skip serialization and store the value as is (when serialization/compression is enabled).

Solution is basically similar to what is done here. Instead of globally skipping the pack call for any Cache component usage, we should do this only when used by the RateLimiter component. (for setting the initial counter value)

@taylorotwell
Copy link
Member

@TheLevti is there a big issue with just doing it globally in the cache component?

@TheLevti
Copy link
Contributor

@TheLevti is there a big issue with just doing it globally in the cache component?

Also checked that. Theoretically possible even though it would be not correct. phpredis will return the value as is if the value can not be deserialized/uncompressed.

We can come up with a smarter solution later and use this proposal for now.

@TheLevti
Copy link
Contributor

<?php

$r = new Redis;
$r->connect('localhost', 6379);

shell_exec('redis-cli set foo not-valid-serialization');

$r->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP);

var_dump($r->get('foo'));
❯ php /tmp/fallback.php
string(23) "not-valid-serialization"

So it will work

@TheLevti
Copy link
Contributor

TheLevti commented Jan 24, 2025

I have used the following test in RedisCacheIntegrationTest to confirm the rate limiter works with this changes:

    /**
     * @param  string  $driver
     */
    #[DataProvider('redisDriverProvider')]
    public function testRedisCacheRateLimiter($driver)
    {
        $store = new RedisStore($this->redis[$driver]);
        $repository = new Repository($store);
        $rateLimiter = new RateLimiter($repository);

        $this->assertFalse($rateLimiter->tooManyAttempts('key', 1));
        $this->assertEquals(1, $rateLimiter->hit('key', 60));
        $this->assertTrue($rateLimiter->tooManyAttempts('key', 1));
    }

@taylorotwell taylorotwell merged commit 48b82c2 into laravel:11.x Jan 24, 2025
38 checks passed
@JeppeKnockaert JeppeKnockaert deleted the fix-cache-serialization branch January 27, 2025 08:55
@vlakoff
Copy link
Contributor

vlakoff commented Feb 4, 2025

You could make use of is_finite():

Changing this:

return is_numeric($value) && ! in_array($value, [INF, -INF]) && ! is_nan($value);

To this:

return is_numeric($value) && is_finite($value);

@TheLevti
Copy link
Contributor

TheLevti commented Feb 4, 2025

You could make use of is_finite():

Changing this:

return is_numeric($value) && ! in_array($value, [INF, -INF]) && ! is_nan($value);

To this:

return is_numeric($value) && is_finite($value);

Ah nice, did not know about this function. Will try to remember on next iteration of improvements on this part ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rate limiter no longer works when compression/serialization is enabled since v11.39.0

4 participants