Skip to main content

SKY-D329 · Security in code

Broad destructive shell command

High

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
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

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

scripts/deploy.sh · ShellReported on line 6
#!/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

scripts/deploy.sh · Shell
#!/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

  1. Reads shell commands in .sh, .bash, .zsh, .ksh and .bats files, GitHub Actions run: steps, GitLab CI script items, Dockerfile RUN instructions, fixed command strings in Python os.system()-style calls or shell=True calls, and fixed strings passed to child_process.exec() in TypeScript and JavaScript.
  2. Finds rm with both a recursive and a force option in any spelling (-rf, -r -f, --recursive --force, /bin/rm -Rf). It looks through sudo, env, xargs, nohup, exec, command and time, into sh -c/bash -c strings, eval, find -exec, $(…) and backticks, and into the arguments of unknown programs such as busybox rm. Arguments that echo or printf only print, and text after a # comment, are ignored.
  3. 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.
  4. Skips relative paths such as build or dist, and package-manager caches and scratch space: anything below /tmp/, /var/tmp/, /var/cache/, /var/lib/apt/lists/ or /usr/local/share/.cache/, and .cache or .npm under /root, ~ or $HOME. A cache path that holds .., a variable after the prefix, a backtick or { is reported, since it can leave the cache.
  5. Also reports git reset --hard and git clean whose short flags include f, d and x together. 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.

scripts/deploy.sh · Shell · the fix, against the flagged file
#!/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/tests in a Dockerfile build stage or rm -rf /usr/src/app in a container.
  • git reset --hard origin/main and git clean -ffdx in a CI job that starts from a fresh checkout are reported, though there is no local work to lose there.
  • Only rm and the two git commands are checked: find / -delete, Python shutil.rmtree() or Node fs.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 disk
  • A baseline (skylos baseline ., then --baseline) accepts the commands that exist today and reports new ones.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.