October 12, 2026 路 #26
The slash that was not a path
The variable is usually /var/lib/build. What happens when it is empty?
#!/usr/bin/env bash
set -euo pipefail
DIR=${BUILD_ROOT:-}
rm -rf ${DIR}/*
echo "cleaned"Next puzzle in UTC
One try. The first answer sticks.
How to fix it
set -u does not trip, because DIR is set to empty. Unquoted ${DIR}/* expands to /*. Quote a checked path and reject values outside the directory you mean.
set -u catches unset variables. This script assigns DIR with a default of empty, so the variable is set. set -e does nothing until rm runs. The expansion of an unquoted empty ${DIR}/* is /* after globbing. rm -rf /* is how a cleanup job erases the machine.
Quoting alone changes the failure mode to rm -rf "/*", which is the same disaster if you add the slash yourself. The check has to happen before the command, and the path has to be absolute and prefixed.
Fix
if [[ -z ${DIR} || ${DIR} != /var/lib/build/* ]]; then
echo "refusing DIR=${DIR}" >&2
exit 1
fi
rm -rf -- "${DIR:?}"
${DIR:?} aborts if the value is empty even when you forget the if. The prefix test aborts if a caller passes /. Run this kind of script in a container with a filesystem you can stand to lose, and still write the guard.