diff --git a/.github/workflows/audit-health.yml b/.github/workflows/audit-health.yml index 2d939d9..20102f0 100644 --- a/.github/workflows/audit-health.yml +++ b/.github/workflows/audit-health.yml @@ -73,7 +73,19 @@ jobs: if: github.event_name == 'pull_request' env: GH_TOKEN: ${{ secrets.FLEET_PAT || secrets.GITHUB_TOKEN }} - run: bash scripts/ci/audit-health.sh --static + # The secrets context, whose KEYS are the names this run can see. + # Listing secrets through the API needs a scope no token here has, so + # without this the check could only ever withhold — and a check that + # can only withhold is decoration. Passed as an env var rather than + # interpolated into the script body (no shell-quoting of secret + # material) and reduced to keys before anything is printed. Actions + # masks the values regardless; none is ever echoed. + SECRETS_CONTEXT: ${{ toJSON(secrets) }} + run: | + set -uo pipefail + SECRET_NAMES=$(printf '%s' "$SECRETS_CONTEXT" | jq -r 'keys[]') + export SECRET_NAMES + bash scripts/ci/audit-health.sh --static - name: Is the layer that watches everything else awake if: github.event_name != 'pull_request' diff --git a/scripts/ci/audit-health.sh b/scripts/ci/audit-health.sh index d7cba5c..e051565 100755 --- a/scripts/ci/audit-health.sh +++ b/scripts/ci/audit-health.sh @@ -153,8 +153,20 @@ fi # So both listings must SUCCEED before any verdict is drawn. If either cannot # be read the phantom check is withheld entirely, because a partial listing # charges every secret defined in the half we could not see. +# +# IN ACTIONS there is a way that needs no scope at all: `toJSON(secrets)` hands +# the workflow its own secrets context, whose KEYS are the names available to +# that run. The caller passes those names in SECRET_NAMES (one per line) via an +# env var, never inline and never printed — Actions masks the values anyway, +# but nothing here ever holds one. When SECRET_NAMES is set it is authoritative +# and the API listings are not consulted, which is what lets this check bite on +# a PR instead of withholding forever. A check that can only ever withhold is +# decoration, and decoration is the thing this file exists to delete. known_secrets=$'GITHUB_TOKEN\ntoken' secrets_readable=yes +if [ -n "${SECRET_NAMES:-}" ]; then + known_secrets+=$'\n'"$SECRET_NAMES" +else if s=$(gh secret list -R "$OWNER/$REPO" --json name --jq '.[].name' 2>/dev/null); then known_secrets+=$'\n'"$s" else @@ -165,6 +177,7 @@ if s=$(gh api "orgs/$OWNER/actions/secrets" --jq '.secrets[].name' 2>/dev/null); else secrets_readable=no fi +fi if [ "$STATIC" = yes ]; then echo "static mode: checking only what this diff controls (secrets that exist)." diff --git a/scripts/ci/test-audit-health.sh b/scripts/ci/test-audit-health.sh index f4fbce4..040f5bd 100755 --- a/scripts/ci/test-audit-health.sh +++ b/scripts/ci/test-audit-health.sh @@ -300,6 +300,41 @@ grep -q "REALLY_EXISTS" <<<"$out" && bad "charged a secret while unable to list grep -q "WITHHELD" <<<"$out" && ok "says the check was withheld" || bad "silently skipped instead of withholding out loud" [ $rc -eq 0 ] && ok "does not fail the run on an outage" || bad "an outage failed the check (exit $rc)" +# SECRET_NAMES (from toJSON(secrets) in Actions) is authoritative when set, so +# the check BITES in CI instead of withholding forever. Same blind stub as +# above — the API listings fail — but the names arrive by env var. +d=$(new_case) +mkwf "$d" ctx.yml '17 6 * * 1' " env: + A: \${{ secrets.PROVIDED }} + B: \${{ secrets.ABSENT_ONE }}" +echo '{"workflows":[{"id":1,"path":".github/workflows/ctx.yml","name":"C","state":"active"}]}' > "$d/spec/WFLIST" +runrow success 1 > "$d/spec/RUNS_1" +cat > "$d/bin/gh" <<'BLIND2' +#!/usr/bin/env bash +spec="$GH_SPEC" +case "$1 $2" in + "secret list") echo "gh: insufficient scope" >&2; exit 1 ;; +esac +if [ "$1" = "api" ]; then + case "$2" in + *"actions/secrets") echo "gh: HTTP 403" >&2; exit 1 ;; + *"/actions/workflows") cat "$spec/WFLIST"; exit 0 ;; + *"/actions/workflows/"*"/runs"*) + id=$(sed -E 's|.*/workflows/([0-9]+)/runs.*|\1|' <<<"$2") + [ -f "$spec/RUNS_$id" ] && { cat "$spec/RUNS_$id"; exit 0; } + echo '{"workflow_runs":[]}'; exit 0 ;; + esac +fi +exit 0 +BLIND2 +chmod +x "$d/bin/gh" +out=$( cd "$d" && PATH="$d/bin:$PATH" GH_SPEC="$d/spec" OWNER=acme REPO=widget \ + SECRET_NAMES=$'PROVIDED\nOTHER' bash "$AUDIT" --static 2>&1 ); rc=$? +grep -q "PROVIDED" <<<"$out" && bad "charged a secret the context says exists" || ok "SECRET_NAMES satisfies a referenced secret" +grep -q "ABSENT_ONE" <<<"$out" && ok "still bites on one the context does NOT list" || bad "SECRET_NAMES suppressed a real phantom" +[ $rc -eq 1 ] && ok "the check BITES in CI rather than withholding forever" || bad "withheld despite having the names (exit $rc)" +grep -q "WITHHELD" <<<"$out" && bad "withheld even though SECRET_NAMES was supplied" || ok "does not withhold when the names are supplied" + # ============================================================================= echo echo "audit-health — --static judges the DIFF, never the world"