Skip to content

Commit bb738ae

Browse files
committed
[SF-11323] disallow sending command getkeys
1 parent 8a5dada commit bb738ae

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

redis/connection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,14 @@ def pack_command(self, *args):
878878
elif b" " in args[0]:
879879
args = tuple(args[0].split()) + args[1:]
880880

881+
# `COMMAND GETKEYS` can crash redis server entirely under certain conditions.
882+
# So we have decided to make sure that `COMMAND GETKEYS` is never sent to the server.
883+
# If you need to send `COMMAND GETKEYS` to the server, please reach out to Doogie
884+
# and Zach to discuss the use case.
885+
# ref: https://github.com/redis/redis/pull/12380
886+
if len(args) > 1 and args[0].lower() == b'command' and args[1].lower().startswith(b'getkeys'):
887+
raise Exception(f'Redis command "{args[0].decode()} {args[1].decode()}" is not supported')
888+
881889
buff = SYM_EMPTY.join((SYM_STAR, str(len(args)).encode(), SYM_CRLF))
882890

883891
buffer_cutoff = self._buffer_cutoff

tests/test_commands.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4585,21 +4585,22 @@ def test_command_list(self, r: redis.Redis):
45854585
@skip_if_server_version_lt("2.8.13")
45864586
@skip_if_redis_enterprise()
45874587
def test_command_getkeys(self, r):
4588-
res = r.command_getkeys("MSET", "a", "b", "c", "d", "e", "f")
4589-
assert res == ["a", "c", "e"]
4590-
res = r.command_getkeys(
4591-
"EVAL",
4592-
'"not consulted"',
4593-
"3",
4594-
"key1",
4595-
"key2",
4596-
"key3",
4597-
"arg1",
4598-
"arg2",
4599-
"arg3",
4600-
"argN",
4601-
)
4602-
assert res == ["key1", "key2", "key3"]
4588+
with pytest.raises(Exception):
4589+
res = r.command_getkeys("MSET", "a", "b", "c", "d", "e", "f")
4590+
assert res == ["a", "c", "e"]
4591+
res = r.command_getkeys(
4592+
"EVAL",
4593+
'"not consulted"',
4594+
"3",
4595+
"key1",
4596+
"key2",
4597+
"key3",
4598+
"arg1",
4599+
"arg2",
4600+
"arg3",
4601+
"argN",
4602+
)
4603+
assert res == ["key1", "key2", "key3"]
46034604

46044605
@skip_if_server_version_lt("2.8.13")
46054606
def test_command(self, r):
@@ -4613,13 +4614,14 @@ def test_command(self, r):
46134614
@skip_if_server_version_lt("7.0.0")
46144615
@skip_if_redis_enterprise()
46154616
def test_command_getkeysandflags(self, r: redis.Redis):
4616-
res = [
4617-
[b"mylist1", [b"RW", b"access", b"delete"]],
4618-
[b"mylist2", [b"RW", b"insert"]],
4619-
]
4620-
assert res == r.command_getkeysandflags(
4621-
"LMOVE", "mylist1", "mylist2", "left", "left"
4622-
)
4617+
with pytest.raises(Exception):
4618+
res = [
4619+
[b"mylist1", [b"RW", b"access", b"delete"]],
4620+
[b"mylist2", [b"RW", b"insert"]],
4621+
]
4622+
assert res == r.command_getkeysandflags(
4623+
"LMOVE", "mylist1", "mylist2", "left", "left"
4624+
)
46234625

46244626
@pytest.mark.onlynoncluster
46254627
@skip_if_server_version_lt("4.0.0")

0 commit comments

Comments
 (0)