Skip to content

Commit e9c7b8a

Browse files
committed
feat(string): add digest length validation for DelEX
1 parent 8d4d8ea commit e9c7b8a

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/commands/cmd_string.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,17 @@ class CommandDelEX : public Commander {
118118
CommandParser parser(args, 2);
119119
while (parser.Good()) {
120120
if (parser.EatEqICase("ifdeq")) {
121-
option_ = {DelExOption::IFDEQ, GET_OR_RET(parser.TakeStr())};
121+
std::string digest = GET_OR_RET(parser.TakeStr());
122+
if (digest.size() != 16) {
123+
return {Status::RedisParseErr, "ERR digest must be exactly 16 hexadecimal characters"};
124+
}
125+
option_ = {DelExOption::IFDEQ, std::move(digest)};
122126
} else if (parser.EatEqICase("ifdne")) {
123-
option_ = {DelExOption::IFDNE, GET_OR_RET(parser.TakeStr())};
127+
std::string digest = GET_OR_RET(parser.TakeStr());
128+
if (digest.size() != 16) {
129+
return {Status::RedisParseErr, "ERR digest must be exactly 16 hexadecimal characters"};
130+
}
131+
option_ = {DelExOption::IFDNE, std::move(digest)};
124132
} else if (parser.EatEqICase("ifeq")) {
125133
option_ = {DelExOption::IFEQ, GET_OR_RET(parser.TakeStr())};
126134
} else if (parser.EatEqICase("ifne")) {

tests/gocase/unit/type/strings/strings_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,4 +1204,19 @@ func testString(t *testing.T, configs util.KvrocksServerConfigs) {
12041204
require.Equal(t, []redis.LCSMatchedPosition{}, rdb.LCS(ctx, &redis.LCSQuery{Key1: "virus1", Key2: "virus2", Idx: true}).Val().Matches)
12051205
require.Equal(t, []redis.LCSMatchedPosition{}, rdb.LCS(ctx, &redis.LCSQuery{Key1: "virus1", Key2: "virus2", Idx: true, WithMatchLen: true}).Val().Matches)
12061206
})
1207+
1208+
t.Run("DelEX IFDEQ and IFDNE reject invalid digest length", func(t *testing.T) {
1209+
key := "test-string-key-invalid-digest"
1210+
value := "Hello world"
1211+
1212+
require.NoError(t, rdb.Del(ctx, key).Err())
1213+
require.NoError(t, rdb.Set(ctx, key, value, 0).Err())
1214+
require.ErrorContains(t, rdb.Do(ctx, "DelEX", key, "ifdeq", "123456789012345").Err(),
1215+
"exactly 16 hexadecimal characters")
1216+
require.Equal(t, value, rdb.Get(ctx, key).Val())
1217+
1218+
require.ErrorContains(t, rdb.Do(ctx, "DelEX", key, "ifdne", "123456789012345").Err(),
1219+
"exactly 16 hexadecimal characters")
1220+
require.Equal(t, value, rdb.Get(ctx, key).Val())
1221+
})
12071222
}

0 commit comments

Comments
 (0)