R315 Check every part of a destructive shell command’s target path before the command
Before a shell command that deletes, overwrites or empties files, such as rm, find … -delete, a > redirection, mv or cp onto an existing file or rsync --delete, check that each variable and command substitution that may be empty or unset and that its target path is built from, directly or through another variable, is set and not empty, as ${VAR:?} or [ -n "$VAR" ] || exit 1 does, or that the resolved target lies under its checked root and is not that root, and stop when the check fails; set -u alone is no such check.
Reason
In a shell, an empty or unset variable, or a command substitution whose output is empty, drops out of a path without an error: "$dir/$name" becomes $dir/, and with both empty it becomes /, the root of the file system. rm -rf on that path removes far more than was meant, and cannot be undone. Agents pursue goals only by legitimate means, never by destructive ones (Art. II §6(1)), and a command whose target can widen this way, unseen, is destructive by its shape whatever its author meant. set -u stops on an unset variable but not on an empty one, nor on an empty command substitution, so it leaves the commonest case open.
Verification
The commands covered are those that delete, overwrite or empty files, run through a shell: on a command line, in a shell script, or in a step that another file hands to a shell, such as a CI step or a Makefile recipe. They include rm with or without -r or -f, find with -delete or with -exec rm, a > redirection or truncate, mv, cp or ln -f onto an existing file, rsync --delete and git clean. The target path is each path such a command deletes, overwrites or empties: the operands of rm, the directory find starts from, the file of a redirection or of truncate, the destination of mv, cp and rsync, the link ln -f replaces and the directory git clean cleans.
The parts of a target path are the variables, positional parameters and command substitutions, such as $(basename "$p"), it is built from, directly or through another variable. A variable whose value is built from other parts brings them into the path too, so a check of that variable alone does not cover them: after d="${S:?}/$(…)"; [ -n "$d" ], the target $d can still be $S/. A part needs no check only when the same command or script gave it a value that is not empty, written literally, before the path is built; a command’s output, a positional parameter, an environment variable and a value read with read or from a file may all be empty.
Checks that meet this rule: writing the part in the path as ${VAR:?}; running [ -n "$VAR" ] || exit 1 before the command; putting a command substitution into a variable first and checking that, as in n=$(basename "$p"); [ -n "$n" ] || exit 1; rm -rf "${D:?}/$n"; and resolving both the root expected for the target and the target itself, then asserting that the target lies under that root and is not the root, and running the command on the resolved target, as in r=$(realpath -m -- "${root:?}") && t=$(realpath -m -- "${p:?}") && [ -n "$r" ] && [ -n "$t" ] || exit 1; case "$t" in "$r"/?*) ;; *) exit 1 ;; esac; rm -rf -- "$t". A comparison of paths not resolved first does not meet it, since $root// and $root/x/.. name the root itself and still match "$root"/?*. set -u alone does not meet it, nor does set -e.
Violated when an agent runs or writes such a command whose target path is built from a part that may be empty or unset and that no check before the command stops on.