Repository navigation
fix: Check for \r\n when reading the reply type header - #2941
Merged
michael-grunder merged 1 commit intoOct 1, 2026
Merged
Conversation
redis_read_reply_type() read the INT, BULK and MULTIBULK header with redis_sock_get_line() and only checked for NULL. When read_timeout expires in the middle of the line, the partial line is returned, so ":123" cut after ":1" parses as 1 and the rest of the line is read as the reply to the next command. Use redis_sock_gets(), which since 6e13625 fails a line that does not end in \r\n, disconnects and throws.
Member
|
Merged, thanks |
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.
redis_read_reply_type()reads the INT, BULK and MULTIBULK header withredis_sock_get_line()and only checks forNULL. Whenread_timeoutexpires in the middle of that line,redis_sock_get_line()returns the part read so far, so:123cut after:1is parsed as1. The connection stays open and the rest of the line is read as the reply to the next command.6e13625 (#2899) added a CRLF check to
redis_sock_gets(), which fixed the command specific readers. This caller was not changed, sorawCommand(),eval()and the other replies parsed by the generic reader are still affected. This PR reads the header withredis_sock_gets()instead. Becauseredis_sock_gets()strips the trailing\r\n,strtol()parses the same value as before.Before and after
Tested on develop (bac0015) with PHP 8.4.18 CLI. The script at the end needs no Redis server. It starts a small fake server that pauses 1.5 s in the middle of a line, and the client uses a
read_timeoutof 1 s.rawCommand,:123cut after:11rawCommand, 3 element array with a length line cut["aa","2"], thenget('k2')returns'cc'incr,:123cut after:1get, error line of 4094 bytes followed by+INJECTEDIn case B the second
getraises no error. It returns the last element of the previous reply as the value ofk2. Cases C and D were fixed by 6e13625 and are included for reference.Output
develop:
this PR:
The fake server accepts one connection only, so after phpredis disconnects the next command cannot reconnect. That comes from the test server, not from phpredis.
tests/TestRedis.php --class redisagainst Redis 7.4 gives the same result with and without this change: 211 passed, 20 failed, 42 skipped. The 20 failures are session tests that fail the same way on develop in my environment.For 200
EVALcalls on one connection,strace -ccounted 921 system calls on 6.3.0 and also 921 on 6.3.0 with 6e13625 and this change. When the reply is already buffered, the extraredis_check_eof()inredis_sock_gets()returns without a system call.When the wrong reply reaches another request
I checked this with PHP's built in web server and two requests. The first request runs case B and then
get('a')andget('b'). The second request runsget('c')andget('d').connect()get('b')returns"cc"pconnect(), default settingsget('b')returns"cc"pconnect()withredis.pconnect.echo_check_liveness=0orredis.pconnect.pooling_enabled=0get('b')returns"cc"get('c')returns"v-a"andget('d')returns"v-b"Long running PHP processes (Swoole, RoadRunner, queue workers) that keep one
Redisobject, or a pool of them, across requests never go through that liveness check. The next user of the connection gets the leftover replies. That is where we ran into this.6.3.0
6.3.0 has neither fix. There, a single error line longer than the reply buffer also leaves its tail on the socket. With Redis 7.4.11,
eval('return redis.error_reply(ARGV[1])', [str_repeat('x', 4091) . '+INJECTED'], 0)makes the nextget()on that connection return'+INJECTED'. A 6.3.x release with 6e13625 and this change would help.Possibly the same problem in cluster code (not part of this PR)
cluster_send_direct()incluster_library.csends ASKING, READONLY, MULTI and DISCARD to a node and reads the rest of the+OKreply withredis_sock_gets_raw(). That helper callsredis_sock_get_line()directly, without the CRLF check. If a read timeout cuts that line, the rest of it probably stays on the node's socket and is read by the next command on that connection, the same way as above. I have not tested this path and it is outside the scope of this PR. It is worth a look in a separate change.Reproduction script:
php phpredis-repro.php