Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(redis): classify RedisJSON and other module read commands as read-only (#21196)

Vincent Huang9e312fdc42df
diff --git a/backend/plugin/parser/redis/query.go b/backend/plugin/parser/redis/query.go
index 2d5274c55e..5cb27b13bc 100644
--- a/backend/plugin/parser/redis/query.go
+++ b/backend/plugin/parser/redis/query.go
@@ -10,6 +10,16 @@ import (
"github.com/bytebase/bytebase/backend/plugin/parser/base"
)
+// readCommands holds the core Redis commands that the server flags "readonly"
+// in its own command table. Redis has no read-only transaction or connection
+// mode, so the SQL Editor read-only gate has to classify by command name.
+//
+// TestReadCommandsMatchServerFlags diffs this map against
+// testdata/core_readonly_commands.txt, a snapshot of `COMMAND INFO` from the
+// pinned Redis in that file's header. Regenerate the snapshot when bumping the
+// pinned version; the test then reports every newly shipped read command.
+// Commands left out on purpose live in that test's knownExclusions, with the
+// reason.
var readCommands = map[string]bool{
"bitcount": true,
"bitfield_ro": true,
@@ -28,15 +38,19 @@ var readCommands = map[string]bool{
"geosearch": true,
"getbit": true,
"hexists": true,
+ "hexpiretime": true,
"hget": true,
"hgetall": true,
"hkeys": true,
"hmget": true,
"hlen": true,
+ "hpexpiretime": true,
+ "hpttl": true,
"hrandfield": true,
"hscan": true,
"hvals": true,
"hstrlen": true,
+ "httl": true,
"keys": true,
"lcs": true,
"lindex": true,
@@ -53,7 +67,6 @@ var readCommands = map[string]bool{
"sscan": true,
"scard": true,
"sdiff": true,
- "select": true,
"smismember": true,
"sismember": true,
"sinter": true,
@@ -110,6 +123,85 @@ var doubleNameReadCommands = map[string]map[string]bool{
},
}
+// moduleReadCommands holds the read-only commands of the Redis Stack modules.
+// Module commands namespace with a dot inside a single token ("JSON.GET"), so
+// they cannot go through doubleNameReadCommands, which expects the subcommand
+// as a second token ("XINFO STREAM").
+//
+// The server's "readonly" flag means "does not write the keyspace", not "has no
+// side effects", so it is not enough on its own here. RediSearch flags
+// FT.ALIASADD, FT.DICTADD, FT.DICTDEL, FT.CONFIG and FT.CURSOR readonly even
+// though they mutate index or config state, and RedisBloom flags CF.COMPACT
+// readonly even though it rewrites the filter. Those stay out. So do the
+// modules' internal cluster commands.
+var moduleReadCommands = map[string]bool{
+ // RedisJSON.
+ "json.arrindex": true,
+ "json.arrlen": true,
+ "json.debug": true,
+ "json.get": true,
+ "json.mget": true,
+ "json.objkeys": true,
+ "json.objlen": true,
+ "json.resp": true,
+ "json.strlen": true,
+ "json.type": true,
+
+ // RedisTimeSeries.
+ "ts.get": true,
+ "ts.info": true,
+ "ts.mget": true,
+ "ts.mrange": true,
+ "ts.mrevrange": true,
+ "ts.queryindex": true,
+ "ts.range": true,
+ "ts.revrange": true,
+
+ // RedisBloom: Bloom filter, Cuckoo filter, Count-Min sketch, Top-K,
+ // t-digest. The scandump commands stay out: they page through internal
+ // chunks for replication, not for reading data.
+ "bf.card": true,
+ "bf.exists": true,
+ "bf.info": true,
+ "bf.mexists": true,
+ "cf.count": true,
+ "cf.exists": true,
+ "cf.info": true,
+ "cf.mexists": true,
+ "cms.info": true,
+ "cms.query": true,
+ "topk.info": true,
+ "topk.list": true,
+ "topk.query": true,
+ "tdigest.byrank": true,
+ "tdigest.byrevrank": true,
+ "tdigest.cdf": true,
+ "tdigest.info": true,
+ "tdigest.max": true,
+ "tdigest.min": true,
+ "tdigest.quantile": true,
+ "tdigest.rank": true,
+ "tdigest.revrank": true,
+ "tdigest.trimmed_mean": true,
+
+ // RediSearch. FT.AGGREGATE with WITHCURSOR leaves a server-side cursor
+ // behind, and FT.CURSOR is not allowed here, so such a cursor is only
+ // reclaimed when it times out.
+ "ft.aggregate": true,
+ "ft.dictdump": true,
+ "ft.explain": true,
+ "ft.get": true,
+ "ft.info": true,
+ "ft.mget": true,
+ "ft.profile": true,
+ "ft.search": true,
+ "ft.spellcheck": true,
+ "ft.sugget": true,
+ "ft.suglen": true,
+ "ft.syndump": true,
+ "ft.tagvals": true,
+}
+
func init() {
base.RegisterQueryValidator(storepb.Engine_REDIS, validateQuery)
}
@@ -142,7 +234,7 @@ func isReadCommand(fields []string) bool {
return false
}
command := strings.ToLower(fields[0])
- if _, ok := readCommands[command]; ok {
+ if readCommands[command] || moduleReadCommands[command] {
return true
}
if doubleNameReadCommands[command] != nil && len(fields) > 1 {
diff --git a/backend/plugin/parser/redis/query_test.go b/backend/plugin/parser/redis/query_test.go
index 8e5c08fec0..e4eb4b3e0a 100644
--- a/backend/plugin/parser/redis/query_test.go
+++ b/backend/plugin/parser/redis/query_test.go
@@ -1,8 +1,12 @@
package redis
import (
+ "os"
+ "slices"
+ "strings"
"testing"
+ "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -28,6 +32,93 @@ func TestValidateQuery(t *testing.T) {
valid: true,
allQuery: true,
},
+ // RedisJSON reads. The command namespaces with a dot inside one token,
+ // so it can only match readCommands/moduleReadCommands, never the
+ // container-subcommand fallback.
+ {
+ statement: "JSON.GET doc",
+ valid: true,
+ allQuery: true,
+ },
+ {
+ statement: "json.get doc $.name",
+ valid: true,
+ allQuery: true,
+ },
+ {
+ statement: "JSON.DEBUG MEMORY doc",
+ valid: true,
+ allQuery: true,
+ },
+ {
+ statement: `JSON.SET doc $ {"a":1}`,
+ valid: false,
+ allQuery: false,
+ },
+ {
+ statement: "JSON.DEL doc $.a",
+ valid: false,
+ allQuery: false,
+ },
+ // Other Redis Stack modules.
+ {
+ statement: "FT.SEARCH idx *",
+ valid: true,
+ allQuery: true,
+ },
+ {
+ statement: "TS.GET series",
+ valid: true,
+ allQuery: true,
+ },
+ {
+ statement: "BF.EXISTS filter item",
+ valid: true,
+ allQuery: true,
+ },
+ // FT.DICTADD mutates a dictionary even though Redis flags it readonly.
+ {
+ statement: "FT.DICTADD dict term",
+ valid: false,
+ allQuery: false,
+ },
+ // Core read commands added in Redis 7.4.
+ {
+ statement: "HTTL h FIELDS 1 f",
+ valid: true,
+ allQuery: true,
+ },
+ // Read-only script mode only blocks commands flagged "write", so Lua
+ // still reaches SELECT and the readonly-flagged module commands that
+ // mutate state. See knownExclusions in
+ // TestReadCommandsMatchServerFlags.
+ {
+ statement: "EVAL_RO \"return redis.call('GET','k')\" 0",
+ valid: false,
+ allQuery: false,
+ },
+ {
+ statement: "EVALSHA_RO abc 0",
+ valid: false,
+ allQuery: false,
+ },
+ {
+ statement: "FCALL_RO fn 0",
+ valid: false,
+ allQuery: false,
+ },
+ // SELECT switches the connection to another logical database, so a
+ // later read escapes the database the request was authorized against.
+ {
+ statement: "select 3",
+ valid: false,
+ allQuery: false,
+ },
+ {
+ statement: "select 3\nkeys *",
+ valid: false,
+ allQuery: false,
+ },
}
for _, test := range tests {
@@ -36,8 +127,98 @@ func TestValidateQuery(t *testing.T) {
require.Error(t, err)
} else {
require.NoError(t, err)
- require.Equal(t, test.valid, gotValid)
- require.Equal(t, test.allQuery, gotAllQuery)
+ require.Equal(t, test.valid, gotValid, test.statement)
+ require.Equal(t, test.allQuery, gotAllQuery, test.statement)
+ }
+ }
+}
+
+// TestReadCommandsMatchServerFlags diffs the core allowlist against a snapshot
+// of the flags Redis reports for itself, so a Redis upgrade that ships new read
+// commands fails here instead of reaching a customer. See the snapshot header
+// for how to regenerate it.
+func TestReadCommandsMatchServerFlags(t *testing.T) {
+ // knownExclusions holds commands Redis flags "readonly" that Bytebase
+ // deliberately keeps out of the gate. Every entry needs a reason: the flag
+ // means "does not write the keyspace", not "has no side effects".
+ //
+ // SELECT is not listed. Redis does not flag it readonly, so the reverse
+ // check below is what keeps it out of the allowlist.
+ knownExclusions := map[string]string{
+ // Read-only script mode only rejects commands flagged "write", so a
+ // script still reaches SELECT and every readonly-flagged module command
+ // that mutates state. Verified on redis-stack-server 7.4.7: running
+ // EVAL_RO "redis.call('SELECT',1); return redis.call('GET','marker')"
+ // against database 0 returns database 1's value, and a no-writes
+ // function called through FCALL_RO both crosses databases and adds a
+ // RediSearch dictionary term via FT.DICTADD.
+ "eval_ro": "Lua reaches SELECT and readonly-flagged mutating module commands",
+ "evalsha_ro": "same as eval_ro",
+ "fcall_ro": "same as eval_ro, through a no-writes function",
+ }
+
+ content, err := os.ReadFile("testdata/core_readonly_commands.txt")
+ require.NoError(t, err)
+
+ serverReadOnly := map[string]bool{}
+ for line := range strings.SplitSeq(string(content), "\n") {
+ line = strings.TrimSpace(line)
+ if line == "" || strings.HasPrefix(line, "#") {
+ continue
+ }
+ serverReadOnly[line] = true
+ }
+ require.NotEmpty(t, serverReadOnly, "snapshot has no commands")
+
+ allowed := map[string]bool{}
+ for command := range readCommands {
+ allowed[command] = true
+ }
+ for container, subcommands := range doubleNameReadCommands {
+ for subcommand := range subcommands {
+ allowed[container+" "+subcommand] = true
}
}
+
+ var rejected []string
+ for command := range serverReadOnly {
+ if allowed[command] {
+ continue
+ }
+ if _, ok := knownExclusions[command]; ok {
+ continue
+ }
+ rejected = append(rejected, command)
+ }
+ slices.Sort(rejected)
+ // assert, not require: a run that breaks both directions must report both.
+ assert.Emptyf(t, rejected,
+ "Redis flags these commands readonly but the gate rejects them: %v. "+
+ "Add each to readCommands or doubleNameReadCommands, or to knownExclusions with a reason.",
+ rejected)
+
+ var unflagged []string
+ for command := range allowed {
+ if !serverReadOnly[command] {
+ unflagged = append(unflagged, command)
+ }
+ }
+ slices.Sort(unflagged)
+ assert.Emptyf(t, unflagged,
+ "the gate accepts these but Redis does not flag them readonly: %v. "+
+ "Either the name is wrong, or the command has side effects and must be removed.",
+ unflagged)
+}
+
+// TestModuleReadCommandsWellFormed guards the module allowlist, which has no
+// snapshot because module presence varies per instance. A key that is not a
+// lowercase dotted name can never match, since isReadCommand lowercases the
+// first token and module commands carry the dot inside it.
+func TestModuleReadCommandsWellFormed(t *testing.T) {
+ require.NotEmpty(t, moduleReadCommands)
+ for command := range moduleReadCommands {
+ require.Equal(t, strings.ToLower(command), command, "module command must be lowercase")
+ require.Contains(t, command, ".", "module command must be dot-namespaced")
+ require.False(t, readCommands[command], "module command duplicated in readCommands: %s", command)
+ }
}
diff --git a/backend/plugin/parser/redis/testdata/core_readonly_commands.txt b/backend/plugin/parser/redis/testdata/core_readonly_commands.txt
new file mode 100644
index 0000000000..eacd8686ef
--- /dev/null
+++ b/backend/plugin/parser/redis/testdata/core_readonly_commands.txt
@@ -0,0 +1,111 @@
+# Core Redis commands that the server flags "readonly" in its own command table.
+#
+# Source: redis_version 7.4.7, from
+# redis/redis-stack-server@sha256:798ab84d9f266936b034ab11c4d04a2b8e4b441884c5aa7d17ac951eefdf742a
+# Pinned by digest on purpose: the "latest" tag moves, so it would not name the
+# version this snapshot was taken from.
+#
+# Generated with:
+# docker run -d --rm --name redis-oracle -p 6399:6379 \
+# redis/redis-stack-server@sha256:798ab84d9f266936b034ab11c4d04a2b8e4b441884c5aa7d17ac951eefdf742a
+# # then, per top-level entry of `COMMAND INFO` and each of its subcommands,
+# # keep the name when its flags contain "readonly"; module commands (a dot in
+# # the name) are excluded because module presence varies per instance.
+#
+# Container subcommands appear as "<container> <subcommand>".
+# Regenerate when bumping the pinned version. TestReadCommandsMatchServerFlags
+# then reports every read command Redis has shipped since.
+bitcount
+bitfield_ro
+bitpos
+dbsize
+dump
+eval_ro
+evalsha_ro
+exists
+expiretime
+fcall_ro
+geodist
+geohash
+geopos
+georadius_ro
+georadiusbymember_ro
+geosearch
+get
+getbit
+getrange
+hexists
+hexpiretime
+hget
+hgetall
+hkeys
+hlen
+hmget
+hpexpiretime
+hpttl
+hrandfield
+hscan
+hstrlen
+httl
+hvals
+keys
+lcs
+lindex
+llen
+lolwut
+lpos
+lrange
+memory usage
+mget
+object encoding
+object freq
+object idletime
+object refcount
+pexpiretime
+pfcount
+pttl
+randomkey
+scan
+scard
+sdiff
+sinter
+sintercard
+sismember
+smembers
+smismember
+sort_ro
+srandmember
+sscan
+strlen
+substr
+sunion
+touch
+ttl
+type
+xinfo consumers
+xinfo groups
+xinfo stream
+xlen
+xpending
+xrange
+xread
+xrevrange
+zcard
+zcount
+zdiff
+zinter
+zintercard
+zlexcount
+zmscore
+zrandmember
+zrange
+zrangebylex
+zrangebyscore
+zrank
+zrevrange
+zrevrangebylex
+zrevrangebyscore
+zrevrank
+zscan
+zscore
+zunion