Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 31 additions & 21 deletions .github/workflows/secret-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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 }}
Expand Down
Loading