Skip to content

Commit 4f51cca

Browse files
fix(ci): build the scanner, not the caller, in scan-and-report.yml (#286)
Refs #276 and #212. This pull request does not close #276 by itself: the first acceptance criterion needs a hosted run from an external caller (see "Not shown yet"). ## Symptom Every repository that calls `scan-and-report.yml` as a reusable workflow fails at **Run scan** with exit 127: `./target/release/panic-attack` does not exist. Observed on hyperpolymath/echidna `main`, Security Scan run 42351376316. ## Cause In a reusable workflow, `actions/checkout` with no `repository:` checks out the **caller**. The step `cargo build --locked --release` therefore built the caller's crate. It worked only when this repository ran the workflow on itself. ## Change The workflow now fetches the scanner source at the commit of this workflow file and builds that. - **Source:** `job.workflow_repository` at `job.workflow_sha`. GitHub documents both as the repository and commit of the workflow file that defines the job. - A caller that pins `scan-and-report.yml@<sha>` gets the scanner at that SHA. It is never `main` by name, which keeps the #212 property. - A run of this workflow in its own repository still builds the commit under test. - **Location:** the runner's temp directory, outside the workspace. The scan cannot report on the scanner's own files. - **Refusals:** the fetch step exits 2 for a repository that is not `owner/name` and for a commit that is not a full SHA. It also checks that the fetched `HEAD` is that SHA. - **Cache:** the cache step keys on the scanner's workspace, not the caller's. - **Scan step:** it takes the target path from the environment and quotes it. No `uses:` reference changes, so `actions.lock` is unchanged. One difference from the proposal in #276: the proposal names `hyperpolymath/panic-attack` as a literal. This change takes the repository from `job.workflow_repository`, so a rename of this repository cannot break the fetch. ## Acceptance criteria | Criterion | State | |---|---| | An external caller reaches `Run scan`, and the binary exists | Local run only. Needs a hosted run from an external caller. | | This repository's own run still builds the commit under test | Hosted `workflow_dispatch` run on this branch: see the comment below when it completes. | | The report contains no findings from the scanner source | Holds by construction: the source is outside the workspace. The local report names no scanner path. | ## Evidence (local) I extracted the changed steps from the workflow file and ran them on x86_64 Linux with cargo 1.97.0. - **Fetch:** succeeds at `c7b1c44` (`main` at that time). It is refused with exit 2 for `main`, a short SHA, a malformed repository and an empty repository. - **Build:** 3 min 14 s. The binary exists at the path that the scan step uses. - **Scan:** a checkout of hyperpolymath/echidna as the caller. Exit 0, schema 2.6 report, 951 of 951 files analysed, 130 weak points. The report names no scanner source path, and no scanner source is inside the workspace. ## Not shown yet That `job.workflow_sha` names this repository's commit inside a call from another repository. The local run cannot show it. A caller must pin a commit that contains this change; echidna's `security-scan.yml` still pins `be6ac46`. --- _Generated by [Claude Code](https://claude.ai/code/session_0136P7ZmAsYpFA9gxfQLF3F6)_ Signed-off-by: Claude <noreply@anthropic.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent c7b1c44 commit 4f51cca

1 file changed

Lines changed: 51 additions & 10 deletions

File tree

‎.github/workflows/scan-and-report.yml‎

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,70 @@ jobs:
3636
runs-on: ubuntu-latest
3737
timeout-minutes: 20
3838
steps:
39+
# The scan target. In a reusable workflow, a checkout with no
40+
# `repository:` is the CALLER, not this repository.
3941
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
4042

43+
# The scanner source, at the commit of THIS workflow file. `job.workflow_*`
44+
# names the workflow that defines the job: for a caller that pins
45+
# `scan-and-report.yml@<sha>` it is this repository at that SHA, and for a
46+
# run of this file in its own repository it is the commit under test. It is
47+
# never `main` by name.
48+
#
49+
# The source goes to the runner's temp directory, outside the workspace,
50+
# so the scan below cannot report on the scanner's own files.
51+
- name: Fetch the scanner source at this workflow's commit
52+
env:
53+
SCANNER_REPOSITORY: ${{ job.workflow_repository }}
54+
SCANNER_SHA: ${{ job.workflow_sha }}
55+
SCANNER_SRC: ${{ runner.temp }}/panic-attack-src
56+
GIT_TERMINAL_PROMPT: '0'
57+
run: |
58+
set -euo pipefail
59+
[[ "$SCANNER_REPOSITORY" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]] \
60+
|| { echo "::error::scanner repository is not owner/name (got '${SCANNER_REPOSITORY}'); no scanner was built"; exit 2; }
61+
[[ "$SCANNER_SHA" =~ ^[0-9a-f]{40}$ ]] \
62+
|| { echo "::error::scanner commit is not a full SHA (got '${SCANNER_SHA}'); no scanner was built"; exit 2; }
63+
git init -q "$SCANNER_SRC"
64+
git -C "$SCANNER_SRC" fetch -q --depth 1 "https://github.com/${SCANNER_REPOSITORY}.git" "$SCANNER_SHA"
65+
git -C "$SCANNER_SRC" checkout -q --detach FETCH_HEAD
66+
[[ "$(git -C "$SCANNER_SRC" rev-parse HEAD)" == "$SCANNER_SHA" ]] \
67+
|| { echo "::error::fetched scanner source is not ${SCANNER_SHA}"; exit 2; }
68+
echo "scanner source: ${SCANNER_REPOSITORY}@${SCANNER_SHA}"
69+
4170
- name: Install Rust
4271
uses: dtolnay/rust-toolchain@683b588c487a69d01f6c6ddf40f036ff682cc2c8 # v1
4372
with:
4473
toolchain: stable
4574

4675
- name: Cache Rust dependencies
4776
uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
48-
49-
# Build the commit under test, not `main`. The previous step was
50-
# `cargo install --git … --branch main`, so every pull request was scanned
51-
# by whatever happened to be on main at that moment: the workflow reported
52-
# on code it had not checked out, and a PR that broke the scanner could
53-
# pass with the last good binary.
54-
- name: Build the commit under test
55-
run: cargo build --locked --release
77+
with:
78+
# Key and save the scanner's build, not the caller's workspace.
79+
workspaces: ${{ runner.temp }}/panic-attack-src -> target
80+
81+
# Build the scanner at the fetched commit, not `main` and not the caller.
82+
# History of this step: `cargo install --git … --branch main` scanned
83+
# every pull request with whatever was on main (#212). Its replacement,
84+
# `cargo build` in the workspace, built the CALLER, so every external
85+
# caller failed at "Run scan" with exit 127 (#276).
86+
- name: Build the scanner at this workflow's commit
87+
env:
88+
SCANNER_SRC: ${{ runner.temp }}/panic-attack-src
89+
run: |
90+
set -euo pipefail
91+
cargo build --locked --release --manifest-path "$SCANNER_SRC/Cargo.toml"
92+
test -x "$SCANNER_SRC/target/release/panic-attack"
5693
5794
- name: Run scan
5895
id: scan
96+
env:
97+
SCANNER: ${{ runner.temp }}/panic-attack-src/target/release/panic-attack
98+
REPO_PATH: ${{ inputs.repo_path }}
5999
run: |
60-
./target/release/panic-attack assail ${{ inputs.repo_path }} --output scan-result.json
61-
echo "scan_complete=true" >> $GITHUB_OUTPUT
100+
set -euo pipefail
101+
"$SCANNER" assail "$REPO_PATH" --output scan-result.json
102+
echo "scan_complete=true" >> "$GITHUB_OUTPUT"
62103
63104
- name: Enforce severity gate
64105
if: steps.scan.outputs.scan_complete == 'true' && inputs.fail_on_severity != 'none'

0 commit comments

Comments
 (0)