A Redis cache warmer whose ACL user holds `+@write ~cache:*` runs `FLUSHALL` and wipes every key; why did the ACL allow it, and how do you close it?
answer
- patterns only see keys named as arguments
- which categories hold FLUSHALL
- order of minus and plus rules
- calls add, they do not replace
basics
~20 sKey patterns limit only commands that name keys, and FLUSHALL names none, so only command rules can stop it. It sits in @write, so +@write granted it. Fix it with +@write -@dangerous, in that order, or a named command list.
solid answer
~40 sRedis checks a key pattern only against keys named in a command's arguments. `FLUSHALL`, `FLUSHDB` and `SWAPDB` take no key arguments, so `~cache:*` never applies to them, and only command rules can stop them. `FLUSHALL` is in `@write`, so `+@write` granted it. The fix is `+@write -@dangerous`, in that order, because rules run left to right and `-@dangerous +@write` adds it straight back. Better for a warmer is a short named list such as `+set +unlink +ping`. Define the user with `reset` first, because a later `ACL SETUSER` adds to what is there, then prove the result with `ACL DRYRUN warmer FLUSHALL`. `ACL LOG` will not help with the incident itself, since it records only refusals.
code
plaintext · 6 lines> ACL SETUSER warmer reset on >WARMER_SECRET +@write -@dangerous +ping %W~cache:*
OK
> ACL DRYRUN warmer FLUSHALL
"User warmer has no permissions to run the 'flushall' command"
> ACL DRYRUN warmer SET cache:1 x
OKgo deeper
Recall that command rules and key patterns are separate checks, and that a command naming no key is stopped only by a command rule, however narrow the user's patterns look.
Predict the result of a rule string by reading it left to right, and name which categories FLUSHALL and SCAN belong to, using ACL CAT to check rather than guessing.
Demonstrate the diagnosis: read the rules with ACL GETUSER, prove the outcome with ACL DRYRUN, explain why ACL LOG has no entry, and stop live clients correctly, since off does not.
Set the default: allowlists over blocklists, and definitions that start with reset. Decide how far +@all may be used at all, given how a category grant can grow as commands are added.
## Why the key pattern did not help A Redis ACL check has two separate parts for a command. The **command check** asks whether this user may call the command at all. The **key check** asks whether each key the command names matches one of the user's patterns. If a command names no keys, the second check has nothing to look at. The docs state this directly: key patterns restrict only commands that operate on specific keys named in their arguments. A user defined as `~tenant1:* +@all` can still run `FLUSHALL` and delete every key. Categories then made it worse. Redis gives `FLUSHALL` the `@keyspace` and `@dangerous` categories, and because it is flagged as a write command it also lands in `@write` and `@slow`. So `+@write` looks like permission to change values under a prefix, but it also grants commands that replace the whole data set. ## Commands a key pattern does not reach | Command | Grant that includes it | In `@dangerous`? | Effect beyond the pattern | |---|---|---|---| | `FLUSHALL` | `@write` | Yes | Deletes every key | | `FLUSHDB` | `@write` | Yes | Deletes the selected database | | `SWAPDB` | `@write` | Yes | Swaps two databases | | `KEYS` | `@read` | Yes | Lists key names | | `SCAN` | `@read` | No | Lists key names | | `RANDOMKEY` | `@read` | No | Returns a key name | The last two rows catch people who stop at `-@dangerous`. A reporting job with `+@read -@dangerous %R~report:*` cannot read values elsewhere, but `SCAN` will still list key names from outside its pattern. If the names are sensitive, add `-scan -randomkey`. ## Three ways a user silently widens Each of these replies `OK`, which is why they survive review: 1. **Order.** Rules run left to right, and `redis.conf` warns that additive and subtractive rules change meaning with their order. `-@dangerous +@write` removes the category and then adds `FLUSHALL` back, while `+@write -@dangerous` does not. 2. **Incremental calls.** Every `ACL SETUSER` applies on top of the existing user. If a deploy script later sends `ACL SETUSER warmer +@write`, the earlier `-@dangerous` is undone for those commands. A second `~orders:*` keeps the first pattern as well as the new one. 3. **Grants that grow.** `+@all -flushall -flushdb -swapdb` blocks three named commands and nothing else, and `+@all` also includes commands that modules load later. One related case does fail loudly. Adding a pattern to a user that already holds `~*` returns an error telling you to use `resetkeys`, because a narrower pattern could not narrow anything. ## Diagnosing a user The usual tools answer different questions: - `ACL GETUSER warmer` shows the command rules in effect, such as `-@all +@write -@dangerous`, so you can read the order. - `ACL DRYRUN warmer FLUSHALL` evaluates the exact call without running it. - `ACL LOG` is **not** evidence here. The call was permitted, and the log records only refusals and failed logins. - `ACL CAT write` and `ACL CAT dangerous` show what a grant really contains on this server. ## Closing it The durable change is to move from what must be blocked to what is needed: 1. Redefine the user starting with `reset`, so nothing earlier survives. 2. Prefer named commands for a job with a small, known set. Where a category is worth it, end the command rules with `-@dangerous`. 3. Expect `-@dangerous` to remove more than you meant. `INFO`, `CLIENT` and `SORT` are in it too, so watch `ACL LOG` after the change. 4. Keep the definition in one place, so nobody patches it with an incremental call. ## What the fix leaves open Narrower rules still leave a warmer with real power, and the review should say so: - `DEL` and `UNLINK` stay allowed on `cache:*`, so it can still empty its own prefix. - `+@write` grants every data type's write commands, where a named list grants only those it uses. - Nothing limits how much it writes: ACL rules say what a user may do, not how much. A rule change applies to the user's live connections at once, because the server overwrites the user in place. But `off` does not end a running client: it stops new logins only. To stop one now, use `ACL DELUSER`, which closes that user's connections, or `CLIENT KILL USER warmer`.
- You find the bad user during an incident and run `ACL SETUSER warmer off`. Why does it keep running commands?`off` only prevents new logins, so connections that have already authenticated keep working. Tightening its command rules takes effect on those connections right away. To stop the client, use `ACL DELUSER warmer`, which closes its connections, or `CLIENT KILL USER warmer`, which closes them and leaves the user defined.
- Why is `reset` at the start of a definition safer than a plain re-run of the same rules?`ACL SETUSER` applies on top of the existing user, so a re-run of a narrowed definition leaves anything granted earlier in place. `reset` first clears passwords, patterns, channels and selectors, sets `off` and `-@all`. The whole call applies at once, so ending up with `on` and the new rules leaves only those rules.
- A user limited to ~cache:* can only delete keys under cache:
- FLUSHALL is only in @admin, so +@write cannot include it
- Rule order does not matter because Redis sorts minus rules last
- A later ACL SETUSER with a narrower rule replaces the wider earlier one
- ACL LOG will show which user ran the FLUSHALL that wiped the data
- Setting a user off disconnects its existing clients immediately