[11.x] Proper rate limiter fix with phpredis serialization/compression enabled#54372
Merged
taylorotwell merged 5 commits intolaravel:11.xfrom Jan 27, 2025
Merged
Conversation
… compression is enabled
6caa278 to
62da8f7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As a follow up from #54307:
As it turns out it is not safe to globally skip phpredis's serialization/compression for numeric values in EVAL script parameters. The result of my investigation (with the help of the phpredis team, see: phpredis/phpredis#2616) concludes the following: when those options are enabled, the framework must not interfere at all. The application code must disable serialization/compression on per key basis where we know that this key is used for increment/decrement or similar operations.
I have found several cases where skipping the serialization/compression for numeric values, causes the RateLimiter to still fail.
One example is when used with MSGPACK. Its a binary format and for example the rate limit counter 1 is stored as 1, but when this counter value is read, it is interpreted as 49.
Proof
For the following test I expect a fresh counter with a rate limit of 1
Redis output looks fine here
Phpunit output reveals the error when we read back the value and phpredis tries to deserialize
Solution
Because the
RateLimiteris built on top of theCachecomponent, I have added this logic in all 3 places where it sets and reads the counter value. As a result the test pass with all combinations of compression/serialization settings locally.In addition a new helper method is provided in the
PacksPhpRedisValuestrait that users can use to skip phpredis serialization/compression on per key basis. This allows full control over this behavior. (fyi, we should mention in the docs that connections with those settings should ideally by only used for caching).