Skip to content
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
7d14bb1
Add database-level access control
dvkashapov Jul 4, 2025
97a27c5
Fix selector flag assignment and apply clang-format
dvkashapov Jul 7, 2025
b2fd61a
Extend serverCommand with get_dbid_args, unify ACL checks
dvkashapov Jul 14, 2025
b4adb23
Run generate commands
dvkashapov Oct 30, 2025
c0709a1
Merge remote-tracking branch 'upstream/unstable' into unstable
dvkashapov Oct 30, 2025
88d96ba
Fix tests and apply clang
dvkashapov Oct 30, 2025
98f4550
Delete CROSS_DB and NOT_IMPLEMENTED
dvkashapov Oct 30, 2025
e6c10dd
Delete CROSS_DB from commands.def
dvkashapov Oct 30, 2025
74ccbec
Add db+= and db-= syntax
dvkashapov Oct 31, 2025
8bc8d13
Fix comment style
dvkashapov Oct 31, 2025
a4397dc
clang-format fix
dvkashapov Oct 31, 2025
fa90b04
Refactor and add more tests
dvkashapov Nov 5, 2025
a3af4f6
Merge remote-tracking branch 'upstream/unstable' into unstable
dvkashapov Nov 6, 2025
15068cf
Fix reason in ACL LOG and add test
dvkashapov Nov 16, 2025
ec93e49
Use cmd->fullname when no argpos
dvkashapov Nov 16, 2025
711b4f3
Add module api for db-level check and test
dvkashapov Nov 16, 2025
fc5bc3a
apply pr suggestions
dvkashapov Nov 17, 2025
10d7499
apply clang-format
dvkashapov Nov 17, 2025
7f0e020
add ALL_DBS flag to migration/slot commands
dvkashapov Nov 17, 2025
b6ab15b
db= syntax, acl getuser support
dvkashapov Nov 26, 2025
0562230
Return ACL_DENIED_DB only for R/W commands in forbidden db
dvkashapov Nov 26, 2025
04568af
Use sds
dvkashapov Nov 26, 2025
19186e7
Merge remote-tracking branch 'upstream/unstable' into unstable
dvkashapov Nov 27, 2025
d35a3a7
Merge remote-tracking branch 'upstream/unstable' into unstable
dvkashapov Nov 27, 2025
f725b5a
Merge remote-tracking branch 'upstream/unstable' into unstable
dvkashapov Dec 4, 2025
a16ffb6
Add new errmsg for invalid dbid
dvkashapov Dec 4, 2025
7825f1d
Delete prev module api for databases
dvkashapov Dec 4, 2025
1b88e62
Add new API
dvkashapov Dec 4, 2025
a17927a
Reorder transaction_db_id in select and more MULTI/EXEC tests
dvkashapov Dec 4, 2025
3f437c8
Apply review suggestions
dvkashapov Dec 12, 2025
efa1125
Comment for VM_ACLCheckCommandPermissions and delete reset dbid
dvkashapov Dec 13, 2025
c3afa10
Revert reset in multi, move errno ERANGE
dvkashapov Dec 15, 2025
d7c9bd2
make ACLSelectorCanAccessDb static inline
dvkashapov Dec 16, 2025
42b8334
Restrict all commands with keys/keyspace, add alldbs to FLUSHSLOT
dvkashapov Dec 17, 2025
16dc2dc
Eval/func tests, clean up some lines
dvkashapov Dec 17, 2025
5dbce55
New api with denial_reason ptr
dvkashapov Dec 17, 2025
5d815aa
Merge remote-tracking branch 'upstream/unstable' into unstable
dvkashapov Dec 17, 2025
0ba174a
Small fixes
dvkashapov Dec 17, 2025
ab93b29
Change wording in JSON files
dvkashapov Dec 18, 2025
8ce4a75
Apply review suggestions
dvkashapov Dec 20, 2025
41703e4
Merge remote-tracking branch 'upstream/unstable' into unstable
dvkashapov Dec 20, 2025
1411b8a
Update lua and module api to use new VM_ACL check
dvkashapov Dec 20, 2025
19ae1b7
Merge remote-tracking branch 'upstream/unstable' into unstable
dvkashapov Dec 21, 2025
755678a
Apply review suggestions
dvkashapov Dec 21, 2025
da84977
Del test for COUNTKEYSINSLOT
dvkashapov Dec 21, 2025
5666547
Delete get_dbid_args for MIGRATE
dvkashapov Dec 22, 2025
e7907a5
Del dbid >= server.dbnum
dvkashapov Dec 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 225 additions & 17 deletions src/acl.c

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/cli_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

