Skip to content

Place SHELLSPEC_INFILE file under the temporary directory - #361

Open
thanegill wants to merge 1 commit into
shellspec:masterfrom
thanegill:fix-infile-relative-path
Open

thanegill wants to merge 1 commit into
shellspec:masterfrom
thanegill:fix-infile-relative-path

Conversation

@thanegill

Copy link
Copy Markdown

When --random is used, the runner writes the shuffled spec list to $SHELLSPEC_INFILE, which defaults to the relative path file. This drops a stray file named file into the working/project directory, silently overwrites any existing ./file, and is never cleaned up, unlike every other internal scratch file.

Reproduce this issue with:

mkdir /tmp/shellspec_test && cd /tmp/shellspec_test
shellspec --init
echo 'IMPORTANT - do not delete' > file

shellspec --random specfiles      # also: --random examples

cat file        # -> overwritten with the spec list
ls file         # -> still present after shellspec exits

Along with this fix:

  • Added spec/random_no_stray_file_spec.sh: a regression spec parameterized over both --random specfiles and --random examples (the two paths through the write). It runs the binary in an isolated project and asserts the working directory gains no unexpected entries. Because the self-test suite runs under --sandbox, which makes PATH read-only, the nested binary is launched via a fresh sh -c with PATH restored from $SHELLSPEC_POSIX_PATH.
  • Also added a CHANGELOG.md entry under [Unreleased].

When --random is used, the runner writes the shuffled spec list to
$SHELLSPEC_INFILE, which defaulted to the relative path "file". That left a
stray "file" in the working/project directory, truncating any existing
./file, and — unlike every other internal scratch file, which lives under
$SHELLSPEC_TMPBASE and is removed by cleanup() — it was never cleaned up.

Set SHELLSPEC_INFILE to "$SHELLSPEC_TMPBASE/.shellspec-infile" alongside the
sibling temporary files so it is isolated, never clobbers a user's file, and
is removed on exit. Add a regression spec that runs the binary with --random
and asserts no stray file is left in the working directory.
@adam-moss

Copy link
Copy Markdown

Independent confirmation of this bug, in case it helps the review.

Version: ShellSpec 0.28.1 (the latest release), on macOS 27 arm64 with /bin/sh (bash 3.2 in posix mode), /bin/dash, /bin/bash 3.2, bash 5 and native /bin/zsh. The master branch (SHELLSPEC_VERSION='0.29.0-dev') still has export SHELLSPEC_INFILE=file on line 37 of shellspec.

What we saw: our project sets --random examples in .shellspec, and every run left a file literally named file, holding the shuffled example list, in the project root. Our default-deny .gitignore hid it, so it went unnoticed until a file-level comparison found it.

Minimal reproduction (same result as the PR description):

d=$(mktemp -d) && cd "$d" && mkdir spec
cat > spec/x_spec.sh <<'EOF'
Describe 'x'
  It 'a'
    When call true
    The status should be success
  End
End
EOF
printf 'IMPORTANT USER DATA\n' > file
shellspec --shell /bin/sh --random examples; echo "exit=$?"
cat file

Result: the run exits 0 and file now holds spec/x_spec.sh:@1-1; the original content is gone. --random specfiles does the same. With --random none, no file is created.

Cause, as the PR says: SHELLSPEC_INFILE defaults to the relative path file, and libexec/shellspec-runner.sh (line 184 in 0.28.1) writes the shuffled list to it with >"$SHELLSPEC_INFILE". Unlike the other per-run files, it is not under $SHELLSPEC_TMPBASE, so it lands in the working directory and is never removed.

The change in this PR, SHELLSPEC_INFILE="$SHELLSPEC_TMPBASE/.shellspec-infile", matches what we would expect, and it would be good to see it merged: as it stands, --random can overwrite a user's file without warning.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants