From c1ecff8d49c7e0375b6b27e3282666c9b1819a8c Mon Sep 17 00:00:00 2001 From: geoffg-sentry <165922362+geoffg-sentry@users.noreply.github.com> Date: Fri, 13 Nov 2026 11:37:05 -0500 Subject: [PATCH] fix(secret-scan); Some updates to #220 for scanning diffs in place: - Check out the event head SHA instead of refs/pull/N/head or the merge queue branch, so force-pushes and dequeued entries can't change what gets scanned. - Move the history deepen into its own step, fetching only the head commit rather than all branches. - Fail the job on history setup errors instead of reporting them as secret findings to the SIEM. --- .github/workflows/secret-scan.yml | 52 ++++++++++++++++++------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/.github/workflows/secret-scan.yml b/.github/workflows/secret-scan.yml index b0c6cdc..6796fed 100644 --- a/.github/workflows/secret-scan.yml +++ b/.github/workflows/secret-scan.yml @@ -17,15 +17,16 @@ jobs: commits="${{ github.event.pull_request.commits }}" # Generations back to the fork point, plus one commit past it. depth=$((commits + 2)) - ref="refs/pull/${{ github.event.pull_request.number }}/head" base_tip="${{ github.event.pull_request.base.sha }}" head="${{ github.event.pull_request.head.sha }}" else base_tip="${{ github.event.merge_group.base_sha }}" head="${{ github.event.merge_group.head_sha }}" - ref="${{ github.event.merge_group.head_ref }}" fi - if [ -z "$base_tip" ] || [ -z "$head" ] || [ -z "$ref" ]; then + # Checkout this SHA, not refs/pull/N/head or the merge queue branch. + # Those refs move, and the queue branch is deleted once the entry + # leaves the queue. The event SHA stays fetchable. + if [ -z "$base_tip" ] || [ -z "$head" ]; then echo "::error::No scan range for ${event}" exit 1 fi @@ -48,15 +49,38 @@ jobs: depth=$((count + 2)) fi echo "depth=$depth" >> "$GITHUB_OUTPUT" - echo "ref=$ref" >> "$GITHUB_OUTPUT" echo "base=$base" >> "$GITHUB_OUTPUT" echo "head=$head" >> "$GITHUB_OUTPUT" echo "Scan ${head} since ${base} (fetch-depth ${depth})" - name: Checkout Code uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - ref: ${{ steps.range.outputs.ref }} + ref: ${{ steps.range.outputs.head }} fetch-depth: ${{ steps.range.outputs.depth }} + - name: Fetch scan history + id: history + env: + BASE: ${{ steps.range.outputs.base }} + HEAD: ${{ steps.range.outputs.head }} + run: | + # A shallow checkout of the head SHA can still stop short of the + # merge base when the pull request contains merge commits. Deepen + # that commit only. `git fetch origin` with no refspec downloads + # every branch, which is the disk use this workflow is avoiding. + if ! git cat-file -e "${HEAD}^{commit}" 2>/dev/null; then + echo "::error::${HEAD} is not in the checkout" + exit 1 + fi + if ! git cat-file -e "${BASE}^{commit}" 2>/dev/null || ! git merge-base --is-ancestor "$BASE" "$HEAD"; then + echo "Deepening ${HEAD} until ${BASE} is an ancestor" + git fetch --no-tags --deepen=100 origin "$HEAD" || exit 1 + fi + if ! git merge-base --is-ancestor "$BASE" "$HEAD"; then + echo "::error::${BASE} is not an ancestor of ${HEAD}; refusing to scan" + exit 1 + fi + echo "Scanning commits after ${BASE} through ${HEAD}" + git rev-list --count "${BASE}..${HEAD}" - name: Install Cosign id: cosign continue-on-error: true @@ -125,27 +149,13 @@ jobs: tar xzf "trufflehog_${TRUFFLEHOG_VERSION}_linux_amd64.tar.gz" -C /usr/local/bin chmod +x /usr/local/bin/trufflehog - name: Run TruffleHog scan - if: ${{ steps.extract.outcome == 'success' }} + if: ${{ steps.history.outcome == 'success' && steps.extract.outcome == 'success' }} continue-on-error: true id: scan env: BASE: ${{ steps.range.outputs.base }} HEAD: ${{ steps.range.outputs.head }} run: | - # A depth-1 fetch of a base tip that is not an ancestor leaves no - # path between the two commits. TruffleHog then errors with - # "unable to resolve merge base", scans nothing, and exits 0. - if ! git cat-file -e "${BASE}^{commit}" 2>/dev/null || ! git merge-base --is-ancestor "$BASE" "$HEAD"; then - echo "Deepening history until ${BASE} is an ancestor of ${HEAD}" - git fetch --no-tags --deepen=100 origin || exit 1 - fi - if ! git merge-base --is-ancestor "$BASE" "$HEAD"; then - echo "::error::${BASE} is not an ancestor of ${HEAD}; refusing to scan" - exit 1 - fi - echo "Scanning commits after ${BASE} through ${HEAD}" - git rev-list --count "${BASE}..${HEAD}" || true - # --since-commit scans the diff range. On a file:// repo TruffleHog # still checks the worktree out a second time unless told to trust # this checkout. That second checkout is what fills the disk. @@ -165,7 +175,7 @@ jobs: fi trufflehog "${args[@]}" - name: Report scan skipped - if: ${{ !cancelled() && steps.scan.outcome == 'skipped' }} + if: ${{ !cancelled() && steps.history.outcome == 'success' && steps.scan.outcome == 'skipped' }} env: COSIGN_OUTCOME: ${{ steps.cosign.outcome }} RELEASE_OUTCOME: ${{ steps.trufflehog_release.outcome }}