/* Definitions to configure commands.c to generate the above structs. */
#define MAKE_CMD(name, summary, complexity, since, doc_flags, replaced, deprecated, group, group_enum, history, \
num_history, tips, num_tips, function, arity, flags, acl, key_specs, key_specs_num, get_keys, \
numargs) \
num_history, tips, num_tips, function, arity, flags, acl, get_dbid_args, key_specs, \
key_specs_num, get_keys, numargs) \
name, summary, group, since, numargs
#define MAKE_ARG(name, type, key_spec_index, token, summary, since, flags, numsubargs, deprecated_since) \
name, type, token, since, flags, numsubargs
Expand Down
6 changes: 3 additions & 3 deletions src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
#include "server.h"

#define MAKE_CMD(name, summary, complexity, since, doc_flags, replaced, deprecated, group, group_enum, history, \
num_history, tips, num_tips, function, arity, flags, acl, key_specs, key_specs_num, get_keys, \
numargs) \
num_history, tips, num_tips, function, arity, flags, acl, get_dbid_args, key_specs, \
key_specs_num, get_keys, numargs) \
name, summary, complexity, since, doc_flags, replaced, deprecated, group_enum, history, num_history, tips, \
num_tips, function, arity, flags, acl, key_specs, key_specs_num, get_keys, numargs
num_tips, function, arity, flags, acl, get_dbid_args, key_specs, key_specs_num, get_keys, numargs
#define MAKE_ARG(name, type, key_spec_index, token, summary, since, flags, numsubargs, deprecated_since) \
name, type, key_spec_index, token, summary, since, flags, deprecated_since, numsubargs
#define COMMAND_STRUCT serverCommand
Expand Down
848 changes: 425 additions & 423 deletions src/commands.def

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ following keys. To be safe, assume all of them are optional.
the command. (Don't use it for anything else.)
* `"command_flags"`: An array of flags represented as strings. Command flags:
* `"ADMIN"`
* `"ALL_DBS"`
* `"ALLOW_BUSY"`
* `"ASKING"`
* `"BLOCKING"`
Expand Down Expand Up @@ -93,6 +94,9 @@ following keys. To be safe, assume all of them are optional.
* `"STREAM"`
* `"STRING"`
* `"TRANSACTION"`
* `"get_dbid_args"`: The name of the C function in Valkey's source code
Comment thread
madolson marked this conversation as resolved.
implementing retrieval of database ID arguments from commands that accept
database ID as an argument.
* `"command_tips"`: Optional. A list of one or more of these strings:
* `"NONDETERMINISTIC_OUTPUT"`
* `"NONDETERMINISTIC_OUTPUT_ORDER"`
Expand Down
11 changes: 11 additions & 0 deletions src/commands/acl-getuser.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
[
"7.0.0",
"Added selectors and changed the format of key and channel patterns from a list to their rule representation."
],
[
"9.1.0",
"Added database permission rules."
]
],
"command_flags": [
Expand Down Expand Up @@ -61,6 +65,10 @@
"description": "Root selector's channels.",
"type": "string"
},
"databases": {
"description": "Root selector's databases.",
"type": "string"
},
"selectors": {
"type": "array",
"items": {
Expand All @@ -75,6 +83,9 @@
},
"channels": {
"type": "string"
},
"databases": {
"type": "string"
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/commands/acl-setuser.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
[
"7.0.0",
"Added selectors and key based permissions."
],
[
"9.1.0",
"Added database permission rules."
]
],
"command_flags": [
Expand Down
3 changes: 2 additions & 1 deletion src/commands/cluster-cancelslotmigrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"command_flags": [
"NO_ASYNC_LOADING",
"ADMIN",
"STALE"
"STALE",
"ALL_DBS"
],
"reply_schema": {
"const": "OK"
Expand Down
3 changes: 2 additions & 1 deletion src/commands/cluster-countkeysinslot.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"container": "CLUSTER",
"function": "clusterCommand",
"command_flags": [
"STALE"
"STALE",
"ALL_DBS"
Comment thread
dvkashapov marked this conversation as resolved.
Outdated
],
"arguments": [
{
Expand Down
3 changes: 2 additions & 1 deletion src/commands/cluster-migrateslots.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"command_flags": [
"NO_ASYNC_LOADING",
"ADMIN",
"STALE"
"STALE",
"ALL_DBS"
],
"arguments": [
{
Expand Down
1 change: 1 addition & 0 deletions src/commands/copy.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"SLOW",
"WRITE"
],
"get_dbid_args": "copyDbIdArgs",
"key_specs": [
{
"flags": [
Expand Down
3 changes: 2 additions & 1 deletion src/commands/flushall.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
]
],
"command_flags": [
"WRITE"
"WRITE",
"ALL_DBS"
],
"acl_categories": [
"DANGEROUS",
Expand Down
1 change: 1 addition & 0 deletions src/commands/migrate.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"SLOW",
"WRITE"
],
"get_dbid_args": "migrateDbIdArgs",
Comment thread
dvkashapov marked this conversation as resolved.
Outdated
"command_tips": [
"NONDETERMINISTIC_OUTPUT"
],
Expand Down
1 change: 1 addition & 0 deletions src/commands/move.json
Comment thread
dvkashapov marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"KEYSPACE",
"WRITE"
],
"get_dbid_args": "moveDbIdArgs",
"key_specs": [
{
"flags": [
Expand Down
1 change: 1 addition & 0 deletions src/commands/select.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"CONNECTION",
"FAST"
],
"get_dbid_args": "selectDbIdArgs",
Comment thread
dvkashapov marked this conversation as resolved.
"reply_schema": {
"const": "OK"
},
Expand Down
1 change: 1 addition & 0 deletions src/commands/swapdb.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"KEYSPACE",
"WRITE"
],
"get_dbid_args": "swapdbDbIdArgs",
"arguments": [
{
"name": "index1",
Expand Down
78 changes: 76 additions & 2 deletions src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -899,9 +899,14 @@ void selectCommand(client *c) {

if (selectDb(c, id) == C_ERR) {
addReplyError(c, "DB index is out of range");
} else {
addReply(c, shared.ok);
return;
}

if (c->flag.multi) {
serverAssert(c->mstate != NULL);
c->mstate->transaction_db_id = id;
}
addReply(c, shared.ok);
}

void randomkeyCommand(client *c) {
Expand Down Expand Up @@ -2954,3 +2959,72 @@ int bitfieldGetKeys(struct serverCommand *cmd, robj **argv, int argc, getKeysRes
}
return 1;
}

int *selectDbIdArgs(robj **argv, int argc, int *count) {
if (argc < 2) return NULL;

long long dbid;
if (getLongLongFromObject(argv[1], &dbid) != C_OK) return NULL;
if (dbid < 0 || dbid >= server.dbnum) return NULL;

int *result = zmalloc(sizeof(int));
result[0] = (int)dbid;
*count = 1;
return result;
}

int *swapdbDbIdArgs(robj **argv, int argc, int *count) {
if (argc < 3) return NULL;

long long db1, db2;
if (getLongLongFromObject(argv[1], &db1) != C_OK ||
getLongLongFromObject(argv[2], &db2) != C_OK) return NULL;
if (db1 < 0 || db1 >= server.dbnum || db2 < 0 || db2 >= server.dbnum) return NULL;

int *result = zmalloc(2 * sizeof(int));
result[0] = (int)db1;
result[1] = (int)db2;
*count = 2;
return result;
}

int *moveDbIdArgs(robj **argv, int argc, int *count) {
if (argc < 3) return NULL;

long long dbid;
if (getLongLongFromObject(argv[2], &dbid) != C_OK) return NULL;
if (dbid < 0 || dbid >= server.dbnum) return NULL;

int *result = zmalloc(sizeof(int));
result[0] = (int)dbid;
*count = 1;
return result;
}

int *migrateDbIdArgs(robj **argv, int argc, int *count) {
if (argc < 5) return NULL;

long long dbid;
if (getLongLongFromObject(argv[4], &dbid) != C_OK) return NULL;
if (dbid < 0 || dbid >= server.dbnum) return NULL;

int *result = zmalloc(sizeof(int));
result[0] = (int)dbid;
*count = 1;
return result;
}

int *copyDbIdArgs(robj **argv, int argc, int *count) {
if (argc < 5) return NULL;

if (strcasecmp(argv[3]->ptr, "db") != 0) return NULL;

long long dbid;
if (getLongLongFromObject(argv[4], &dbid) != C_OK) return NULL;
if (dbid < 0 || dbid >= server.dbnum) return NULL;

int *result = zmalloc(sizeof(int));
result[0] = (int)dbid;
*count = 1;
return result;
}
15 changes: 15 additions & 0 deletions src/intset.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,18 @@ int intsetValidateIntegrity(const unsigned char *p, size_t size, int deep) {

return 1;
}

/* Free an intset */
void intsetFree(intset *is) {
if (is) zfree(is);
}

/* Deep copy an intset */
intset *intsetDup(intset *is) {
if (!is) return intsetNew();

size_t size = intsetBlobLen(is);
intset *copy = zmalloc(size);
memcpy(copy, is, size);
return copy;
}
2 changes: 2 additions & 0 deletions src/intset.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,7 @@ uint8_t intsetGet(intset *is, uint32_t pos, int64_t *value);
uint32_t intsetLen(const intset *is);
size_t intsetBlobLen(intset *is);
int intsetValidateIntegrity(const unsigned char *is, size_t size, int deep);
void intsetFree(intset *is);
intset *intsetDup(intset *is);

#endif // __INTSET_H
Loading
Loading