The job token cannot read daniel/gitdan-crates, so CI authenticates with a read-only deploy key. The action writes it 0600 outside the workspace, pins the forge's host key (RSA, the only type offered, confirmed against the trusted known_hosts record and a fresh keyscan), exports GIT_SSH_COMMAND, proves access with ls-remote, and fails on a Cargo.lock with two commits of the crates repo. The selftest is wired into selftest.sh. Existing actions and scripts are untouched. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MfBCVNDv3y5gm2djeZSAk4
180 lines
10 KiB
Bash
Executable File
180 lines
10 KiB
Bash
Executable File
#!/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 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"
|
|
[ -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" </dev/null
|
|
KEY=$(cat "$scratch/id") # command substitution drops the trailing newline
|
|
secret_line=$(sed -n 2p "$scratch/id")
|
|
else
|
|
echo "NOTE: ssh-keygen not found; key-load and fingerprint checks are skipped"
|
|
secret_line=SELFTESTSECRETKEYMATERIAL
|
|
KEY=$(printf -- '-----BEGIN OPENSSH PRIVATE KEY-----\n%s\n-----END OPENSSH PRIVATE KEY-----' "$secret_line")
|
|
fi
|
|
|
|
run_access() { # run_access <name> [VAR=value ...]; leaves $scratch/<name>.{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
|
|
ok "key is 0600 in a 0700 directory outside the workspace and in no output, env file or argv"
|
|
|
|
[ "$(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 || fail "key without trailing newline does not load"
|
|
run_access crlf DEPLOY_KEY="$(sed 's/$/\r/' "$scratch/id")" || fail "CRLF key refused"
|
|
crlf_file=$(sed -n "s/.* -i '\\([^']*\\)'.*/\\1/p" "$scratch/crlf.env")
|
|
ssh-keygen -y -P '' -f "$crlf_file" >/dev/null 2>&1 </dev/null || fail "CRLF key does not load"
|
|
fi
|
|
ok "a key lacking a trailing newline (or with CRLF endings) loads"
|
|
|
|
for opt in "IdentitiesOnly=yes" "StrictHostKeyChecking=yes" "BatchMode=yes" "GlobalKnownHostsFile=/dev/null" "-F /dev/null"; do
|
|
grep -qF -- "$opt" <<<"$cmd" || fail "GIT_SSH_COMMAND lacks $opt: $cmd"
|
|
done
|
|
kh=$(sed -n "s/.*UserKnownHostsFile='\\([^']*\\)'.*/\\1/p" <<<"$cmd")
|
|
cmp -s "$kh" "$script_dir/gitdan-known-hosts" || fail "known-hosts file is not the pinned one"
|
|
grep -q '^CARGO_' "$scratch/good.env" && fail "a CARGO_* variable was exported"
|
|
[ "$(wc -l < "$scratch/good.env")" = 1 ] || fail "more than GIT_SSH_COMMAND was exported"
|
|
grep -qx 'ls-remote' <(sed -n 1p "$scratch/good.argv") || fail "git was not run as ls-remote"
|
|
grep -qF 'ssh://git@gitdan.com:22022/daniel/gitdan-crates.git' "$scratch/good.argv" || fail "ls-remote did not target gitdan-crates"
|
|
[ "$(cat "$scratch/good.ssh")" = "$cmd" ] || fail "ls-remote ran without the exported GIT_SSH_COMMAND"
|
|
ok "GIT_SSH_COMMAND pins one key and one known-hosts file strictly; nothing else is exported"
|
|
|
|
if run_access denied DEPLOY_KEY="$KEY" STUB_GIT_RC=128 STUB_GIT_ERR='git@gitdan.com: Permission denied (publickey).'; then
|
|
fail "a failed ls-remote passed"
|
|
fi
|
|
grep -q GITDAN_CRATES_DEPLOY_KEY "$scratch/denied.out" || fail "ls-remote failure does not name the secret"
|
|
grep -q 'Permission denied' "$scratch/denied.out" || fail "ls-remote failure hid git's own message"
|
|
ok "a failed ls-remote fails the step, naming the secret"
|
|
|
|
# A PATH holding only what the script needs, so `ssh` is absent.
|
|
nossh="$scratch/nossh"; mkdir -p "$nossh"
|
|
for t in bash env dirname mktemp tr chmod cp cat timeout; do ln -s "$(command -v "$t")" "$nossh/$t"; done
|
|
cp "$stub/git" "$nossh/git"
|
|
: > "$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"
|