Repository navigation
Close the socket when a reply is only partly read - #2961
Closed
SnowySailor wants to merge 4 commits into
Closed
SnowySailor wants to merge 4 commits into
SnowySailor wants to merge 4 commits into
Conversation
When a read fails or times out in the middle of a reply, the rest of the reply can still arrive. If the socket stays open, the next command reads it as its own reply and the connection stays out of step. Disconnect wherever we give up before the end of a reply, the same way redis_sock_gets() already does: * a short read in redis_sock_read_bulk_reply() and redis_sock_read_bulk_zstr() * a timeout before the reply type byte in redis_read_reply_type() * an unknown reply type byte in redis_sock_read() and redis_sock_read_zstr() * any failed read in cluster_check_response(), which now also rejects unknown reply type bytes * a mismatched reply in cluster_send_direct(), and a mismatched reply with an unread payload in cluster_send_slot() Fixes phpredis#2960
2 tasks done
A bulk length that doesn't parse or is out of range, or a bulk body that doesn't end in \r\n, means we no longer know where the next reply starts. Throwing or returning NULL with the socket still open lets the next command read the rest of this reply as its own. Disconnect in these cases as well: * an invalid bulk length in redis_sock_read_bulk_reply(), redis_sock_read_bulk_zstr() and redis_read_variant_bulk() * a bulk body not followed by \r\n in redis_sock_read_bulk_reply() and redis_sock_read_bulk_zstr() * a `$` length that doesn't parse or is out of range in redis_sock_read() and redis_sock_read_zstr() * a length that doesn't parse or is out of range in redis_read_reply_type() Thanks to cyppe for finding the invalid bulk length and missing \r\n checks in redis_sock_read_bulk_reply().
Previously we could leak our running transaction data if we disconnected during a transaction. Also don't accept partial cluster transaction replies.
Member
|
Thanks for the PR! It's got merge conflicts and I wasn't able to resolve them in GitHub's UI. Going to merge your commits in a new PR. |
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.
Fixes #2960
When a read fails or times out partway through a reply, phpredis throws but leaves the socket open. The rest of the reply arrives later, and the next command reads it as its own reply, so the connection stays out of step. This PR disconnects wherever phpredis gives up before the end of a reply, the same way
redis_sock_gets()already does:redis_sock_read_bulk_reply()andredis_sock_read_bulk_zstr()redis_read_reply_type()redis_sock_read()andredis_sock_read_zstr()cluster_check_response(), which now also rejects unknown reply type bytescluster_send_direct(), and a mismatched reply with an unread payload incluster_send_slot()A second commit also disconnects when a bulk length or terminator is invalid, because phpredis then no longer knows where the next reply starts:
redis_sock_read_bulk_reply(),redis_sock_read_bulk_zstr()andredis_read_variant_bulk()\r\ninredis_sock_read_bulk_reply()andredis_sock_read_bulk_zstr()$length that doesn't parse or is out of range inredis_sock_read()andredis_sock_read_zstr()redis_read_reply_type()Testing
RedisandRedisCluster, with binary and CRLF-text values: after the change, the command after the timeout reconnects and gets its own reply.RedisCluster::echo(), andRedis::rawCommand()with a timeout before the type byte.tests/TestRedis.php --class Redisand--class RedisCluster(6-node cluster with thephpredisACL user, as in CI) give the same results with and without the change. The only failures are session tests,testFailedTransactions, andtestQueuedReplyModes, which fail in my local environment ondeveloptoo.$-5, or$abc, called throughget(),rawCommand()andmget(). Before the change, the next commands read the leftover bytes or an earlier command's reply. After it, the next command reconnects and gets its own reply.--class Redis(Valkey 9.0) and--class RedisCluster(one-node cluster) give the same results with and without this commit.There is no new unit test, because the bug needs a server that stops partway through a reply. I can add one if you have a preferred way to do that in the test suite.