SKY-D329 · Security in code
Broad destructive shell command
Some shell commands wipe far more than intended when a path is wrong or a variable is empty. SKY-D329 reports rm -rf of the root, the home folder, the current folder, .git or a bare $VAR/, and git reset --hard and git clean -fdx, which throw away uncommitted work.
- Languages
- Shell, GitHub Actions, GitLab CI, Dockerfile, Python, TypeScript/JavaScript
- Quality gate
- counted toward the gate limits (full and diff scans)
- Finding type
- review
Why it matters
If RELEASE_DIR is empty, rm -rf "$RELEASE_DIR"/* becomes rm -rf /* and deletes everything the user can write to. Scripts that coding agents or CI run unattended have no one watching to stop them.
What Skylos flags
#!/usr/bin/env bash# Replace the previous release with the new build.set -euo pipefail RELEASE_DIR="$(cat .release-dir)"rm -rf "$RELEASE_DIR"/*tar -xzf release.tar.gz -C "$RELEASE_DIR"What Skylos leaves alone
#!/usr/bin/env bash# Replace the previous release with the new build.set -euo pipefail RELEASE_DIR="$(cat .release-dir)"rm -rf "${RELEASE_DIR:?}"/*tar -xzf release.tar.gz -C "$RELEASE_DIR"Why Skylos leaves it alone
${RELEASE_DIR:?} makes the shell stop with an error when the variable is empty or unset, so the command can never become rm -rf /*. Skylos's empty-variable pattern matches only a bare $VAR/, ${VAR}/ or $VAR/*.
How Skylos decides
- Reads shell commands in
.sh,.bash,.zsh,.kshand.batsfiles, GitHub Actionsrun:steps, GitLab CI script items, DockerfileRUNinstructions, fixed command strings in Pythonos.system()-style calls orshell=Truecalls, and fixed strings passed tochild_process.exec()in TypeScript and JavaScript. - Finds
rmwith both a recursive and a force option in any spelling (-rf,-r -f,--recursive --force,/bin/rm -Rf). It looks throughsudo,env,xargs,nohup,exec,commandandtime, intosh -c/bash -cstrings,eval,find -exec,$(…)and backticks, and into the arguments of unknown programs such asbusybox rm. Arguments thatechoorprintfonly print, and text after a#comment, are ignored. - Reports when a target is
/or any other absolute path,~,$HOME,.,..,*,./*,.git, or a variable followed only by/or/*($DIR/,${DIR}/*), which an empty value turns into the root. - Skips relative paths such as
buildordist, and package-manager caches and scratch space: anything below/tmp/,/var/tmp/,/var/cache/,/var/lib/apt/lists/or/usr/local/share/.cache/, and.cacheor.npmunder/root,~or$HOME. A cache path that holds.., a variable after the prefix, a backtick or{is reported, since it can leave the cache. - Also reports
git reset --hardandgit cleanwhose short flags includef,dandxtogether. Every finding is HIGH.
How to fix it
Make the target impossible to get wrong: guard variables with ${VAR:?}, delete a named sub-folder rather than a whole tree, and check that the path is the one you expect before removing it.
#!/usr/bin/env bash# Replace the previous release with the new build.set -euo pipefail RELEASE_DIR="$(cat .release-dir)"Removed: rm -rf "$RELEASE_DIR"/*Added: case "$RELEASE_DIR" inAdded: /srv/app/releases/?*) ;;Added: *) echo "refusing to clean '$RELEASE_DIR'" >&2; exit 1 ;;Added: esacAdded: rm -rf "${RELEASE_DIR:?}"/*tar -xzf release.tar.gz -C "$RELEASE_DIR"When it can be wrong
- Any absolute path is reported, even a narrow one in a throwaway place, such as
rm -rf /app/testsin a Dockerfile build stage orrm -rf /usr/src/appin a container. git reset --hard origin/mainandgit clean -ffdxin a CI job that starts from a fresh checkout are reported, though there is no local work to lose there.- Only
rmand the twogitcommands are checked:find / -delete, Pythonshutil.rmtree()or Nodefs.rmSync()are not part of this rule.
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
In a shell script, put the marker on the reported line as a real comment. In GitHub Actions, GitLab CI and Dockerfiles the marker may also sit on the line directly above, one rule per marker.
Shell rm -rf /srv/build-cache/* # skylos: ignore[SKY-D329] dedicated cache diskA baseline (
skylos baseline ., then--baseline) accepts the commands that exist today and reports new ones.