From 89613f87a1441def62b0009fb343ed1963bdc738 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:56:15 +0000 Subject: [PATCH 1/9] Decode anchored workflow pins before comparing identities --- scripts/check-workflow-edges.sh | 44 +++++++++++++++++++------- tests/workflows/workflow_edges_test.sh | 22 ++++++++++++- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/scripts/check-workflow-edges.sh b/scripts/check-workflow-edges.sh index 9ab58b1..dac18a8 100755 --- a/scripts/check-workflow-edges.sh +++ b/scripts/check-workflow-edges.sh @@ -107,13 +107,25 @@ produced_artifacts() { ' "$1" } -# env_pin FILE VAR — the value of a `VAR:` line in the workflow's env blocks. +# env_pin FILE VAR — the first matching mapping value, including reusable inputs. +# Decode YAML before comparing pins, as the checkout reader does: anchor syntax and +# aliases are not part of a pin. Traverse entries in order before their siblings +# so the first-match contract remains independent of a key's nesting depth. env_pin() { - awk -v var="$2" ' - $0 ~ "^[[:space:]]*" var ":[[:space:]]" { - sub("^[[:space:]]*" var ":[[:space:]]*", ""); gsub(/["'"'"']/, ""); print; exit - } - ' "$1" + local workflow="$1" var="$2" + yq --yaml-fix-merge-anchor-to-spec=true -p yaml -o json -I0 'explode(.)' "$workflow" | + jq -sr --arg var "$var" ' +def pins: + if type == "object" then to_entries[] | + if .key == $var then .value else .value | pins end + elif type == "array" then .[] | pins + else empty end; +if length != 1 then error("expected exactly one YAML document") +else first(.[0] | pins) | + if type == "object" or type == "array" then error("pin must be a scalar") + else tostring end +end + ' } @@ -335,13 +347,17 @@ check_pin() { # check_pin REPORTER REPORT_FLAG PRODUCER VAR [INPUT] local report="$1" flag="$2" producer="$3" var="$4" input="${5:-}" local in_reporter in_producer pin_name="$var" in_reporter="$({ grep -o -- "${flag}[[:space:]]\\+[0-9a-f]\\{40\\}" "$report" || true; } | awk '{print $2}' | head -n1)" - in_producer="$(env_pin "$WORKFLOWS_DIR/$producer" "$var" | head -n1)" + in_producer="$(env_pin "$WORKFLOWS_DIR/$producer" "$var")" || { + fail "${producer} ${var} pin could not be inspected"; return; + } if [ -z "$in_reporter" ]; then fail "$(basename "$report") names no ${flag}; its findings would carry no tool identity" return fi if [ -z "$in_producer" ] && [ -n "$input" ] && [ -n "$(reusable_calls "$WORKFLOWS_DIR/$producer")" ]; then - in_producer="$(env_pin "$WORKFLOWS_DIR/$producer" "$input" | head -n1)" + in_producer="$(env_pin "$WORKFLOWS_DIR/$producer" "$input")" || { + fail "${producer} with: ${input} pin could not be inspected"; return; + } pin_name="with: $input" if [ -z "$in_producer" ]; then fail "'${producer}' calls a reusable workflow without passing ${input}, so the scan runs that workflow's default, not the ${flag} ${in_reporter:0:12} the verdict claims; pass 'with: ${input}: '" @@ -371,7 +387,9 @@ trap 'rm -rf "$names_file" "$pins_file" "$reusable_cache"' EXIT for wf in "$WORKFLOWS_DIR"/*.yml "$WORKFLOWS_DIR"/*.yaml; do [ -e "$wf" ] || continue for var in HYPATIA_SHA HYPATIA_REF PANIC_ATTACK_SHA PONS_SHA; do - v="$(env_pin "$wf" "$var" | head -n1)" + v="$(env_pin "$wf" "$var")" || { + fail "$(basename "$wf") ${var} pin could not be inspected"; continue; + } [ -n "$v" ] || continue case "$v" in *'${{'* | 'latest' | 'HEAD') continue ;; # floating by design, documented at the pin @@ -393,7 +411,9 @@ for wf in "$WORKFLOWS_DIR"/*.yml "$WORKFLOWS_DIR"/*.yaml; do done <<< "$checkout_rows" # A reusable call's hypatia-ref input pins the same scanner as HYPATIA_SHA. if [ -n "$(reusable_calls "$wf")" ]; then - v="$(env_pin "$wf" hypatia-ref | head -n1)" + v="$(env_pin "$wf" hypatia-ref)" || { + fail "$(basename "$wf") with: hypatia-ref pin could not be inspected"; continue; + } case "$v" in "" | *'${{'* | 'latest' | 'HEAD') ;; *) printf '%s\t%s\t%s\n' HYPATIA_SHA "$v" "$(basename "$wf") (with: hypatia-ref)" >> "$pins_file" ;; @@ -410,8 +430,8 @@ while IFS= read -r var; do done <<< "$(cut -f1 "$pins_file" | sort -u)" # HYPATIA_SHA and HYPATIA_REF are two spellings of the same pin. -h_sha="$(env_pin "$WORKFLOWS_DIR/hypatia-scan.yml" HYPATIA_SHA | head -n1)" -h_ref="$(env_pin "$WORKFLOWS_DIR/hypatia-scan.yml" HYPATIA_REF | head -n1)" +h_sha="$(env_pin "$WORKFLOWS_DIR/hypatia-scan.yml" HYPATIA_SHA)" || fail "hypatia-scan.yml HYPATIA_SHA pin could not be inspected" +h_ref="$(env_pin "$WORKFLOWS_DIR/hypatia-scan.yml" HYPATIA_REF)" || fail "hypatia-scan.yml HYPATIA_REF pin could not be inspected" if [ -n "$h_sha" ] && [ -n "$h_ref" ] && [ "$h_sha" != "$h_ref" ]; then fail "hypatia-scan.yml pins HYPATIA_SHA ${h_sha:0:12} and HYPATIA_REF ${h_ref:0:12}" fi diff --git a/tests/workflows/workflow_edges_test.sh b/tests/workflows/workflow_edges_test.sh index bc50172..794ffdc 100755 --- a/tests/workflows/workflow_edges_test.sh +++ b/tests/workflows/workflow_edges_test.sh @@ -52,7 +52,7 @@ says "the summary counts the edges it checked" "workflow_run edge(s)" echo "=== 2. Firing: a dead workflow_run edge ===" fresh >/dev/null sed -i 's/^name: Hypatia Security Scan$/name: Hypatia Scan/' "$WORK/repo/.github/workflows/hypatia-scan.yml" -expect_rc 1 "renaming a producer is caught (workflow_run matches name:, not the file)" +expect_rc 1 "renaming a producer is caught (workflow_run matches name:, not the file)" says "the failure names the dead edge" "Hypatia Security Scan" says "the failure says what it breaks" "never re-render" @@ -204,6 +204,17 @@ export EDGES_REUSABLE_DIR="$REUSABLES" fresh >/dev/null; reusable; delegate "$HSHA" expect_rc 0 "a reusable producer passing the verdict's pin resolves both edges" +fresh >/dev/null; reusable; delegate "&scanner \"$HSHA\"" +expect_rc 0 "an anchored reusable hypatia-ref scalar matches the verdict pin" +sed -i "s/$HSHA/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/" "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 1 "an anchored reusable hypatia-ref cannot hide a wrong pin" + +fresh >/dev/null; reusable; delegate '*scanner' +sed -i "/^jobs:/i\x-scanner: \&scanner \"$HSHA\"" "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 0 "an aliased reusable hypatia-ref scalar matches the verdict pin" +sed -i "s/$HSHA/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/" "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 1 "an aliased reusable hypatia-ref cannot hide a wrong pin" + fresh >/dev/null; reusable; delegate expect_rc 1 "a reusable producer that passes no hypatia-ref is caught" says "the failure says the default runs instead" "without passing hypatia-ref" @@ -306,6 +317,14 @@ expect_rc 1 "a wrong literal checkout ref is caught even with the correct env pi says "the disagreement identifies the actual checkout" "Hypatia checkout" fresh >/dev/null; checkout_fixture '${{ env.HYPATIA_SHA }}' "$(printf 'env:\n HYPATIA_SHA: %s' "$HSHA")" expect_rc 0 "an exact, uniquely declared workflow env binding resolves" +fresh >/dev/null; checkout_fixture '${{ env.HYPATIA_SHA }}' "$(printf 'env:\n HYPATIA_SHA: &scanner "%s"' "$HSHA")" +expect_rc 0 "an anchored HYPATIA_SHA agrees with its decoded checkout ref" +sed -i "s/$HSHA/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/" "$WORK/repo/.github/workflows/oracle-pin-control.yml" +expect_rc 1 "an anchored HYPATIA_SHA cannot hide a wrong checkout pin" +fresh >/dev/null; checkout_fixture '${{ env.HYPATIA_SHA }}' "$(printf 'env:\n PIN: &scanner "%s"\n HYPATIA_SHA: *scanner' "$HSHA")" +expect_rc 0 "an aliased HYPATIA_SHA agrees with its decoded checkout ref" +sed -i "s/$HSHA/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/" "$WORK/repo/.github/workflows/oracle-pin-control.yml" +expect_rc 1 "an aliased HYPATIA_SHA cannot hide a wrong checkout pin" fresh >/dev/null; checkout_fixture '${{ env.HYPATIA_SHA }}' sed -i "/ runs-on:/i\ env:\n HYPATIA_SHA: $HSHA" "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 0 "an exact, uniquely declared binding in the same job resolves" @@ -541,6 +560,7 @@ SH chmod +x "$WORK/failing-parser/yq" PATH="$WORK/failing-parser:$PATH" expect_rc 1 "a failing decode cannot disappear behind valid empty JSON output" says "the captured parser failure is reported" "checkout refs could not be inspected" +says "env pin extraction also preserves a failing decoder's status" "HYPATIA_SHA pin could not be inspected" echo "" if [ "$fail" -ne 0 ]; then From 2dcec12c66fdb26462b47c3079fecb57abb59422 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:58:52 +0000 Subject: [PATCH 2/9] Name the shared Hypatia fixture expression --- tests/workflows/workflow_edges_test.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/workflows/workflow_edges_test.sh b/tests/workflows/workflow_edges_test.sh index 794ffdc..a0a5909 100755 --- a/tests/workflows/workflow_edges_test.sh +++ b/tests/workflows/workflow_edges_test.sh @@ -247,6 +247,7 @@ unset EDGES_REUSABLE_DIR echo "=== 12. Hypatia checkout refs participate in the one-tool pin contract ===" readonly PIN_REF='${{ env.PIN }}' +readonly HYPATIA_REF_EXPR='${{ env.HYPATIA_SHA }}' checkout_fixture() { local ref="$1" binding="${2:-}" cat > "$WORK/repo/.github/workflows/oracle-pin-control.yml" </dev/null; checkout_fixture 1111111111111111111111111111111111111111 "$(printf 'env:\n HYPATIA_SHA: %s' "$HSHA")" expect_rc 1 "a wrong literal checkout ref is caught even with the correct env pin" says "the disagreement identifies the actual checkout" "Hypatia checkout" -fresh >/dev/null; checkout_fixture '${{ env.HYPATIA_SHA }}' "$(printf 'env:\n HYPATIA_SHA: %s' "$HSHA")" +fresh >/dev/null; checkout_fixture "$HYPATIA_REF_EXPR" "$(printf 'env:\n HYPATIA_SHA: %s' "$HSHA")" expect_rc 0 "an exact, uniquely declared workflow env binding resolves" -fresh >/dev/null; checkout_fixture '${{ env.HYPATIA_SHA }}' "$(printf 'env:\n HYPATIA_SHA: &scanner "%s"' "$HSHA")" +fresh >/dev/null; checkout_fixture "$HYPATIA_REF_EXPR" "$(printf 'env:\n HYPATIA_SHA: &scanner "%s"' "$HSHA")" expect_rc 0 "an anchored HYPATIA_SHA agrees with its decoded checkout ref" sed -i "s/$HSHA/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/" "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "an anchored HYPATIA_SHA cannot hide a wrong checkout pin" -fresh >/dev/null; checkout_fixture '${{ env.HYPATIA_SHA }}' "$(printf 'env:\n PIN: &scanner "%s"\n HYPATIA_SHA: *scanner' "$HSHA")" +fresh >/dev/null; checkout_fixture "$HYPATIA_REF_EXPR" "$(printf 'env:\n PIN: &scanner "%s"\n HYPATIA_SHA: *scanner' "$HSHA")" expect_rc 0 "an aliased HYPATIA_SHA agrees with its decoded checkout ref" sed -i "s/$HSHA/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/" "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "an aliased HYPATIA_SHA cannot hide a wrong checkout pin" -fresh >/dev/null; checkout_fixture '${{ env.HYPATIA_SHA }}' +fresh >/dev/null; checkout_fixture "$HYPATIA_REF_EXPR" sed -i "/ runs-on:/i\ env:\n HYPATIA_SHA: $HSHA" "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 0 "an exact, uniquely declared binding in the same job resolves" fresh >/dev/null; checkout_fixture "$PIN_REF" @@ -398,9 +399,9 @@ expect_rc 0 "a matching ref in a complete flow step is accepted" fresh >/dev/null; checkout_fixture "$HSHA" printf ' - *checkout-options\n' >> "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "an undefined whole-step alias fails YAML parsing" -fresh >/dev/null; checkout_fixture '${{ env.HYPATIA_SHA }}' +fresh >/dev/null; checkout_fixture "$HYPATIA_REF_EXPR" expect_rc 1 "an undefined checkout env binding is refused" -fresh >/dev/null; checkout_fixture '${{ env.HYPATIA_SHA }}' "$(printf 'env:\n HYPATIA_SHA: %s\n HYPATIA_SHA: %s' "$HSHA" "$HSHA")" +fresh >/dev/null; checkout_fixture "$HYPATIA_REF_EXPR" "$(printf 'env:\n HYPATIA_SHA: %s\n HYPATIA_SHA: %s' "$HSHA" "$HSHA")" expect_rc 1 "duplicate identical checkout env bindings are ambiguous" fresh >/dev/null; checkout_fixture "$HSHA" sed -i '/ ref:/d' "$WORK/repo/.github/workflows/oracle-pin-control.yml" From 6a5d83c0dc347571085eed34c681e52f5dd86efa Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:14:58 +0000 Subject: [PATCH 3/9] Scope pin bindings and cache validated workflow decodes --- scripts/check-workflow-edges.sh | 103 ++++++++++++++++--------- tests/workflows/workflow_edges_test.sh | 49 +++++++++++- 2 files changed, 114 insertions(+), 38 deletions(-) diff --git a/scripts/check-workflow-edges.sh b/scripts/check-workflow-edges.sh index dac18a8..0a836bd 100755 --- a/scripts/check-workflow-edges.sh +++ b/scripts/check-workflow-edges.sh @@ -107,36 +107,22 @@ produced_artifacts() { ' "$1" } -# env_pin FILE VAR — the first matching mapping value, including reusable inputs. -# Decode YAML before comparing pins, as the checkout reader does: anchor syntax and -# aliases are not part of a pin. Traverse entries in order before their siblings -# so the first-match contract remains independent of a key's nesting depth. -env_pin() { - local workflow="$1" var="$2" - yq --yaml-fix-merge-anchor-to-spec=true -p yaml -o json -I0 'explode(.)' "$workflow" | - jq -sr --arg var "$var" ' -def pins: - if type == "object" then to_entries[] | - if .key == $var then .value else .value | pins end - elif type == "array" then .[] | pins - else empty end; -if length != 1 then error("expected exactly one YAML document") -else first(.[0] | pins) | - if type == "object" or type == "array" then error("pin must be a scalar") - else tostring end -end - ' -} - - -# hypatia_checkouts FILE — inspect real YAML nodes, never source indentation. +# workflow_json FILE — the process-local, validated JSON path (or raw-AST +# rejection row with status 3). Key by absolute physical parent + filename, +# preserving the full path inside the private cache rather than using basenames. +# Failed decodes stay failed; never expose partial output to another lookup. # Reject duplicate mapping keys on the raw AST before aliases are expanded or # JSON is emitted; JSON conversion alone preserves/loses ambiguity downstream. -# yq decodes anchors, aliases, block scalars and flow maps. jq then checks typed -# jobs/steps/inputs and literal env bindings with step > job > workflow priority. -# An uninspectable overriding scope is an error, never permission to fall back. -hypatia_checkouts() { - local workflow="$1" preflight duplicates keys +workflow_json() { + local source="$1" parent workflow cache preflight duplicates keys + parent="$(cd "$(dirname "$source")" && pwd -P)" || return 1 + workflow="$parent/$(basename "$source")" + cache="$decoded_cache$workflow" + if [ -f "$cache.rejected" ]; then cat "$cache.rejected"; return 3; fi + if [ -f "$cache.json" ]; then printf '%s' "$cache.json"; return 0; fi + if [ -f "$cache.failed" ]; then return 1; fi + mkdir -p "$(dirname "$cache")" || return 1 + : > "$cache.failed" || return 1 preflight="$(yq -p yaml -o yaml -r -N ' ([.. | select(kind == "map") | to_entries | group_by(.key) | .[] | select(length > 1)] | length) as $duplicates | @@ -147,14 +133,54 @@ hypatia_checkouts() { while IFS=$'\t' read -r duplicates keys; do [[ "$duplicates" =~ ^[0-9]+$ && "$keys" =~ ^[0-9]+$ ]] || return 1 if (( duplicates > 0 )); then - printf '!\tduplicate YAML mapping keys (raw AST)\n'; return 0 + printf '!\tduplicate YAML mapping keys (raw AST)\n' > "$cache.rejected" || return 1 + cat "$cache.rejected"; return 3 fi if (( keys > 0 )); then - printf '!\taliased, complex or non-string YAML mapping keys are unsupported\n'; return 0 + printf '!\taliased, complex or non-string YAML mapping keys are unsupported\n' > "$cache.rejected" || return 1 + cat "$cache.rejected"; return 3 fi done <<< "$preflight" - yq --yaml-fix-merge-anchor-to-spec=true -p yaml -o json -I0 'explode(.)' "$workflow" | - jq -sr ' + yq --yaml-fix-merge-anchor-to-spec=true -p yaml -o json -I0 'explode(.)' "$workflow" > "$cache.tmp" || return 1 + jq -se 'length == 1 and (.[0] | type) == "object"' "$cache.tmp" >/dev/null || return 1 + mv "$cache.tmp" "$cache.json" || return 1 + printf '%s' "$cache.json" +} + +# env_pin FILE VAR — the first actual env binding or reusable-call input. +# Input declarations and matching keys inside unrelated data are not bindings. +# Validate before command substitution can strip a block scalar's final newline. +env_pin() { + local workflow="$1" var="$2" json + json="$(workflow_json "$workflow")" || return 1 + jq -r --arg var "$var" ' +. as $root | +first(paths as $p | select($p[-1] == $var) | + select( + (($p | length) == 2 and $p[0] == "env") or + (($p | length) == 4 and $p[0] == "jobs" and $p[2] == "env") or + (($p | length) == 6 and $p[0] == "jobs" and $p[2] == "steps" and + ($p[3] | type) == "number" and $p[4] == "env") or + (($p | length) == 4 and $p[0] == "jobs" and $p[2] == "with" and + ($root.jobs[$p[1]].uses | type) == "string" and + ($root.jobs[$p[1]].uses | test("^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+/\\.github/workflows/[^@]+@")))) | + $root | getpath($p)) | +if type == "object" or type == "array" then error("pin must be a scalar") +else tostring | + if test("\\s") then error("pin must not contain whitespace") else . end +end + ' "$json" +} + +# hypatia_checkouts FILE — inspect cached real YAML nodes, never indentation. +# jq checks typed jobs/steps/inputs and literal env bindings with step > job > +# workflow priority. An uninspectable override never permits falling back. +hypatia_checkouts() { + local workflow="$1" json status=0 + json="$(workflow_json "$workflow")" || status=$? + if [ "$status" -eq 3 ]; then printf '%s\n' "$json"; return 0; fi + [ "$status" -eq 0 ] || return 1 + jq -sr ' def text: gsub("^\\s+|\\s+$"; ""); def row($pin; $where): [$pin, $where] | @tsv; def bad($where; $why): row("!"; $where + ": " + $why); @@ -206,7 +232,7 @@ else .[0] as $root | $root.jobs | to_entries[] as $job | elif ($job.value.steps | type) != "array" then bad("workflow"; "job steps must be a sequence") else $job.value.steps | to_entries[] | inspect($root; $job; .) end end - ' + ' "$json" } # reusable_calls FILE — every job-level reusable-workflow call in FILE @@ -238,6 +264,9 @@ reusable_calls() { # A call that cannot be read is a setup error (exit 2), never a pass: the edge # through it would be unverified. reusable_cache="$(mktemp -d)" +trap 'rm -rf "$reusable_cache"' EXIT +decoded_cache="$reusable_cache/decoded" +mkdir -p "$decoded_cache" reusable_source() { local call="$1" spec ref owner repo path out spec="${call%@*}"; ref="${call##*@}" @@ -280,8 +309,8 @@ if [ -z "$edges" ]; then fail "verdict-grid-publish.yml declares no workflow_run trigger: the verdict grid would never re-render after a scanner finishes" fi -names_file="$(mktemp)" -trap 'rm -rf "$names_file" "$reusable_cache"' EXIT +names_file="$reusable_cache/names" +: > "$names_file" for wf in "$WORKFLOWS_DIR"/*.yml "$WORKFLOWS_DIR"/*.yaml; do [ -e "$wf" ] || continue n="$(workflow_name "$wf")" @@ -382,8 +411,8 @@ done # panic-attack is pinned once for its scan and once for patch-bridge triage; the # pair must agree, which is why both use env.PANIC_ATTACK_SHA. A literal second # pin in another workflow is the clash this catches. -pins_file="$(mktemp)" -trap 'rm -rf "$names_file" "$pins_file" "$reusable_cache"' EXIT +pins_file="$reusable_cache/pins" +: > "$pins_file" for wf in "$WORKFLOWS_DIR"/*.yml "$WORKFLOWS_DIR"/*.yaml; do [ -e "$wf" ] || continue for var in HYPATIA_SHA HYPATIA_REF PANIC_ATTACK_SHA PONS_SHA; do diff --git a/tests/workflows/workflow_edges_test.sh b/tests/workflows/workflow_edges_test.sh index a0a5909..293e5f9 100755 --- a/tests/workflows/workflow_edges_test.sh +++ b/tests/workflows/workflow_edges_test.sh @@ -215,6 +215,12 @@ expect_rc 0 "an aliased reusable hypatia-ref scalar matches the verdict pin" sed -i "s/$HSHA/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/" "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 1 "an aliased reusable hypatia-ref cannot hide a wrong pin" +fresh >/dev/null; reusable; delegate "$HSHA" +sed -i '/^ workflow_dispatch:/a\ inputs:\n hypatia-ref:\n type: string' "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 0 "a dispatch input declaration cannot shadow a reusable pin binding" +sed -i "s/$HSHA/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/" "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 1 "an input declaration cannot hide a wrong reusable pin binding" + fresh >/dev/null; reusable; delegate expect_rc 1 "a reusable producer that passes no hypatia-ref is caught" says "the failure says the default runs instead" "without passing hypatia-ref" @@ -327,6 +333,17 @@ expect_rc 0 "an aliased HYPATIA_SHA agrees with its decoded checkout ref" sed -i "s/$HSHA/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/" "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "an aliased HYPATIA_SHA cannot hide a wrong checkout pin" fresh >/dev/null; checkout_fixture "$HYPATIA_REF_EXPR" +sed -i '/^jobs:/i\on:\n workflow_call:\n inputs:\n HYPATIA_SHA:\n type: string' "$WORK/repo/.github/workflows/oracle-pin-control.yml" +sed -i "/ runs-on:/i\ env:\n HYPATIA_SHA: $HSHA" "$WORK/repo/.github/workflows/oracle-pin-control.yml" +expect_rc 0 "a workflow-call input declaration cannot shadow an actual env pin" +sed -i "s/$HSHA/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/" "$WORK/repo/.github/workflows/oracle-pin-control.yml" +expect_rc 1 "a workflow-call declaration cannot hide a wrong actual env pin" +fresh >/dev/null; checkout_fixture "$HYPATIA_REF_EXPR" "$(printf 'env:\n HYPATIA_SHA: |-\n %s' "$HSHA")" +expect_rc 0 "a chomped block scalar supplies the exact HYPATIA_SHA" +sed -i 's/HYPATIA_SHA: |-/HYPATIA_SHA: |/' "$WORK/repo/.github/workflows/oracle-pin-control.yml" +expect_rc 1 "a block scalar trailing newline is refused before shell substitution" +says "the decoded whitespace rejection is explicit" "pin must not contain whitespace" +fresh >/dev/null; checkout_fixture "$HYPATIA_REF_EXPR" sed -i "/ runs-on:/i\ env:\n HYPATIA_SHA: $HSHA" "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 0 "an exact, uniquely declared binding in the same job resolves" fresh >/dev/null; checkout_fixture "$PIN_REF" @@ -548,20 +565,50 @@ mkdir -p "$WORK/no-tools" PATH="$WORK/no-tools" expect_rc 2 "missing parser tools are a clear setup failure" says "the setup diagnostic names Mike Farah yq and jq" "setup requires Mike Farah yq" +fresh >/dev/null +mkdir -p "$WORK/counting-parser" +EDGES_REAL_YQ="$(command -v yq)" +EDGES_DECODE_LOG="$WORK/decode-log" +export EDGES_REAL_YQ EDGES_DECODE_LOG +: > "$EDGES_DECODE_LOG" +cat > "$WORK/counting-parser/yq" <<'SH' +#!/usr/bin/env bash +case "$*" in *'explode(.)'*) printf '%s\n' "${!#}" >> "$EDGES_DECODE_LOG" ;; esac +exec "$EDGES_REAL_YQ" "$@" +SH +chmod +x "$WORK/counting-parser/yq" +PATH="$WORK/counting-parser:$PATH" expect_rc 0 "cached workflow decoding preserves the shipped verdict" +if [ -s "$EDGES_DECODE_LOG" ] && [ -z "$(sort "$EDGES_DECODE_LOG" | uniq -d)" ]; then + ok "each workflow is decoded at most once per checker process" +else + bad "workflow decoding was absent or repeated despite the cache" +fi + fresh >/dev/null mkdir -p "$WORK/failing-parser" cat > "$WORK/failing-parser/yq" <<'SH' #!/usr/bin/env bash case "$*" in *--version*) echo 'yq (https://github.com/mikefarah/yq/) version v4.53.3' ;; - *'explode(.)'*) printf '{"jobs":{}}\n'; exit 17 ;; + *'explode(.)'*) + printf '%s\n' "${!#}" >> "$EDGES_DECODE_LOG" + if [ "${EDGES_INVALID_DECODE:-}" = documents ]; then printf '{}\n{}\n'; exit 0; fi + printf '{"jobs":{}}\n'; exit 17 ;; *) printf '0\t0\n' ;; esac SH chmod +x "$WORK/failing-parser/yq" +: > "$EDGES_DECODE_LOG" PATH="$WORK/failing-parser:$PATH" expect_rc 1 "a failing decode cannot disappear behind valid empty JSON output" says "the captured parser failure is reported" "checkout refs could not be inspected" says "env pin extraction also preserves a failing decoder's status" "HYPATIA_SHA pin could not be inspected" +if [ -s "$EDGES_DECODE_LOG" ] && [ -z "$(sort "$EDGES_DECODE_LOG" | uniq -d)" ]; then + ok "a failed decode is cached without retrying or publishing partial JSON" +else + bad "a failed workflow decode was absent or retried" +fi +: > "$EDGES_DECODE_LOG" +EDGES_INVALID_DECODE=documents PATH="$WORK/failing-parser:$PATH" expect_rc 1 "successful decoder output must still validate before entering the cache" echo "" if [ "$fail" -ne 0 ]; then From 6cc32cc00e115d3b10b4faf0424324ce80214bf0 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:18:43 +0000 Subject: [PATCH 4/9] Use Bash conditions for cache guards and controls --- scripts/check-workflow-edges.sh | 10 +++++----- tests/workflows/workflow_edges_test.sh | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/check-workflow-edges.sh b/scripts/check-workflow-edges.sh index 0a836bd..09c20c1 100755 --- a/scripts/check-workflow-edges.sh +++ b/scripts/check-workflow-edges.sh @@ -118,9 +118,9 @@ workflow_json() { parent="$(cd "$(dirname "$source")" && pwd -P)" || return 1 workflow="$parent/$(basename "$source")" cache="$decoded_cache$workflow" - if [ -f "$cache.rejected" ]; then cat "$cache.rejected"; return 3; fi - if [ -f "$cache.json" ]; then printf '%s' "$cache.json"; return 0; fi - if [ -f "$cache.failed" ]; then return 1; fi + if [[ -f "$cache.rejected" ]]; then cat "$cache.rejected"; return 3; fi + if [[ -f "$cache.json" ]]; then printf '%s' "$cache.json"; return 0; fi + if [[ -f "$cache.failed" ]]; then return 1; fi mkdir -p "$(dirname "$cache")" || return 1 : > "$cache.failed" || return 1 preflight="$(yq -p yaml -o yaml -r -N ' @@ -178,8 +178,8 @@ end hypatia_checkouts() { local workflow="$1" json status=0 json="$(workflow_json "$workflow")" || status=$? - if [ "$status" -eq 3 ]; then printf '%s\n' "$json"; return 0; fi - [ "$status" -eq 0 ] || return 1 + if [[ "$status" -eq 3 ]]; then printf '%s\n' "$json"; return 0; fi + [[ "$status" -eq 0 ]] || return 1 jq -sr ' def text: gsub("^\\s+|\\s+$"; ""); def row($pin; $where): [$pin, $where] | @tsv; diff --git a/tests/workflows/workflow_edges_test.sh b/tests/workflows/workflow_edges_test.sh index 293e5f9..25b5a0f 100755 --- a/tests/workflows/workflow_edges_test.sh +++ b/tests/workflows/workflow_edges_test.sh @@ -578,7 +578,7 @@ exec "$EDGES_REAL_YQ" "$@" SH chmod +x "$WORK/counting-parser/yq" PATH="$WORK/counting-parser:$PATH" expect_rc 0 "cached workflow decoding preserves the shipped verdict" -if [ -s "$EDGES_DECODE_LOG" ] && [ -z "$(sort "$EDGES_DECODE_LOG" | uniq -d)" ]; then +if [[ -s "$EDGES_DECODE_LOG" ]] && [[ -z "$(sort "$EDGES_DECODE_LOG" | uniq -d)" ]]; then ok "each workflow is decoded at most once per checker process" else bad "workflow decoding was absent or repeated despite the cache" @@ -602,7 +602,7 @@ chmod +x "$WORK/failing-parser/yq" PATH="$WORK/failing-parser:$PATH" expect_rc 1 "a failing decode cannot disappear behind valid empty JSON output" says "the captured parser failure is reported" "checkout refs could not be inspected" says "env pin extraction also preserves a failing decoder's status" "HYPATIA_SHA pin could not be inspected" -if [ -s "$EDGES_DECODE_LOG" ] && [ -z "$(sort "$EDGES_DECODE_LOG" | uniq -d)" ]; then +if [[ -s "$EDGES_DECODE_LOG" ]] && [[ -z "$(sort "$EDGES_DECODE_LOG" | uniq -d)" ]]; then ok "a failed decode is cached without retrying or publishing partial JSON" else bad "a failed workflow decode was absent or retried" From 74add588ac39eec7e0fd17959d27137af3baa972 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:24:10 +0000 Subject: [PATCH 5/9] Preserve intentionally floating pin expressions without newline loss --- scripts/check-workflow-edges.sh | 3 ++- tests/workflows/workflow_edges_test.sh | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/scripts/check-workflow-edges.sh b/scripts/check-workflow-edges.sh index 09c20c1..c0dd682 100755 --- a/scripts/check-workflow-edges.sh +++ b/scripts/check-workflow-edges.sh @@ -167,7 +167,8 @@ first(paths as $p | select($p[-1] == $var) | $root | getpath($p)) | if type == "object" or type == "array" then error("pin must be a scalar") else tostring | - if test("\\s") then error("pin must not contain whitespace") else . end + if test("[\\r\\n]") or (test("\\s") and (contains("${{") | not)) + then error("pin must not contain whitespace") else . end end ' "$json" } diff --git a/tests/workflows/workflow_edges_test.sh b/tests/workflows/workflow_edges_test.sh index 25b5a0f..66b2c94 100755 --- a/tests/workflows/workflow_edges_test.sh +++ b/tests/workflows/workflow_edges_test.sh @@ -343,6 +343,15 @@ expect_rc 0 "a chomped block scalar supplies the exact HYPATIA_SHA" sed -i 's/HYPATIA_SHA: |-/HYPATIA_SHA: |/' "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "a block scalar trailing newline is refused before shell substitution" says "the decoded whitespace rejection is explicit" "pin must not contain whitespace" +fresh >/dev/null; checkout_fixture "$HSHA" 'env: + HYPATIA_SHA: "${{ inputs.sha }}"' +expect_rc 0 "a floating env expression with spaces does not clash with a literal checkout" +sed -i "s/ref: $HSHA/ref: $HYPATIA_REF_EXPR/" "$WORK/repo/.github/workflows/oracle-pin-control.yml" +expect_rc 1 "an actual checkout cannot resolve a dynamic env expression as a full SHA" +fresh >/dev/null; checkout_fixture "$HSHA" 'env: + HYPATIA_SHA: | + ${{ inputs.sha }}' +expect_rc 1 "a floating expression cannot bypass decoded trailing-newline rejection" fresh >/dev/null; checkout_fixture "$HYPATIA_REF_EXPR" sed -i "/ runs-on:/i\ env:\n HYPATIA_SHA: $HSHA" "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 0 "an exact, uniquely declared binding in the same job resolves" From 5f2ac53367cb9fd28b5596f59460090511c728bd Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:45:38 +0000 Subject: [PATCH 6/9] Decode scanner pins consistently in the rule-order gate --- scripts/check-hypatia-rule-order.sh | 106 ++++++++++---- tests/workflows/hypatia_rule_order_test.sh | 157 +++++++++++++++++++-- 2 files changed, 223 insertions(+), 40 deletions(-) diff --git a/scripts/check-hypatia-rule-order.sh b/scripts/check-hypatia-rule-order.sh index 65011c2..4bef5e9 100755 --- a/scripts/check-hypatia-rule-order.sh +++ b/scripts/check-hypatia-rule-order.sh @@ -43,6 +43,19 @@ for f in "$POLICY" "$HYPATIA_WF" "$LANE" "$IGNORE"; do fi done +if ! command -v yq >/dev/null 2>&1 || ! command -v jq >/dev/null 2>&1; then + echo "check-hypatia-rule-order: setup requires Mike Farah yq >=4.53.3 (v4) and jq" >&2 + exit 2 +fi +yq_version="$(yq --version 2>&1)" || { + echo "check-hypatia-rule-order: cannot read the required yq version" >&2; exit 2; +} +if [[ ! "$yq_version" =~ mikefarah/yq/.*version\ v([0-9]+)\.([0-9]+)\.([0-9]+) ]] || + (( BASH_REMATCH[1] != 4 || BASH_REMATCH[2] < 53 || (BASH_REMATCH[2] == 53 && BASH_REMATCH[3] < 3) )); then + echo "check-hypatia-rule-order: setup requires Mike Farah yq >=4.53.3 (v4); got $yq_version" >&2 + exit 2 +fi + errors=0 fail() { echo "FAIL: $*" >&2 @@ -167,36 +180,73 @@ done < <(jq -r '.observed_modules[] | [.module, .layer] | @tsv' "$POLICY") # ── pins must agree with the files that use them ──────────────────────────── policy_hypatia="$(jq -r '.scanner.commit' "$POLICY")" -# Recognize only literal scalar keys in this workflow, not comments or a key -# that merely contains the pin name. Reject duplicate or malformed declarations -# before comparing to policy; the first matching line is not authoritative. -# This is the workflow-edge pin check, not a general YAML evaluator. -pin_value() { - local value="${1#*:}" - value="$(printf '%s\n' "$value" | sed -E 's/^[[:space:]]*//; s/[[:space:]]+#.*$//; s/[[:space:]]+$//')" - if [[ ${#value} -eq 42 ]]; then - if [[ ( ${value:0:1} == "'" && ${value: -1} == "'" ) || - ( ${value:0:1} == '"' && ${value: -1} == '"' ) ]]; then - value="${value:1:40}" +# The scanner pin is HYPATIA_SHA where hypatia-scan.yml runs the scan itself, +# or the `hypatia-ref:` input where it calls a reusable workflow (the call +# passes the pin; the reusable has none of its own). Both present must agree: +# two pins for one scanner leave the policy two answers. +# Inspect the raw AST before expanding aliases or converting to JSON. Decode +# this one workflow once for both pins; declarations and run text are not inputs. +workflow_pins() { + local workflow="$1" preflight duplicates keys sha_duplicates ref_duplicates + preflight="$(yq -p yaml -o yaml -r -N ' + ([.. | select(kind == "map") | to_entries | group_by(.key) | + .[] | select(length > 1)]) as $duplicates | + ([.. | select(kind == "map") | to_entries[] | .key | + select(kind != "scalar" or (tag != "!!str" and tag != "!!merge"))] | length) as $keys | + ([$duplicates[] | select(.[0].key == "HYPATIA_SHA")] | length) as $sha_duplicates | + ([$duplicates[] | select(.[0].key == "hypatia-ref")] | length) as $ref_duplicates | + [($duplicates | length), $keys, $sha_duplicates, $ref_duplicates] | @tsv + ' "$workflow")" || return 1 + while IFS=$'\t' read -r duplicates keys sha_duplicates ref_duplicates; do + [[ "$duplicates" =~ ^[0-9]+$ && "$keys" =~ ^[0-9]+$ && + "$sha_duplicates" =~ ^[0-9]+$ && "$ref_duplicates" =~ ^[0-9]+$ ]] || return 1 + if (( duplicates > 0 )); then + echo "duplicate YAML mapping keys (raw AST)" >&2 + if (( sha_duplicates > 0 )); then echo "hypatia-scan.yml declares HYPATIA_SHA more than once" >&2; fi + if (( ref_duplicates > 0 )); then echo "hypatia-scan.yml passes hypatia-ref more than once" >&2; fi + return 1 fi - fi - [[ "$value" =~ ^[0-9a-f]{40}$ ]] || return 1 - printf '%s' "$value" + if (( keys > 0 )); then + echo "aliased, complex or non-string YAML mapping keys are unsupported" >&2; return 1 + fi + done <<< "$preflight" + yq --yaml-fix-merge-anchor-to-spec=true -p yaml -o json -I0 'explode(.)' "$workflow" | + jq -sc ' +def pin($var; $scope): + . as $root | + [paths as $p | select($p[-1] == $var) | + select( + ($scope == "env" and ( + (($p | length) == 2 and $p[0] == "env") or + (($p | length) == 4 and $p[0] == "jobs" and $p[2] == "env") or + (($p | length) == 6 and $p[0] == "jobs" and $p[2] == "steps" and + ($p[3] | type) == "number" and $p[4] == "env"))) or + ($scope == "with" and ($p | length) == 4 and $p[0] == "jobs" and $p[2] == "with" and + ($root.jobs[$p[1]].uses | type) == "string" and + ($root.jobs[$p[1]].uses | test("^([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+/\\.github/workflows/[^@]+@|\\./\\.github/workflows/[^@]+$)")))) | + $root | getpath($p)] | + if length > 1 then + if $var == "HYPATIA_SHA" then error("hypatia-scan.yml declares HYPATIA_SHA more than once") + else error("hypatia-scan.yml passes hypatia-ref more than once") end + elif length == 0 then "" + else .[0] | + ("hypatia-scan.yml has a malformed " + $var + " scanner pin") as $malformed | + if type != "string" then error($malformed + ": must be a literal string") + elif test("\\s") then error($malformed + ": scanner pin must not contain whitespace") + elif (test("^[0-9a-f]{40}$") | not) then error($malformed + ": must be a literal full SHA") + else . end + end; +if length != 1 then error("expected exactly one YAML document") +elif (.[0] | type) != "object" or (.[0].jobs | type) != "object" then error("workflow and jobs must be mappings") +else .[0] | {sha:pin("HYPATIA_SHA"; "env"), ref:pin("hypatia-ref"; "with")} end + ' } - -mapfile -t sha_rows < <(sed -nE '/^[[:space:]]*HYPATIA_SHA[[:space:]]*:/p' "$HYPATIA_WF") -mapfile -t ref_rows < <(sed -nE '/^[[:space:]]*hypatia-ref[[:space:]]*:/p' "$HYPATIA_WF") -wf_sha=; wf_ref= -if (( ${#sha_rows[@]} > 1 )); then - fail "hypatia-scan.yml declares HYPATIA_SHA more than once" -elif (( ${#sha_rows[@]} == 1 )); then - wf_sha="$(pin_value "${sha_rows[0]}")" || fail "hypatia-scan.yml has a malformed HYPATIA_SHA scanner pin" -fi -if (( ${#ref_rows[@]} > 1 )); then - fail "hypatia-scan.yml passes hypatia-ref more than once" -elif (( ${#ref_rows[@]} == 1 )); then - wf_ref="$(pin_value "${ref_rows[0]}")" || fail "hypatia-scan.yml has a malformed hypatia-ref scanner pin" -fi +wf_pins="$(workflow_pins "$HYPATIA_WF")" || { + fail "hypatia-scan.yml scanner pins could not be inspected" + wf_pins='{"sha":"","ref":""}' +} +wf_sha="$(jq -r '.sha' <<< "$wf_pins")" +wf_ref="$(jq -r '.ref' <<< "$wf_pins")" wf_hypatia="${wf_sha:-$wf_ref}" wf_pin_name="HYPATIA_SHA"; [ -n "$wf_sha" ] || wf_pin_name="the hypatia-ref input" if [ -z "$wf_hypatia" ]; then diff --git a/tests/workflows/hypatia_rule_order_test.sh b/tests/workflows/hypatia_rule_order_test.sh index c7410d9..fcf7006 100755 --- a/tests/workflows/hypatia_rule_order_test.sh +++ b/tests/workflows/hypatia_rule_order_test.sh @@ -173,7 +173,7 @@ YML fresh; caller_wf "$(jq -r '.scanner.commit' "$WORK/repo/.machine_readable/policies/hypatia-rule-order.json")" expect_rc 0 "a reusable caller whose hypatia-ref matches the policy is accepted" -fresh; caller_wf 4444444444444444444444444444444444444444 +fresh; caller_wf '"4444444444444444444444444444444444444444"' expect_rc 1 "a reusable caller whose hypatia-ref disagrees with the policy is refused" says "the caller-pin failure names the input" "hypatia-ref input in hypatia-scan.yml" @@ -182,11 +182,96 @@ expect_rc 1 "a reusable caller that passes no hypatia-ref cannot validate the po says "the missing caller pin is stated" "passes no hypatia-ref input" # Two pins for one scanner that disagree are refused even when one matches. -fresh -printf ' hypatia-ref: 5555555555555555555555555555555555555555\n' >> "$WORK/repo/.github/workflows/hypatia-scan.yml" +fresh; caller_wf '"5555555555555555555555555555555555555555"' +scanner_pin="$(jq -r '.scanner.commit' "$REPO_DIR/.machine_readable/policies/hypatia-rule-order.json")" +sed -i "/^jobs:/i\env:\n HYPATIA_SHA: \"$scanner_pin\"" "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 1 "HYPATIA_SHA and a disagreeing hypatia-ref in one workflow are refused" says "the double-pin failure names both" "pins the scanner twice" +# Minimal valid direct-scanner workflow for structural pin controls. +env_wf() { + local pin="$1" + cat > "$WORK/repo/.github/workflows/hypatia-scan.yml" <> "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 1 "duplicate YAML mapping keys cannot hide a scanner pin" +says "the scanner raw AST preflight remains visible" "duplicate YAML mapping keys" + +fresh; env_wf '*undefined' +expect_rc 1 "an undefined scanner scalar alias fails closed" +fresh; env_wf "$scanner_pin" +printf '\n---\n{}\n' >> "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 1 "multiple scanner workflow documents fail closed" +fresh; env_wf main; edit_policy '.scanner.commit = "main"' +expect_rc 1 "a floating policy and workflow cannot agree without a full SHA" + +fresh +mkdir -p "$WORK/failing-parser" +cat > "$WORK/failing-parser/yq" <<'SH' +#!/usr/bin/env bash +case "$*" in + *--version*) echo 'yq (https://github.com/mikefarah/yq/) version v4.53.3' ;; + *'explode(.)'*) printf '{"jobs":{}}\n'; exit 17 ;; + *) printf '0\t0\t0\t0\n' ;; +esac +SH +chmod +x "$WORK/failing-parser/yq" +PATH="$WORK/failing-parser:$PATH" expect_rc 1 "a scanner decoder failure cannot publish partial JSON" +says "the scanner decoder failure is reported" "scanner pins could not be inspected" # Malformed refs must not masquerade as immutable scanner pins. for invalid in main be98f74ce93d 4444444444444444444444444444444444444444-extra; do @@ -197,21 +282,21 @@ done # A second declaration of the same kind is ambiguous even when the values # agree: YAML consumers can disagree about duplicate-key handling. fresh -printf ' HYPATIA_SHA: %s\n' "$(jq -r '.scanner.commit' "$WORK/repo/.machine_readable/policies/hypatia-rule-order.json")" >> "$WORK/repo/.github/workflows/hypatia-scan.yml" +printf ' HYPATIA_SHA: %s\n' "$scanner_pin" >> "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 1 "duplicate identical HYPATIA_SHA declarations are refused" says "the duplicate SHA diagnostic names the key" "declares HYPATIA_SHA more than once" fresh -printf ' HYPATIA_SHA: 4444444444444444444444444444444444444444\n' >> "$WORK/repo/.github/workflows/hypatia-scan.yml" +printf ' HYPATIA_SHA: "4444444444444444444444444444444444444444"\n' >> "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 1 "conflicting HYPATIA_SHA declarations are refused" -fresh; caller_wf "$(jq -r '.scanner.commit' "$WORK/repo/.machine_readable/policies/hypatia-rule-order.json")" -printf ' hypatia-ref: %s\n' "$(jq -r '.scanner.commit' "$WORK/repo/.machine_readable/policies/hypatia-rule-order.json")" >> "$WORK/repo/.github/workflows/hypatia-scan.yml" +fresh; caller_wf "$scanner_pin" +printf ' hypatia-ref: %s\n' "$scanner_pin" >> "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 1 "duplicate identical hypatia-ref declarations are refused" says "the duplicate input diagnostic names the key" "passes hypatia-ref more than once" -fresh; caller_wf "$(jq -r '.scanner.commit' "$WORK/repo/.machine_readable/policies/hypatia-rule-order.json")" -printf ' hypatia-ref: 4444444444444444444444444444444444444444\n' >> "$WORK/repo/.github/workflows/hypatia-scan.yml" +fresh; caller_wf "$scanner_pin" +printf ' hypatia-ref: "4444444444444444444444444444444444444444"\n' >> "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 1 "conflicting hypatia-ref declarations are refused" # Comments and similarly named keys cannot stand in for a scanner pin. @@ -230,15 +315,63 @@ expect_rc 1 "a HYPATIA_SHA with a suffix is refused" says "the malformed SHA diagnostic names the key" "malformed HYPATIA_SHA scanner pin" # A valid SHA cannot mask a malformed declaration of the other pin kind. -fresh -printf ' hypatia-ref: main\n' >> "$WORK/repo/.github/workflows/hypatia-scan.yml" +fresh; caller_wf main +sed -i "/^jobs:/i\env:\n HYPATIA_SHA: \"$scanner_pin\"" "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 1 "a malformed hypatia-ref beside valid HYPATIA_SHA is refused" says "the malformed input diagnostic names the key" "malformed hypatia-ref scanner pin" # Quoted scalar pins with trailing YAML comments remain valid. -fresh; caller_wf "'$(jq -r '.scanner.commit' "$WORK/repo/.machine_readable/policies/hypatia-rule-order.json")' # scanner revision" +fresh; caller_wf "'$scanner_pin' # scanner revision" expect_rc 0 "a quoted hypatia-ref with a comment is accepted" +# Distinct real scopes are ambiguous too, even without duplicate YAML keys. +fresh; env_wf "$scanner_pin" +sed -i "/ runs-on:/i\ env:\n HYPATIA_SHA: \"$scanner_pin\"" "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 1 "identical scanner SHA bindings in different scopes are refused" +says "multiple scopes preserve the duplicate SHA diagnostic" "declares HYPATIA_SHA more than once" + +fresh; caller_wf "$scanner_pin" +cat >> "$WORK/repo/.github/workflows/hypatia-scan.yml" <"$WORK/out" 2>&1 [ $? -eq 2 ] && ok "a missing repository directory is a setup error" || bad "missing repo must exit 2" From f0da3458f3f385af49aab044c87caa7ea251e942 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:56:20 +0000 Subject: [PATCH 7/9] Recognize job-container scanner pin bindings --- scripts/check-hypatia-rule-order.sh | 3 ++- scripts/check-workflow-edges.sh | 8 ++++++-- tests/workflows/hypatia_rule_order_test.sh | 13 +++++++++++++ tests/workflows/workflow_edges_test.sh | 21 +++++++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/scripts/check-hypatia-rule-order.sh b/scripts/check-hypatia-rule-order.sh index 4bef5e9..60b9e4f 100755 --- a/scripts/check-hypatia-rule-order.sh +++ b/scripts/check-hypatia-rule-order.sh @@ -219,11 +219,12 @@ def pin($var; $scope): ($scope == "env" and ( (($p | length) == 2 and $p[0] == "env") or (($p | length) == 4 and $p[0] == "jobs" and $p[2] == "env") or + (($p | length) == 5 and $p[0] == "jobs" and $p[2] == "container" and $p[3] == "env") or (($p | length) == 6 and $p[0] == "jobs" and $p[2] == "steps" and ($p[3] | type) == "number" and $p[4] == "env"))) or ($scope == "with" and ($p | length) == 4 and $p[0] == "jobs" and $p[2] == "with" and ($root.jobs[$p[1]].uses | type) == "string" and - ($root.jobs[$p[1]].uses | test("^([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+/\\.github/workflows/[^@]+@|\\./\\.github/workflows/[^@]+$)")))) | + ($root.jobs[$p[1]].uses | test("^([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+/\\.github/workflows/[^@]+@|[.$]/\\.github/workflows/[^@]+$)")))) | $root | getpath($p)] | if length > 1 then if $var == "HYPATIA_SHA" then error("hypatia-scan.yml declares HYPATIA_SHA more than once") diff --git a/scripts/check-workflow-edges.sh b/scripts/check-workflow-edges.sh index c0dd682..62fbb86 100755 --- a/scripts/check-workflow-edges.sh +++ b/scripts/check-workflow-edges.sh @@ -159,6 +159,7 @@ first(paths as $p | select($p[-1] == $var) | select( (($p | length) == 2 and $p[0] == "env") or (($p | length) == 4 and $p[0] == "jobs" and $p[2] == "env") or + (($p | length) == 5 and $p[0] == "jobs" and $p[2] == "container" and $p[3] == "env") or (($p | length) == 6 and $p[0] == "jobs" and $p[2] == "steps" and ($p[3] | type) == "number" and $p[4] == "env") or (($p | length) == 4 and $p[0] == "jobs" and $p[2] == "with" and @@ -175,7 +176,7 @@ end # hypatia_checkouts FILE — inspect cached real YAML nodes, never indentation. # jq checks typed jobs/steps/inputs and literal env bindings with step > job > -# workflow priority. An uninspectable override never permits falling back. +# workflow > job-container priority. An uninspectable override never falls back. hypatia_checkouts() { local workflow="$1" json status=0 json="$(workflow_json "$workflow")" || status=$? @@ -215,7 +216,10 @@ def inspect($root; $job; $entry): else ($inputs.ref | text) as $raw | (if ($raw | test("^\\$\\{\\{\\s*env\\.[A-Za-z_][A-Za-z0-9_]*\\s*\\}\\}$")) then ($raw | capture("^\\$\\{\\{\\s*env\\.(?[A-Za-z_][A-Za-z0-9_]*)\\s*\\}\\}$").name) as $name | - lookup([$step,$job.value,$root]; $name) + lookup([$step,$job.value,$root] + + (if ($job.value.container | type) == "object" then + if ($job.value.container | has("env")) then [$job.value.container] else [] end + else [] end); $name) else {present:true,value:$raw} end) as $resolved | if ($resolved | has("error")) then bad($where; $resolved.error) elif ($resolved.present | not) then bad($where; "missing env binding for checkout ref") diff --git a/tests/workflows/hypatia_rule_order_test.sh b/tests/workflows/hypatia_rule_order_test.sh index fcf7006..0c79d94 100755 --- a/tests/workflows/hypatia_rule_order_test.sh +++ b/tests/workflows/hypatia_rule_order_test.sh @@ -370,6 +370,19 @@ sed -i 's|uses: .*|uses: ./.github/workflows/hypatia-scan-reusable.yml|' "$WORK/ expect_rc 0 "a local reusable caller scanner input matches the policy" sed -i "s/$scanner_pin/$WRONG_SCANNER_PIN/" "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 1 "a local reusable caller with a wrong scanner input is refused" +fresh; caller_wf "$scanner_pin" +sed -i 's|uses: .*|uses: $/.github/workflows/hypatia-scan-reusable.yml|' "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 0 "a dollar-prefixed local reusable caller scanner input matches the policy" + +fresh; env_wf "$scanner_pin" +sed -i '/^env:/,/^jobs:/{ /^env:/d; /^ HYPATIA_SHA:/d; }' "$WORK/repo/.github/workflows/hypatia-scan.yml" +sed -i "/ runs-on:/i\ container:\n image: alpine:3.23\n env:\n HYPATIA_SHA: \"$scanner_pin\"" "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 0 "a job-container scanner env pin matches the policy" +sed -i "s/$scanner_pin/$WRONG_SCANNER_PIN/" "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 1 "a wrong job-container scanner env pin is refused" +sed -i -e "s/$WRONG_SCANNER_PIN/$scanner_pin/" -e "/^jobs:/i\env:\n HYPATIA_SHA: \"$scanner_pin\"" "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 1 "identical job-container and workflow scanner pins remain ambiguous" +says "container bindings preserve the duplicate scanner-pin diagnostic" "declares HYPATIA_SHA more than once" echo "=== 7. Setup errors ===" diff --git a/tests/workflows/workflow_edges_test.sh b/tests/workflows/workflow_edges_test.sh index 66b2c94..778864a 100755 --- a/tests/workflows/workflow_edges_test.sh +++ b/tests/workflows/workflow_edges_test.sh @@ -254,6 +254,7 @@ unset EDGES_REUSABLE_DIR echo "=== 12. Hypatia checkout refs participate in the one-tool pin contract ===" readonly PIN_REF='${{ env.PIN }}' readonly HYPATIA_REF_EXPR='${{ env.HYPATIA_SHA }}' +readonly WRONG_CONTAINER_PIN=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa checkout_fixture() { local ref="$1" binding="${2:-}" cat > "$WORK/repo/.github/workflows/oracle-pin-control.yml" </dev/null; checkout_fixture "$HSHA" expect_rc 0 "a matching literal Hypatia checkout ref is accepted" +fresh >/dev/null +sed -i -e 's/^ env:$/ container:\n image: alpine:3.23\n env:/' -e 's/^ HYPATIA_SHA:/ HYPATIA_SHA:/' "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 0 "the named scanner producer may declare its pin in job-container env" +sed -i "s/$HSHA/$WRONG_CONTAINER_PIN/" "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 1 "a wrong job-container scanner producer pin disagrees with the verdict" +says "the container producer mismatch names its scanner pin" "HYPATIA_SHA in hypatia-scan.yml" + +fresh >/dev/null; checkout_fixture "$PIN_REF" +sed -i "/ runs-on:/i\ container:\n image: alpine:3.23\n env:\n PIN: \"$HSHA\"" "$WORK/repo/.github/workflows/oracle-pin-control.yml" +expect_rc 0 "a typed checkout resolves the job-container env fallback" +sed -i "s/$HSHA/$WRONG_CONTAINER_PIN/" "$WORK/repo/.github/workflows/oracle-pin-control.yml" +expect_rc 1 "a wrong job-container checkout env pin is refused" +sed -i "/^jobs:/i\env:\n PIN: \"$HSHA\"" "$WORK/repo/.github/workflows/oracle-pin-control.yml" +expect_rc 0 "workflow env overrides the job-container checkout fallback" +sed -i -e "s/^ PIN: \"$HSHA\"/ PIN: \"$WRONG_CONTAINER_PIN\"/" -e "s/^ PIN: \"$WRONG_CONTAINER_PIN\"/ PIN: \"$HSHA\"/" "$WORK/repo/.github/workflows/oracle-pin-control.yml" +expect_rc 1 "a wrong workflow env binding cannot fall back to a correct container pin" +fresh >/dev/null; checkout_fixture "$PIN_REF" +sed -i "/ runs-on:/i\ services:\n database:\n image: alpine:3.23\n env:\n PIN: \"$HSHA\"" "$WORK/repo/.github/workflows/oracle-pin-control.yml" +expect_rc 1 "a checkout cannot borrow a service-container env binding" + fresh >/dev/null; checkout_fixture "$HSHA" sed -i -e 's/repository:/Repository:/' -e 's/ ref:/ Ref:/' "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 0 "mixed-case checkout input names preserve a matching pin" From a27096268ec123755eb15e6e26aa33d4118c1e1a Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:00:14 +0000 Subject: [PATCH 8/9] Keep checkout expressions separate from container OS environment --- scripts/check-workflow-edges.sh | 8 +++----- tests/workflows/workflow_edges_test.sh | 9 +++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/scripts/check-workflow-edges.sh b/scripts/check-workflow-edges.sh index 62fbb86..2441732 100755 --- a/scripts/check-workflow-edges.sh +++ b/scripts/check-workflow-edges.sh @@ -176,7 +176,8 @@ end # hypatia_checkouts FILE — inspect cached real YAML nodes, never indentation. # jq checks typed jobs/steps/inputs and literal env bindings with step > job > -# workflow > job-container priority. An uninspectable override never falls back. +# workflow priority. Container OS env is outside the expression env context. +# An uninspectable override never permits falling back. hypatia_checkouts() { local workflow="$1" json status=0 json="$(workflow_json "$workflow")" || status=$? @@ -216,10 +217,7 @@ def inspect($root; $job; $entry): else ($inputs.ref | text) as $raw | (if ($raw | test("^\\$\\{\\{\\s*env\\.[A-Za-z_][A-Za-z0-9_]*\\s*\\}\\}$")) then ($raw | capture("^\\$\\{\\{\\s*env\\.(?[A-Za-z_][A-Za-z0-9_]*)\\s*\\}\\}$").name) as $name | - lookup([$step,$job.value,$root] + - (if ($job.value.container | type) == "object" then - if ($job.value.container | has("env")) then [$job.value.container] else [] end - else [] end); $name) + lookup([$step,$job.value,$root]; $name) else {present:true,value:$raw} end) as $resolved | if ($resolved | has("error")) then bad($where; $resolved.error) elif ($resolved.present | not) then bad($where; "missing env binding for checkout ref") diff --git a/tests/workflows/workflow_edges_test.sh b/tests/workflows/workflow_edges_test.sh index 778864a..3f278ef 100755 --- a/tests/workflows/workflow_edges_test.sh +++ b/tests/workflows/workflow_edges_test.sh @@ -281,13 +281,14 @@ says "the container producer mismatch names its scanner pin" "HYPATIA_SHA in hyp fresh >/dev/null; checkout_fixture "$PIN_REF" sed -i "/ runs-on:/i\ container:\n image: alpine:3.23\n env:\n PIN: \"$HSHA\"" "$WORK/repo/.github/workflows/oracle-pin-control.yml" -expect_rc 0 "a typed checkout resolves the job-container env fallback" +expect_rc 1 "a checkout expression cannot resolve a job-container OS env binding" +says "a container-only checkout expression identifies its missing context binding" "missing env binding for checkout ref" sed -i "s/$HSHA/$WRONG_CONTAINER_PIN/" "$WORK/repo/.github/workflows/oracle-pin-control.yml" -expect_rc 1 "a wrong job-container checkout env pin is refused" +expect_rc 1 "a wrong container-only binding cannot resolve a checkout expression" sed -i "/^jobs:/i\env:\n PIN: \"$HSHA\"" "$WORK/repo/.github/workflows/oracle-pin-control.yml" -expect_rc 0 "workflow env overrides the job-container checkout fallback" +expect_rc 0 "workflow env resolves a checkout expression independently of container OS env" sed -i -e "s/^ PIN: \"$HSHA\"/ PIN: \"$WRONG_CONTAINER_PIN\"/" -e "s/^ PIN: \"$WRONG_CONTAINER_PIN\"/ PIN: \"$HSHA\"/" "$WORK/repo/.github/workflows/oracle-pin-control.yml" -expect_rc 1 "a wrong workflow env binding cannot fall back to a correct container pin" +expect_rc 1 "a wrong workflow env binding cannot borrow a correct container OS pin" fresh >/dev/null; checkout_fixture "$PIN_REF" sed -i "/ runs-on:/i\ services:\n database:\n image: alpine:3.23\n env:\n PIN: \"$HSHA\"" "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "a checkout cannot borrow a service-container env binding" From ee25987d21978ad4aa5353c505041007b3f4faa5 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:17:34 +0000 Subject: [PATCH 9/9] Validate all scanner bindings and preserve source type evidence --- scripts/check-hypatia-rule-order.sh | 64 ++++++---- scripts/check-workflow-edges.sh | 104 +++++++++------- tests/workflows/hypatia_rule_order_test.sh | 61 ++++++++-- tests/workflows/workflow_edges_test.sh | 134 ++++++++++++++++++--- 4 files changed, 269 insertions(+), 94 deletions(-) diff --git a/scripts/check-hypatia-rule-order.sh b/scripts/check-hypatia-rule-order.sh index 60b9e4f..326c321 100755 --- a/scripts/check-hypatia-rule-order.sh +++ b/scripts/check-hypatia-rule-order.sh @@ -186,8 +186,12 @@ policy_hypatia="$(jq -r '.scanner.commit' "$POLICY")" # two pins for one scanner leave the policy two answers. # Inspect the raw AST before expanding aliases or converting to JSON. Decode # this one workflow once for both pins; declarations and run text are not inputs. -workflow_pins() { - local workflow="$1" preflight duplicates keys sha_duplicates ref_duplicates +workflow_pins() ( + local workflow="$1" snapshot_dir preflight duplicates keys sha_duplicates ref_duplicates + snapshot_dir="$(mktemp -d)" || return 1 + trap 'rm -rf "$snapshot_dir"' EXIT + cp -- "$workflow" "$snapshot_dir/workflow.yml" || return 1 + workflow="$snapshot_dir/workflow.yml" preflight="$(yq -p yaml -o yaml -r -N ' ([.. | select(kind == "map") | to_entries | group_by(.key) | .[] | select(length > 1)]) as $duplicates | @@ -210,9 +214,16 @@ workflow_pins() { echo "aliased, complex or non-string YAML mapping keys are unsupported" >&2; return 1 fi done <<< "$preflight" - yq --yaml-fix-merge-anchor-to-spec=true -p yaml -o json -I0 'explode(.)' "$workflow" | + # Preserve resolved YAML tags before JSON conversion erases them. The jq + # scope selector below decides which matching keys are actual pin bindings. + yq --yaml-fix-merge-anchor-to-spec=true -p yaml -o json -I0 ' + explode(.) | {"workflow": .} | + .pin_tags = [.workflow | .. | + select((path | .[-1]) == "HYPATIA_SHA" or (path | .[-1]) == "hypatia-ref") | + {"path": (path | .[1:]), "tag": tag}] + ' "$workflow" | jq -sc ' -def pin($var; $scope): +def pin($tags; $var; $scope): . as $root | [paths as $p | select($p[-1] == $var) | select( @@ -225,37 +236,40 @@ def pin($var; $scope): ($scope == "with" and ($p | length) == 4 and $p[0] == "jobs" and $p[2] == "with" and ($root.jobs[$p[1]].uses | type) == "string" and ($root.jobs[$p[1]].uses | test("^([A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+/\\.github/workflows/[^@]+@|[.$]/\\.github/workflows/[^@]+$)")))) | - $root | getpath($p)] | + {value: ($root | getpath($p)), tag: ([$tags[] | select(.path == $p) | .tag] | .[0])}] | if length > 1 then if $var == "HYPATIA_SHA" then error("hypatia-scan.yml declares HYPATIA_SHA more than once") else error("hypatia-scan.yml passes hypatia-ref more than once") end elif length == 0 then "" else .[0] | ("hypatia-scan.yml has a malformed " + $var + " scanner pin") as $malformed | - if type != "string" then error($malformed + ": must be a literal string") - elif test("\\s") then error($malformed + ": scanner pin must not contain whitespace") - elif (test("^[0-9a-f]{40}$") | not) then error($malformed + ": must be a literal full SHA") - else . end + if .tag != "!!str" then error($malformed + ": scanner pin must have YAML tag !!str") + elif (.value | type) != "string" then error($malformed + ": must be a literal string") + elif (.value | test("\\s")) then error($malformed + ": scanner pin must not contain whitespace") + elif (.value | test("^[0-9a-f]{40}$") | not) then error($malformed + ": must be a literal full SHA") + else .value end end; if length != 1 then error("expected exactly one YAML document") -elif (.[0] | type) != "object" or (.[0].jobs | type) != "object" then error("workflow and jobs must be mappings") -else .[0] | {sha:pin("HYPATIA_SHA"; "env"), ref:pin("hypatia-ref"; "with")} end +elif (.[0] | type) != "object" or (.[0].pin_tags | type) != "array" then error("invalid decoded workflow metadata") +elif (.[0].workflow | type) != "object" or (.[0].workflow.jobs | type) != "object" then error("workflow and jobs must be mappings") +else .[0] as $decoded | $decoded.workflow | + {sha:pin($decoded.pin_tags; "HYPATIA_SHA"; "env"), ref:pin($decoded.pin_tags; "hypatia-ref"; "with")} end ' -} -wf_pins="$(workflow_pins "$HYPATIA_WF")" || { +) +if wf_pins="$(workflow_pins "$HYPATIA_WF")"; then + wf_sha="$(jq -r '.sha' <<< "$wf_pins")" + wf_ref="$(jq -r '.ref' <<< "$wf_pins")" + wf_hypatia="${wf_sha:-$wf_ref}" + wf_pin_name="HYPATIA_SHA"; [ -n "$wf_sha" ] || wf_pin_name="the hypatia-ref input" + if [ -z "$wf_hypatia" ]; then + fail "hypatia-scan.yml declares no HYPATIA_SHA and passes no hypatia-ref input; the policy's scanner.commit cannot be verified" + elif [ -n "$wf_sha" ] && [ -n "$wf_ref" ] && [ "$wf_sha" != "$wf_ref" ]; then + fail "hypatia-scan.yml pins the scanner twice and disagrees: HYPATIA_SHA ${wf_sha:0:12}, hypatia-ref ${wf_ref:0:12}" + elif [ "$policy_hypatia" != "$wf_hypatia" ]; then + fail "policy scanner.commit (${policy_hypatia:0:12}) does not match ${wf_pin_name} in hypatia-scan.yml (${wf_hypatia:0:12})" + fi +else fail "hypatia-scan.yml scanner pins could not be inspected" - wf_pins='{"sha":"","ref":""}' -} -wf_sha="$(jq -r '.sha' <<< "$wf_pins")" -wf_ref="$(jq -r '.ref' <<< "$wf_pins")" -wf_hypatia="${wf_sha:-$wf_ref}" -wf_pin_name="HYPATIA_SHA"; [ -n "$wf_sha" ] || wf_pin_name="the hypatia-ref input" -if [ -z "$wf_hypatia" ]; then - fail "hypatia-scan.yml declares no HYPATIA_SHA and passes no hypatia-ref input; the policy's scanner.commit cannot be verified" -elif [ -n "$wf_sha" ] && [ -n "$wf_ref" ] && [ "$wf_sha" != "$wf_ref" ]; then - fail "hypatia-scan.yml pins the scanner twice and disagrees: HYPATIA_SHA ${wf_sha:0:12}, hypatia-ref ${wf_ref:0:12}" -elif [ "$policy_hypatia" != "$wf_hypatia" ]; then - fail "policy scanner.commit (${policy_hypatia:0:12}) does not match ${wf_pin_name} in hypatia-scan.yml (${wf_hypatia:0:12})" fi policy_grid="$(jq -r '.grid.commit' "$POLICY")" diff --git a/scripts/check-workflow-edges.sh b/scripts/check-workflow-edges.sh index 2441732..ada5659 100755 --- a/scripts/check-workflow-edges.sh +++ b/scripts/check-workflow-edges.sh @@ -123,13 +123,15 @@ workflow_json() { if [[ -f "$cache.failed" ]]; then return 1; fi mkdir -p "$(dirname "$cache")" || return 1 : > "$cache.failed" || return 1 + # Raw validation and alias expansion must see the same source bytes. + cp -- "$workflow" "$cache.yaml" || return 1 preflight="$(yq -p yaml -o yaml -r -N ' ([.. | select(kind == "map") | to_entries | group_by(.key) | .[] | select(length > 1)] | length) as $duplicates | ([.. | select(kind == "map") | to_entries[] | .key | select(kind != "scalar" or (tag != "!!str" and tag != "!!merge"))] | length) as $keys | [$duplicates, $keys] | @tsv - ' "$workflow")" || return 1 + ' "$cache.yaml")" || return 1 while IFS=$'\t' read -r duplicates keys; do [[ "$duplicates" =~ ^[0-9]+$ && "$keys" =~ ^[0-9]+$ ]] || return 1 if (( duplicates > 0 )); then @@ -141,21 +143,28 @@ workflow_json() { cat "$cache.rejected"; return 3 fi done <<< "$preflight" - yq --yaml-fix-merge-anchor-to-spec=true -p yaml -o json -I0 'explode(.)' "$workflow" > "$cache.tmp" || return 1 - jq -se 'length == 1 and (.[0] | type) == "object"' "$cache.tmp" >/dev/null || return 1 + # Wrap the expanded tree first so yq rebinds paths of nodes copied by merges. + # JSON can represent a tagged YAML scalar as an ordinary string. + yq --yaml-fix-merge-anchor-to-spec=true -p yaml -o json -I0 ' + explode(.) | {"workflow": .} | + .non_string = [.workflow | .. | select(kind == "scalar" and tag != "!!str") | path | .[1:]] + ' "$cache.yaml" > "$cache.tmp" || return 1 + jq -se 'length == 1 and (.[0] | type == "object" and + (.workflow | type) == "object" and (.non_string | type) == "array")' "$cache.tmp" >/dev/null || return 1 mv "$cache.tmp" "$cache.json" || return 1 printf '%s' "$cache.json" } -# env_pin FILE VAR — the first actual env binding or reusable-call input. +# env_pin FILE VAR [census] — consensus among actual env bindings and call inputs. # Input declarations and matching keys inside unrelated data are not bindings. # Validate before command substitution can strip a block scalar's final newline. env_pin() { - local workflow="$1" var="$2" json + local workflow="$1" var="$2" mode="${3:-identity}" json json="$(workflow_json "$workflow")" || return 1 - jq -r --arg var "$var" ' -. as $root | -first(paths as $p | select($p[-1] == $var) | + jq -r --arg var "$var" --arg mode "$mode" ' +def floating: contains("${{") or . == "latest" or . == "HEAD"; +.workflow as $root | .non_string as $non_string | +[$root | paths as $p | select($p[-1] == $var) | select( (($p | length) == 2 and $p[0] == "env") or (($p | length) == 4 and $p[0] == "jobs" and $p[2] == "env") or @@ -165,12 +174,19 @@ first(paths as $p | select($p[-1] == $var) | (($p | length) == 4 and $p[0] == "jobs" and $p[2] == "with" and ($root.jobs[$p[1]].uses | type) == "string" and ($root.jobs[$p[1]].uses | test("^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+/\\.github/workflows/[^@]+@")))) | - $root | getpath($p)) | -if type == "object" or type == "array" then error("pin must be a scalar") -else tostring | - if test("[\\r\\n]") or (test("\\s") and (contains("${{") | not)) - then error("pin must not contain whitespace") else . end -end + getpath($p) | + if type != "string" or any($non_string[]; . == $p) or length == 0 then + error("pin must be a nonempty YAML string") + elif $p[2] == "with" and any($non_string[]; . == ["jobs", $p[1], "uses"]) then + error("reusable workflow selector must be a YAML string") + elif contains("\u0000") or test("[\\r\\n]") or (test("\\s") and (contains("${{") | not)) then + error("pin must not contain whitespace or NUL") + else . end] | unique as $bindings | +[$bindings[] | select(floating | not)] as $literals | +if ($literals | length) > 1 then error("conflicting literal pins") +elif $mode == "census" then $literals[] +elif ($bindings | length) > 1 then error("producer pin bindings do not agree") +else $bindings[] end ' "$json" } @@ -184,23 +200,29 @@ hypatia_checkouts() { if [[ "$status" -eq 3 ]]; then printf '%s\n' "$json"; return 0; fi [[ "$status" -eq 0 ]] || return 1 jq -sr ' +.[0].non_string as $non_string | def text: gsub("^\\s+|\\s+$"; ""); def row($pin; $where): [$pin, $where] | @tsv; def bad($where; $why): row("!"; $where + ": " + $why); -def binding($scope; $name): +def yaml_string($path): type == "string" and (any($non_string[]; . == $path) | not); +def binding($root; $path; $name): + ($root | getpath($path)) as $scope | if ($scope | has("env") | not) then {present:false} elif ($scope.env | type) != "object" then {error:"uninspectable env mapping for checkout env binding"} - elif ($scope.env | has($name)) then {present:true,value:$scope.env[$name]} + elif ($scope.env | has($name)) then + if ($scope.env[$name] | yaml_string($path + ["env", $name])) then {present:true,value:$scope.env[$name]} + else {error:"checkout env binding is not a literal string"} end else {present:false} end; -def lookup($scopes; $name): - reduce $scopes[] as $scope ({present:false}; - if .present or has("error") then . else binding($scope; $name) end); +def lookup($root; $paths; $name): + reduce $paths[] as $path ({present:false}; + if .present or has("error") then . else binding($root; $path; $name) end); def inspect($root; $job; $entry): ("job " + ($job.key | tojson) + " step " + (($entry.key + 1) | tostring)) as $where | + ["jobs", $job.key] as $job_path | ($job_path + ["steps", $entry.key]) as $step_path | $entry.value as $step | if ($step | type) != "object" then bad($where; "step must be a mapping") elif ($step | has("uses") | not) then empty - elif ($step.uses | type) != "string" then bad($where; "uninspectable uses selector") + elif ($step.uses | yaml_string($step_path + ["uses"]) | not) then bad($where; "uninspectable uses selector") elif ($step.uses | text | contains("${{")) then bad($where; "uninspectable uses selector expression") elif ($step.uses | text | ascii_downcase | startswith("actions/checkout@") | not) then empty elif ($step | has("with") | not) then empty @@ -208,28 +230,28 @@ def inspect($root; $job; $entry): elif ($step.with | to_entries | group_by(.key | ascii_downcase) | any(length > 1)) then bad($where; "checkout input names collide case-insensitively") else - ($step.with | with_entries(.key |= ascii_downcase)) as $inputs | + ($step.with | to_entries | map({key: (.key | ascii_downcase), value: .}) | from_entries) as $inputs | if ($inputs | has("repository") | not) then empty - elif ($inputs.repository | type) != "string" then bad($where; "uninspectable checkout repository selector") - elif ($inputs.repository | text | contains("${{")) then bad($where; "uninspectable checkout repository expression") - elif ($inputs.repository | text | ascii_downcase) != "hyperpolymath/hypatia" then empty - elif ($inputs.ref | type) != "string" then bad($where; "missing or non-literal full-SHA checkout ref") - else ($inputs.ref | text) as $raw | + elif ($inputs.repository.value | yaml_string($step_path + ["with", $inputs.repository.key]) | not) then + bad($where; "uninspectable checkout repository selector") + elif ($inputs.repository.value | text | contains("${{")) then bad($where; "uninspectable checkout repository expression") + elif ($inputs.repository.value | text | ascii_downcase) != "hyperpolymath/hypatia" then empty + elif ($inputs.ref.value | yaml_string($step_path + ["with", $inputs.ref.key]) | not) then + bad($where; "missing or non-literal full-SHA checkout ref") + else ($inputs.ref.value | text) as $raw | (if ($raw | test("^\\$\\{\\{\\s*env\\.[A-Za-z_][A-Za-z0-9_]*\\s*\\}\\}$")) then ($raw | capture("^\\$\\{\\{\\s*env\\.(?[A-Za-z_][A-Za-z0-9_]*)\\s*\\}\\}$").name) as $name | - lookup([$step,$job.value,$root]; $name) + lookup($root; [$step_path, $job_path, []]; $name) else {present:true,value:$raw} end) as $resolved | if ($resolved | has("error")) then bad($where; $resolved.error) elif ($resolved.present | not) then bad($where; "missing env binding for checkout ref") - elif ($resolved.value | type) != "string" then bad($where; "checkout env binding is not a literal string") elif ($resolved.value | text | test("^[0-9a-f]{40}$") | not) then bad($where; "missing or non-literal full-SHA checkout ref (or unresolved env binding)") else row(($resolved.value | text); $where) end end end; -if length != 1 then bad("workflow"; "expected exactly one YAML document") -elif (.[0] | type) != "object" then bad("workflow"; "root must be a mapping") -elif (.[0].jobs | type) != "object" then bad("workflow"; "jobs must be a mapping") -else .[0] as $root | $root.jobs | to_entries[] as $job | +.[0].workflow as $root | +if ($root.jobs | type) != "object" then bad("workflow"; "jobs must be a mapping") +else $root.jobs | to_entries[] as $job | if ($job.value | type) != "object" then bad("workflow"; "job must be a mapping") elif ($job.value | has("steps") | not) then empty elif ($job.value.steps | type) != "array" then bad("workflow"; "job steps must be a sequence") @@ -412,20 +434,17 @@ done # ── 4. no tool pinned at two commits in this repository ──────────────────── # panic-attack is pinned once for its scan and once for patch-bridge triage; the -# pair must agree, which is why both use env.PANIC_ATTACK_SHA. A literal second -# pin in another workflow is the clash this catches. +# pair must agree, which is why both use env.PANIC_ATTACK_SHA. The census +# excludes intentionally floating pins and checks the remaining literals. pins_file="$reusable_cache/pins" : > "$pins_file" for wf in "$WORKFLOWS_DIR"/*.yml "$WORKFLOWS_DIR"/*.yaml; do [ -e "$wf" ] || continue for var in HYPATIA_SHA HYPATIA_REF PANIC_ATTACK_SHA PONS_SHA; do - v="$(env_pin "$wf" "$var")" || { + v="$(env_pin "$wf" "$var" census)" || { fail "$(basename "$wf") ${var} pin could not be inspected"; continue; } [ -n "$v" ] || continue - case "$v" in - *'${{'* | 'latest' | 'HEAD') continue ;; # floating by design, documented at the pin - esac pin_name="$var"; [[ "$var" != HYPATIA_REF ]] || pin_name=HYPATIA_SHA printf '%s\t%s\t%s\n' "$pin_name" "$v" "$(basename "$wf")" >> "$pins_file" done @@ -443,13 +462,12 @@ for wf in "$WORKFLOWS_DIR"/*.yml "$WORKFLOWS_DIR"/*.yaml; do done <<< "$checkout_rows" # A reusable call's hypatia-ref input pins the same scanner as HYPATIA_SHA. if [ -n "$(reusable_calls "$wf")" ]; then - v="$(env_pin "$wf" hypatia-ref)" || { + v="$(env_pin "$wf" hypatia-ref census)" || { fail "$(basename "$wf") with: hypatia-ref pin could not be inspected"; continue; } - case "$v" in - "" | *'${{'* | 'latest' | 'HEAD') ;; - *) printf '%s\t%s\t%s\n' HYPATIA_SHA "$v" "$(basename "$wf") (with: hypatia-ref)" >> "$pins_file" ;; - esac + if [[ -n "$v" ]]; then + printf '%s\t%s\t%s\n' HYPATIA_SHA "$v" "$(basename "$wf") (with: hypatia-ref)" >> "$pins_file" + fi fi done while IFS= read -r var; do diff --git a/tests/workflows/hypatia_rule_order_test.sh b/tests/workflows/hypatia_rule_order_test.sh index 0c79d94..50e0c8f 100755 --- a/tests/workflows/hypatia_rule_order_test.sh +++ b/tests/workflows/hypatia_rule_order_test.sh @@ -152,7 +152,7 @@ says "the library failure names the variable" "JQ_LIB_SHA256" fresh sed -i "s/^ HYPATIA_SHA: .*/ HYPATIA_SHA: ''/" "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 1 "a workflow with no pin cannot validate the policy" -says "the missing-pin failure says so" "declares no HYPATIA_SHA" +says "the empty-pin failure names the malformed binding" "malformed HYPATIA_SHA scanner pin" # A caller of the shared reusable pins the scanner with its hypatia-ref input, # not an env var: that pin is read and checked like HYPATIA_SHA. @@ -179,7 +179,7 @@ says "the caller-pin failure names the input" "hypatia-ref input in hypatia-scan fresh; caller_wf '' expect_rc 1 "a reusable caller that passes no hypatia-ref cannot validate the policy" -says "the missing caller pin is stated" "passes no hypatia-ref input" +says "the null caller pin is named as malformed" "malformed hypatia-ref scanner pin" # Two pins for one scanner that disagree are refused even when one matches. fresh; caller_wf '"5555555555555555555555555555555555555555"' @@ -203,16 +203,13 @@ jobs: YML } readonly WRONG_SCANNER_PIN=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -fresh -cp "$REPO_DIR/.github/workflows/hypatia-scan.yml" "$WORK/repo/.github/workflows/hypatia-scan.yml" -sed -i "s/^ HYPATIA_SHA: .*/ HYPATIA_SHA: \&scanner \"$scanner_pin\"/" "$WORK/repo/.github/workflows/hypatia-scan.yml" +fresh; env_wf "&scanner \"$scanner_pin\"" expect_rc 0 "an anchored env scanner pin matches the policy" sed -i "s/$scanner_pin/$WRONG_SCANNER_PIN/" "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 1 "an anchored wrong env scanner pin is refused" -fresh -cp "$REPO_DIR/.github/workflows/hypatia-scan.yml" "$WORK/repo/.github/workflows/hypatia-scan.yml" -sed -i "s/^ HYPATIA_SHA: .*/ SCANNER_PIN: \&scanner \"$scanner_pin\"\n HYPATIA_SHA: *scanner/" "$WORK/repo/.github/workflows/hypatia-scan.yml" +fresh; env_wf '*scanner' +sed -i "/^env:/i\x-scanner: \&scanner \"$scanner_pin\"" "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 0 "an aliased env scanner pin matches the policy" sed -i "s/$scanner_pin/$WRONG_SCANNER_PIN/" "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 1 "an aliased wrong env scanner pin is refused" @@ -228,6 +225,23 @@ expect_rc 0 "an aliased reusable scanner input matches the policy" sed -i "s/$scanner_pin/$WRONG_SCANNER_PIN/" "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 1 "an aliased wrong reusable scanner input is refused" +fresh; env_wf "!!binary \"$scanner_pin\"" +expect_rc 1 "a binary-tagged env scanner pin cannot become a JSON string pin" +says "the env pin retains its YAML string-tag requirement" "malformed HYPATIA_SHA scanner pin: scanner pin must have YAML tag !!str" +fresh; caller_wf "!!binary \"$scanner_pin\"" +expect_rc 1 "a binary-tagged reusable scanner input cannot become a JSON string pin" +says "the reusable pin retains its YAML string-tag requirement" "malformed hypatia-ref scanner pin: scanner pin must have YAML tag !!str" +fresh; env_wf '*scanner' +sed -i "/^env:/i\x-scanner: \&scanner !!binary \"$scanner_pin\"" "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 1 "a scalar alias preserves a non-string scanner pin tag" +says "the aliased non-string scanner tag is explained" "scanner pin must have YAML tag !!str" +fresh; env_wf "$scanner_pin" +sed -i -e 's/^ HYPATIA_SHA:.*/ <<: *scanner/' -e "/^env:/i\x-scanner: \&scanner {HYPATIA_SHA: \"$scanner_pin\"}" "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 0 "a merged string scanner pin retains its resolved binding path" +sed -i 's/{HYPATIA_SHA: /{HYPATIA_SHA: !!binary /' "$WORK/repo/.github/workflows/hypatia-scan.yml" +expect_rc 1 "a merged non-string scanner tag is rejected at the resolved binding" +says "the merged non-string scanner tag is explained" "scanner pin must have YAML tag !!str" + fresh; env_wf "$scanner_pin" sed -i 's/^on: .*/on:\n workflow_call:\n inputs:\n HYPATIA_SHA:\n type: string/' "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 0 "an input declaration cannot shadow an actual env scanner pin" @@ -265,13 +279,36 @@ cat > "$WORK/failing-parser/yq" <<'SH' #!/usr/bin/env bash case "$*" in *--version*) echo 'yq (https://github.com/mikefarah/yq/) version v4.53.3' ;; - *'explode(.)'*) printf '{"jobs":{}}\n'; exit 17 ;; + *'explode(.)'*) printf '{"workflow":{"jobs":{}},"pin_tags":[]}\n'; exit 17 ;; *) printf '0\t0\t0\t0\n' ;; esac SH chmod +x "$WORK/failing-parser/yq" PATH="$WORK/failing-parser:$PATH" expect_rc 1 "a scanner decoder failure cannot publish partial JSON" says "the scanner decoder failure is reported" "scanner pins could not be inspected" +if grep -Fq "declares no HYPATIA_SHA" "$WORK/out"; then + bad "an extraction failure must not emit a missing-pin diagnosis" +else + ok "an extraction failure does not emit a missing-pin diagnosis" +fi + +fresh; env_wf "$scanner_pin" +mkdir -p "$WORK/snapshot-parser" +RULE_ORDER_REAL_YQ="$(command -v yq)" +RULE_ORDER_MUTATE_WORKFLOW="$WORK/repo/.github/workflows/hypatia-scan.yml" +export RULE_ORDER_REAL_YQ RULE_ORDER_MUTATE_WORKFLOW +cat > "$WORK/snapshot-parser/yq" <<'SH' +#!/usr/bin/env bash +"$RULE_ORDER_REAL_YQ" "$@" +status=$? +case "$*" in + *--version* | *'explode(.)'*) ;; + *) printf '\nenv: {}\n' >> "$RULE_ORDER_MUTATE_WORKFLOW" ;; +esac +exit "$status" +SH +chmod +x "$WORK/snapshot-parser/yq" +PATH="$WORK/snapshot-parser:$PATH" expect_rc 0 "preflight and decode share a byte snapshot when the original workflow changes" # Malformed refs must not masquerade as immutable scanner pins. for invalid in main be98f74ce93d 4444444444444444444444444444444444444444-extra; do @@ -340,17 +377,17 @@ YML expect_rc 1 "identical reusable scanner inputs in different jobs are refused" says "multiple jobs preserve the duplicate input diagnostic" "passes hypatia-ref more than once" -# Null, empty and absent bindings still explain which scanner pin is missing. +# Null and empty bindings are malformed; only absent bindings are missing. fresh; env_wf null expect_rc 1 "a null env scanner pin cannot validate the policy" -says "a null env scanner pin retains the missing-pin diagnostic" "declares no HYPATIA_SHA" +says "a null env scanner pin retains its malformed-pin diagnostic" "malformed HYPATIA_SHA scanner pin" fresh; env_wf "$scanner_pin" sed -i '/^ HYPATIA_SHA:/d' "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 1 "an absent env scanner pin cannot validate the policy" says "an absent env scanner pin retains the missing-pin diagnostic" "declares no HYPATIA_SHA" fresh; caller_wf "''" expect_rc 1 "an empty string reusable scanner input cannot validate the policy" -says "an empty reusable scanner input retains the missing-pin diagnostic" "passes no hypatia-ref input" +says "an empty reusable scanner input retains its malformed-pin diagnostic" "malformed hypatia-ref scanner pin" fresh; caller_wf "$scanner_pin" sed -i '/^ hypatia-ref:/d' "$WORK/repo/.github/workflows/hypatia-scan.yml" expect_rc 1 "an absent reusable scanner input cannot validate the policy" diff --git a/tests/workflows/workflow_edges_test.sh b/tests/workflows/workflow_edges_test.sh index 3f278ef..8530506 100755 --- a/tests/workflows/workflow_edges_test.sh +++ b/tests/workflows/workflow_edges_test.sh @@ -91,18 +91,75 @@ sed -i 's/--panic-ref b5530834a839734d1d181bfe6edb0d43d4233cf4/--panic-ref b5530 expect_rc 1 "a producer pin that moves without the verdict job is caught" says "the failure names the flag and the variable" "--panic-ref" +echo "=== 5b. Every producer binding must agree ===" +fresh >/dev/null +producer="$WORK/repo/.github/workflows/static-analysis-gate.yml" +panic_pin="$(awk '$1 == "PANIC_ATTACK_SHA:" { print $2; exit }' "$producer")" +pons_pin="$(awk '$1 == "PONS_SHA:" { print $2; exit }' "$producer")" +other_pin=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +sed -i -e "/^env:$/c\env: {PANIC_ATTACK_SHA: \"$panic_pin\", PONS_SHA: \"$pons_pin\"}" \ + -e '/^ \(PANIC_ATTACK_SHA\|PONS_SHA\):/d' \ + -e "/^ panic-attack-assail:/a\ env: {PANIC_ATTACK_SHA: \"$other_pin\"}" "$producer" +expect_rc 1 "a later job pin cannot hide behind the matching workflow pin" +says "the producer binding conflict is explicit" "conflicting literal pins" +sed -i "s/$other_pin/$panic_pin/" "$producer" +expect_rc 0 "the workflow and job may repeat the same pin" +sed -i "/^ panic-attack-assail:/a\ container: {image: alpine:3.23, env: {PANIC_ATTACK_SHA: \"$other_pin\"}}" "$producer" +expect_rc 1 "a conflicting job-container pin is included among producer bindings" +sed -i "s/$other_pin/$panic_pin/" "$producer" +sed -i "/^ - name: Checkout repository$/a\ env: {PANIC_ATTACK_SHA: \"$other_pin\"}" "$producer" +expect_rc 1 "a conflicting step pin is included among producer bindings" +sed -i "s/$other_pin/$panic_pin/" "$producer" +sed -i "/^ panic-attack-assail:/a\ services: {db: {image: alpine:3.23, env: {PANIC_ATTACK_SHA: \"$other_pin\"}}}" "$producer" +expect_rc 0 "equal workflow, job, container and step pins agree with service env excluded" +sed -i 's/^ env: {PANIC_ATTACK_SHA:.*/ env: {PANIC_ATTACK_SHA: HEAD}/' "$producer" +expect_rc 1 "a floating override cannot establish a literal producer identity" +says "an unresolved producer mix has an explicit diagnostic" "producer pin bindings do not agree" + +for value in null true 42 '[]' '{}' '""'; do + sed -i "s/^ env: {PANIC_ATTACK_SHA:.*/ env: {PANIC_ATTACK_SHA: $value}/" "$producer" + expect_rc 1 "a later $value binding cannot fall back to the matching producer pin" +done +says "the malformed binding reports the required type" "pin must be a nonempty YAML string" + echo "=== 6. Firing: one tool pinned at two commits ===" fresh >/dev/null python3 - "$WORK/repo/.github/workflows/codeql.yml" <<'PY' import sys p = sys.argv[1] s = open(p).read() -s = s.replace("jobs:\n", "env:\n PANIC_ATTACK_SHA: 1111111111111111111111111111111111111111\n\njobs:\n", 1) +s = s.replace("jobs:\n", 'env:\n PANIC_ATTACK_SHA: "1111111111111111111111111111111111111111"\n\njobs:\n', 1) open(p, "w").write(s) PY expect_rc 1 "a second pin for the same tool is caught" says "the failure says the pin is in two places" "different commits" +fresh >/dev/null +census="$WORK/repo/.github/workflows/census-control.yml" +cat > "$census" <> "$census" +expect_rc 1 "floating census bindings cannot hide distinct literals in one workflow" +says "the consensus reports the two literal values' conflict" "conflicting literal pins" + echo "=== 7. Firing: no workflow_run at all ===" fresh >/dev/null python3 - "$WORK/repo/.github/workflows/verdict-grid-publish.yml" <<'PY' @@ -225,7 +282,7 @@ fresh >/dev/null; reusable; delegate expect_rc 1 "a reusable producer that passes no hypatia-ref is caught" says "the failure says the default runs instead" "without passing hypatia-ref" -fresh >/dev/null; reusable; delegate 2222222222222222222222222222222222222222 +fresh >/dev/null; reusable; delegate '"2222222222222222222222222222222222222222"' expect_rc 1 "a reusable producer passing a different pin is caught" says "the mismatch names the input it read" "with: hypatia-ref" @@ -249,6 +306,17 @@ cat >> "$WORK/repo/.github/workflows/hypatia-scan.yml" <<'YML' - uses: owner/standards/.github/workflows/scan.yml@2222222222222222222222222222222222222222 YML expect_rc 0 "a step action with a workflow-shaped path is not followed as a reusable call" + +fresh >/dev/null; reusable; delegate "$HSHA" +cat >> "$WORK/repo/.github/workflows/hypatia-scan.yml" </dev/null; checkout_fixture "$HSHA" printf ' Ref: %s\n' "$HSHA" >> "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "identical values do not excuse case-colliding checkout inputs" -fresh >/dev/null; checkout_fixture 1111111111111111111111111111111111111111 +fresh >/dev/null; checkout_fixture '"1111111111111111111111111111111111111111"' sed -i '/ - uses:/,$s/^ //' "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "a wrong Hypatia pin in an indentless steps sequence is refused" fresh >/dev/null; checkout_fixture "$HSHA" sed -i '/ - uses:/,$s/^ //' "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 0 "a matching pin in an indentless steps sequence is accepted" -fresh >/dev/null; checkout_fixture 1111111111111111111111111111111111111111 +fresh >/dev/null; checkout_fixture '"1111111111111111111111111111111111111111"' sed -i '/ - uses:/,$s/^/ /' "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "a more-indented steps sequence cannot hide a wrong Hypatia pin" fresh >/dev/null; checkout_fixture main @@ -333,15 +401,15 @@ expect_rc 1 "extra spaces after the list dash cannot hide a floating checkout re fresh >/dev/null; checkout_fixture "$HSHA" sed -i -e 's/ - uses:/ - uses:/' -e '/ with:/,$s/^/ /' "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 0 "checkout mappings use the actual inline key column after the dash" -fresh >/dev/null; checkout_fixture 1111111111111111111111111111111111111111 +fresh >/dev/null; checkout_fixture '"1111111111111111111111111111111111111111"' sed -i -e 's|hyperpolymath/hypatia|Hyperpolymath/Hypatia|' -e 's|actions/checkout@|Actions/Checkout@|' "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "repository name casing cannot hide a different Hypatia checkout pin" fresh >/dev/null; checkout_fixture "$HSHA" sed -i -e "s/with:/'with':/" -e 's/repository:/"repository":/' -e "s/ref:/'ref':/" "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 0 "matching checkout refs with quoted mapping keys are recognized" -sed -i "s/$HSHA/1111111111111111111111111111111111111111/" "$WORK/repo/.github/workflows/oracle-pin-control.yml" +sed -i "s/$HSHA/\"1111111111111111111111111111111111111111\"/" "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "quoted mapping keys cannot hide a different Hypatia checkout pin" -fresh >/dev/null; checkout_fixture 1111111111111111111111111111111111111111 "$(printf 'env:\n HYPATIA_SHA: %s' "$HSHA")" +fresh >/dev/null; checkout_fixture '"1111111111111111111111111111111111111111"' "$(printf 'env:\n HYPATIA_SHA: %s' "$HSHA")" expect_rc 1 "a wrong literal checkout ref is caught even with the correct env pin" says "the disagreement identifies the actual checkout" "Hypatia checkout" fresh >/dev/null; checkout_fixture "$HYPATIA_REF_EXPR" "$(printf 'env:\n HYPATIA_SHA: %s' "$HSHA")" @@ -398,11 +466,11 @@ expect_rc 0 "separate jobs may declare the same env variable name independently" fresh >/dev/null; checkout_fixture "$PIN_REF" "$(printf 'env: # workflow binding\n PIN: %s' "$HSHA")" expect_rc 0 "an env header with a trailing comment still supplies workflow scope" fresh >/dev/null; checkout_fixture "$PIN_REF" 'env: - PIN: 1111111111111111111111111111111111111111' + PIN: "1111111111111111111111111111111111111111"' sed -i "/ runs-on:/i\ env: # job overrides workflow\n PIN: $HSHA" "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 0 "a unique job binding overrides the same workflow env name" fresh >/dev/null; checkout_fixture "$PIN_REF" "$(printf 'env:\n PIN: %s' "$HSHA")" -sed -i '/ runs-on:/i\ env:\n PIN: 1111111111111111111111111111111111111111' "$WORK/repo/.github/workflows/oracle-pin-control.yml" +sed -i '/ runs-on:/i\ env:\n PIN: "1111111111111111111111111111111111111111"' "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "the job override determines the checked ref, not the workflow value" fresh >/dev/null; checkout_fixture "$PIN_REF" sed -i "/ runs-on:/i\ env:\n PIN: $HSHA\n PIN: $HSHA" "$WORK/repo/.github/workflows/oracle-pin-control.yml" @@ -411,23 +479,23 @@ fresh >/dev/null; checkout_fixture "$PIN_REF" sed -i "/ runs-on:/i\ env:\n PIN: $HSHA" "$WORK/repo/.github/workflows/oracle-pin-control.yml" cat >> "$WORK/repo/.github/workflows/oracle-pin-control.yml" <<'YML' env: - PIN: 1111111111111111111111111111111111111111 + PIN: "1111111111111111111111111111111111111111" YML expect_rc 1 "a wrong step binding cannot hide behind the correct job binding" fresh >/dev/null; checkout_fixture "$PIN_REF" sed -i "/ runs-on:/i\ env:\n PIN: $HSHA" "$WORK/repo/.github/workflows/oracle-pin-control.yml" -sed -i 's/ - uses:/ - env:\n PIN: 1111111111111111111111111111111111111111\n uses:/' "$WORK/repo/.github/workflows/oracle-pin-control.yml" +sed -i 's/ - uses:/ - env:\n PIN: "1111111111111111111111111111111111111111"\n uses:/' "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "a first-key step env override cannot borrow the correct job value" fresh >/dev/null; checkout_fixture "$PIN_REF" sed -i "/ runs-on:/i\ env:\n PIN: $HSHA" "$WORK/repo/.github/workflows/oracle-pin-control.yml" -printf ' env: {PIN: 1111111111111111111111111111111111111111}\n' >> "$WORK/repo/.github/workflows/oracle-pin-control.yml" +printf ' env: {PIN: "1111111111111111111111111111111111111111"}\n' >> "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "a wrong flow step env override cannot hide behind a job value" fresh >/dev/null; checkout_fixture "$PIN_REF" sed -i "/ runs-on:/i\ env:\n PIN: $HSHA" "$WORK/repo/.github/workflows/oracle-pin-control.yml" -sed -i 's/ - uses:/ - env: {PIN: 1111111111111111111111111111111111111111}\n uses:/' "$WORK/repo/.github/workflows/oracle-pin-control.yml" +sed -i 's/ - uses:/ - env: {PIN: "1111111111111111111111111111111111111111"}\n uses:/' "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "a wrong first-key flow step env is inspected" fresh >/dev/null; checkout_fixture "$PIN_REF" "$(printf 'env:\n PIN: %s' "$HSHA")" -sed -i '/ runs-on:/i\ env: {PIN: 1111111111111111111111111111111111111111}' "$WORK/repo/.github/workflows/oracle-pin-control.yml" +sed -i '/ runs-on:/i\ env: {PIN: "1111111111111111111111111111111111111111"}' "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "a wrong flow job env override cannot fall back to a workflow value" fresh >/dev/null; checkout_fixture main sed -i 's/ steps:/ steps: # oracle/' "$WORK/repo/.github/workflows/oracle-pin-control.yml" @@ -526,6 +594,20 @@ expect_rc 0 "uses, input and whole-step aliases resolve to real checkout maps" sed -i 's/ref:.*# alias override/ref: main # alias override/' "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 1 "an explicit wrong ref overrides merged inputs and is refused" +fresh >/dev/null; checkout_fixture "$HYPATIA_REF_EXPR" "$(printf 'x-pins: &pins {HYPATIA_SHA: "%s"}\nx-extra: {stamp: 2026-11-25, note: !metadata ignored}\nenv: {<<: *pins}' "$HSHA")" +expect_rc 0 "a merged YAML string is a pin and unrelated tags are ignored" +sed -i 's/{HYPATIA_SHA: /{HYPATIA_SHA: !!binary /' "$WORK/repo/.github/workflows/oracle-pin-control.yml" +expect_rc 1 "a binary tag survives a merge and cannot supply the scanner pin" +says "the merged pin is identified under its effective env path" "HYPATIA_SHA pin could not be inspected" + +fresh >/dev/null; checkout_fixture "!!binary $HSHA" +sed -i 's/ ref:/ ReF:/' "$WORK/repo/.github/workflows/oracle-pin-control.yml" +expect_rc 1 "a binary tag on a mixed-case checkout ref is rejected" +says "the tagged ref is reported as non-literal" "full-SHA checkout ref" +fresh >/dev/null; checkout_fixture "$PIN_REF" "$(printf 'env: {PIN: !metadata "%s"}' "$HSHA")" +expect_rc 1 "an arbitrary tagged checkout env value cannot resolve as a string" +says "the effective env value must be a literal string" "checkout env binding is not a literal string" + fresh >/dev/null printf '{name: Oracle checkout control, jobs: {score: {steps: [{uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1, with: {repository: hyperpolymath/hypatia, ref: "%s"}}]}}}\n' "$HSHA" > "$WORK/repo/.github/workflows/oracle-pin-control.yml" expect_rc 0 "a complete flow-style workflow with the correct pin is accepted" @@ -615,6 +697,30 @@ else bad "workflow decoding was absent or repeated despite the cache" fi +fresh >/dev/null; checkout_fixture "$HSHA" +EDGES_SOURCE="$WORK/repo/.github/workflows/oracle-pin-control.yml" +export EDGES_SOURCE +mkdir -p "$WORK/mutating-parser" +cat > "$WORK/mutating-parser/yq" <<'SH' +#!/usr/bin/env bash +case "$*" in + *'group_by(.key)'*) + "$EDGES_REAL_YQ" "$@" || exit "$?" + case "${!#}" in + */oracle-pin-control.yml|*/oracle-pin-control.yml.yaml) + sed -i 's/ ref:.*/ ref: main/' "$EDGES_SOURCE" ;; + esac ;; + *) exec "$EDGES_REAL_YQ" "$@" ;; +esac +SH +chmod +x "$WORK/mutating-parser/yq" +PATH="$WORK/mutating-parser:$PATH" expect_rc 0 "decode uses the same bytes that passed raw AST preflight" +if grep -Fxq ' ref: main' "$EDGES_SOURCE"; then + ok "the source really changed between preflight and decode" +else + bad "the preflight wrapper did not mutate the original source" +fi + fresh >/dev/null mkdir -p "$WORK/failing-parser" cat > "$WORK/failing-parser/yq" <<'SH'