#!/usr/bin/env bash # Regression test for the gitdan-crates-access action's two scripts. # # crates-lock-check.sh # 1. ONE COMMIT PASSES — however many packages share it, and whatever the # query string or URL spelling. # 2. TWO COMMITS FAIL — and the message names both. # 3. NONE PASSES — including a lockfile whose only git source is another # repo, with commits of its own. # 4. A MISSING LOCKFILE FAILS — a mistyped path must not read as "clean". # # crates-access.sh (git is stubbed: there is no key and no network here) # 5. EMPTY KEY FAILS, naming the secret. # 6. THE KEY STAYS PRIVATE — 0600 in a 0700 directory outside the # workspace, absent from argv, output and $GITHUB_ENV, and a key with no # trailing newline (or CRLF line ends) still loads. # 7. GIT_SSH_COMMAND USES ONLY THAT KEY AND THE PINNED HOST KEY, strictly, # and no CARGO_* variable is exported (those split sccache keys). # 8. A FAILED ls-remote FAILS THE STEP, naming the secret. # 9. NO ssh CLIENT FAILS CLEARLY; A SCRATCH DIR IN THE WORKSPACE IS REFUSED. # 10. THE PINNED HOST KEY is the one recorded in README.md (SHA256 below). # # What this cannot prove: that the live forge accepts the key, and that its # host key is still the pinned one. Both are first-consumer-run facts. set -euo pipefail script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) lock_check="$script_dir/crates-lock-check.sh" access="$script_dir/crates-access.sh" PINNED_FPR='SHA256:XAMzCdVbcQhcH/cnynQO7gPLFqHMh9wuoUysK9EhBP0' scratch=$(mktemp -d) trap 'rm -rf "$scratch"' EXIT pass_count=0 fail() { echo "ASSERTION FAILED: $*" >&2; exit 1; } ok() { pass_count=$((pass_count + 1)); echo "PASS: $*"; } SRC='git+ssh://git@gitdan.com:22022/daniel/gitdan-crates.git' sha_a=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa sha_b=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb entry() { printf '[[package]]\nname = "%s"\nversion = "0.1.0"\nsource = "%s"\n\n' "$1" "$2"; } check() { bash "$lock_check" "$@"; } echo "=== 1-4. crates-lock-check.sh ===" { entry core "$SRC?branch=main#$sha_a"; entry shared "$SRC?branch=main#$sha_a" entry serde "registry+https://github.com/rust-lang/crates.io-index"; } > "$scratch/one.lock" check "$scratch/one.lock" >/dev/null || fail "one commit shared by two packages was refused" ok "one commit passes, however many packages share it" { entry core "$SRC?branch=main#$sha_a"; entry shared "$SRC?rev=${sha_a}#$sha_a" entry odd "git+https://gitdan.com/daniel/gitdan-crates#$sha_a"; } > "$scratch/spellings.lock" check "$scratch/spellings.lock" >/dev/null || fail "one commit under three spellings was refused" ok "query string and URL spelling do not make a second commit" { entry core "$SRC?branch=main#$sha_a"; entry shared "$SRC?branch=main#$sha_b"; } > "$scratch/two.lock" if out=$(check "$scratch/two.lock" 2>&1); then fail "two commits passed"; fi [[ "$out" == *"$sha_a"* && "$out" == *"$sha_b"* ]] || fail "failure did not name both commits: $out" ok "two commits fail, naming both" { entry core "$SRC?branch=main#$sha_a"; entry shared "git+https://gitdan.com/daniel/gitdan-crates.git#$sha_b"; } > "$scratch/two2.lock" check "$scratch/two2.lock" >/dev/null 2>&1 && fail "two commits under different spellings passed" ok "two commits fail across URL spellings too" { entry core "$SRC?branch=main#$sha_a"; entry shared "git+https://gitdan.com/daniel/gitdan-crates/?branch=main#$sha_b"; } > "$scratch/slash.lock" check "$scratch/slash.lock" >/dev/null 2>&1 && fail "a second commit spelled with a trailing slash passed" { entry core "$SRC?branch=main#$sha_a"; entry shared "git+https://gitdan.com/Daniel/GitDan-Crates.git#$sha_b"; } > "$scratch/case.lock" check "$scratch/case.lock" >/dev/null 2>&1 && fail "a second commit spelled in another case passed" ok "a trailing slash or a different case still counts as the repo" { entry core "$SRC?branch=main#$sha_a"; entry shared "git+https://gitdan.com/notdaniel/gitdan-crates.git#$sha_b"; } > "$scratch/owner.lock" check "$scratch/owner.lock" >/dev/null 2>&1 || fail "a look-alike owner counted as a second commit" ok "a look-alike owner is not this source" { entry other "git+ssh://git@gitdan.com:22022/daniel/gitdan-crates-extra.git#$sha_a" entry a "git+https://example.com/x/y#$sha_a"; entry b "git+https://example.com/x/y#$sha_b" entry serde "registry+https://github.com/rust-lang/crates.io-index"; } > "$scratch/none.lock" check "$scratch/none.lock" >/dev/null || fail "a lockfile with no gitdan-crates entry was refused" printf '' > "$scratch/empty.lock" check "$scratch/empty.lock" >/dev/null || fail "an empty lockfile was refused" ok "none passes, and other git sources (or look-alike repos) are not counted" if out=$(check "$scratch/absent.lock" 2>&1); then fail "a missing lockfile passed"; fi grep -q "lockfile" <<<"$out" || fail "missing-lockfile message does not mention the input: $out" ok "a missing lockfile fails" echo "=== 5-9. crates-access.sh ===" stub="$scratch/stub"; mkdir -p "$stub" cat > "$stub/git" <<'STUB' #!/usr/bin/env bash printf '%s\n' "$@" > "$STUB_DIR/git.argv" printf '%s\n' "${GIT_SSH_COMMAND:-}" > "$STUB_DIR/git.ssh" printf '%s' "${DEPLOY_KEY:-}" > "$STUB_DIR/git.key" [ -z "${STUB_GIT_ERR:-}" ] || echo "$STUB_GIT_ERR" >&2 exit "${STUB_GIT_RC:-0}" STUB chmod +x "$stub/git" # A real key, so the parse check inside the script is exercised when # ssh-keygen exists; otherwise a marker the leak assertions can still grep for. have_keygen=0; command -v ssh-keygen >/dev/null && have_keygen=1 if [ "$have_keygen" = 1 ]; then ssh-keygen -q -t ed25519 -N '' -C selftest -f "$scratch/id" [VAR=value ...]; leaves $scratch/.{out,env} local name="$1"; shift : > "$scratch/$name.env" env PATH="$stub:$PATH" STUB_DIR="$scratch" GITHUB_ENV="$scratch/$name.env" \ RUNNER_TEMP="$scratch/runner-temp" GITHUB_WORKSPACE="$scratch/workspace" \ "$@" bash "$access" > "$scratch/$name.out" 2>&1 } mkdir -p "$scratch/runner-temp" "$scratch/workspace" if run_access empty DEPLOY_KEY=; then fail "an empty key passed"; fi grep -q GITDAN_CRATES_DEPLOY_KEY "$scratch/empty.out" || fail "empty-key message does not name the secret" ok "an empty deploy-key fails, naming GITDAN_CRATES_DEPLOY_KEY" run_access good DEPLOY_KEY="$KEY" || { cat "$scratch/good.out"; fail "a good key was refused"; } cp "$scratch/git.argv" "$scratch/good.argv"; cp "$scratch/git.ssh" "$scratch/good.ssh" cmd=$(sed -n 's/^GIT_SSH_COMMAND=//p' "$scratch/good.env") keyfile=$(sed -n "s/.* -i '\\([^']*\\)'.*/\\1/p" <<<"$cmd") [ -f "$keyfile" ] || fail "key file not found from GIT_SSH_COMMAND: $cmd" [ "$(stat -c %a "$keyfile")" = 600 ] || fail "key file mode is $(stat -c %a "$keyfile"), not 600" [ "$(stat -c %a "$(dirname "$keyfile")")" = 700 ] || fail "key directory is not 0700" case "$(dirname "$keyfile")/" in "$scratch/workspace"/*) fail "key landed in the workspace" ;; esac grep -rqF "$secret_line" "$scratch/workspace" && fail "key material found in the workspace" for f in good.out good.env good.argv good.ssh; do grep -qF "$secret_line" "$scratch/$f" && fail "key material leaked into $f" done [ ! -s "$scratch/git.key" ] || fail "DEPLOY_KEY is still in the environment of git" ok "key is 0600 in a 0700 directory outside the workspace and in no output, env file, argv or child environment" [ "$(tail -c1 "$keyfile" | od -An -c | tr -d ' ')" = '\n' ] || fail "key file does not end in a newline" if [ "$have_keygen" = 1 ]; then ssh-keygen -y -P '' -f "$keyfile" >/dev/null 2>&1 /dev/null 2>&1 "$scratch/nossh.env" if env PATH="$nossh" STUB_DIR="$scratch" GITHUB_ENV="$scratch/nossh.env" DEPLOY_KEY="$KEY" \ "$nossh/bash" "$access" > "$scratch/nossh.out" 2>&1; then fail "no ssh client passed"; fi grep -q 'no ssh client' "$scratch/nossh.out" || fail "missing-ssh message unclear: $(cat "$scratch/nossh.out")" ok "a missing ssh client fails with a clear message" mkdir -p "$scratch/workspace/tmp" if run_access inws DEPLOY_KEY="$KEY" RUNNER_TEMP="$scratch/workspace/tmp"; then fail "scratch dir inside the workspace passed"; fi grep -q 'inside the workspace' "$scratch/inws.out" || fail "workspace refusal unclear" ok "a scratch directory inside the workspace is refused" echo "=== 10. pinned host key ===" [ "$(wc -l < "$script_dir/gitdan-known-hosts")" -ge 1 ] || fail "known-hosts file is empty" grep -q '^\[gitdan.com\]:22022 ' "$script_dir/gitdan-known-hosts" || fail "pinned entry is not for [gitdan.com]:22022" if [ "$have_keygen" = 1 ]; then fprs=$(ssh-keygen -lf "$script_dir/gitdan-known-hosts" | awk '{print $2}') grep -qx "$PINNED_FPR" <<<"$fprs" || fail "pinned key fingerprint is not $PINNED_FPR (got: $fprs)" fi ok "pinned host key is for [gitdan.com]:22022 and has the recorded fingerprint" echo "crates-access-selftest: $pass_count checks passed"