diff --git a/src/multi.c b/src/multi.c index fcfe7ae3319..b274dc9ad72 100644 --- a/src/multi.c +++ b/src/multi.c @@ -378,7 +378,12 @@ void execCommand(client *c) { * they were changed after the commands were queued. */ int acl_errpos; int acl_retval = ACLCheckAllPerm(c, &acl_errpos); - if (acl_retval != ACL_OK) { + /* An earlier command may have logged the client out, e.g. ACL DELUSER of + * its own user, so check authentication as processCommand() does. + * Clients without a user, like the AOF client, skip this. */ + if (c->user && authRequired(c) && !(c->cmd->flags & CMD_NO_AUTH)) { + addReplyErrorObject(c, shared.noautherr); + } else if (acl_retval != ACL_OK) { char *reason; switch (acl_retval) { case ACL_DENIED_CMD: reason = "no permission to execute the command or subcommand"; break; diff --git a/tests/unit/acl.tcl b/tests/unit/acl.tcl index e0310368628..6d6b4755d14 100644 --- a/tests/unit/acl.tcl +++ b/tests/unit/acl.tcl @@ -1752,3 +1752,76 @@ start_server [list overrides [list "dir" $server_path "aclfile" "user.acl"] tags } } } + +set server_path [tmpdir "server.acl-logout"] +set fd [open [file join $server_path "user.acl"] w] +puts $fd "user default on >defpass ~* &* +@all" +close $fd +start_server [list overrides [list "dir" $server_path "aclfile" "user.acl" "requirepass" "defpass"] tags {"acl external:skip"}] { + test {Commands queued after ACL DELUSER of the calling user do not run as the default user} { + r AUTH default defpass + r SET secret topsecret + r RPUSH app:l 1 + r ACL SETUSER app on >pw ~app:* +@all + test_defer {r DEL secret app:l app:out} + + set rr [valkey [srv "host"] [srv "port"] 0 $::tls] + test_defer {$rr close} + $rr AUTH app pw + assert_error {*ACL failure in script*} {$rr EVAL {return server.call('SET','secret','changed')} 0} + assert_error {*GET option of SORT denied*} {$rr SORT app:l BY nosort GET secret* STORE app:out} + + $rr MULTI + $rr ACL DELUSER app + $rr EVAL {return server.call('SET','secret','changed')} 0 + $rr SORT app:l BY nosort GET secret* STORE app:out + # The client is closed after the ACL DELUSER reply, so EXEC reads EOF. + catch {$rr EXEC} + + assert_equal {topsecret 0} [list [r GET secret] [r EXISTS app:out]] + } + + test {AUTH queued after ACL DELUSER of the calling user still runs} { + r AUTH default defpass + r ACL SETUSER app on >pw ~other:* +@all + r ACL SETUSER other on >pw2 ~other:* +@all + test_defer {r ACL DELUSER app other; r DEL other:k} + + set rr [valkey [srv "host"] [srv "port"] 0 $::tls] + test_defer {$rr close} + $rr AUTH app pw + $rr MULTI + $rr ACL DELUSER app + $rr AUTH other pw2 + $rr SET other:k v + catch {$rr EXEC} + + assert_equal v [r GET other:k] + } + + test {Commands queued after ACL LOAD removes the calling user do not run as the default user} { + r AUTH default defpass + r SET secret topsecret + test_defer {r DEL secret} + set aclfile [file join $server_path "user.acl"] + set fd [open $aclfile w] + puts $fd "user default on >defpass ~* &* +@all" + puts $fd "user app on >pw ~app:* +@all" + close $fd + r ACL LOAD + + set rr [valkey [srv "host"] [srv "port"] 0 $::tls] + test_defer {$rr close} + $rr AUTH app pw + set fd [open $aclfile w] + puts $fd "user default on >defpass ~* &* +@all" + close $fd + + $rr MULTI + $rr ACL LOAD + $rr EVAL {return server.call('SET','secret','changed')} 0 + catch {$rr EXEC} + + assert_equal topsecret [r GET secret] + } +} diff --git a/tests/unit/multi.tcl b/tests/unit/multi.tcl index 4f917ebd54e..c702e362065 100644 --- a/tests/unit/multi.tcl +++ b/tests/unit/multi.tcl @@ -1163,6 +1163,16 @@ start_server {overrides {appendonly {yes} appendfilename {appendonly.aof} append } r get foo } {} + + test {MULTI in AOF is loaded when the default user requires a password} { + test_config_set r requirepass foobar + r multi + r set foo bar + r set foo2 bar2 + r exec + r debug loadaof + r mget foo foo2 + } {bar bar2} } start_cluster 1 0 {tags {"external:skip cluster"}} {