|
| 1 | +name: secret-scan |
| 2 | + |
| 3 | +# ADVISORY - not a required check. Fails the run if a commit this push/PR ADDS contains a secret. |
| 4 | +# |
| 5 | +# Runs the gitleaks binary directly instead of gitleaks/gitleaks-action, which needs a |
| 6 | +# GITLEAKS_LICENSE secret on organisation-owned repos. The binary needs no secret at all, so |
| 7 | +# this job cannot go red for a missing licence. The tarball is verified against a pinned |
| 8 | +# SHA-256 before it is unpacked (bump version and hash together; the hash is published in the |
| 9 | +# release's gitleaks_<version>_checksums.txt). |
| 10 | +# |
| 11 | +# --redact is deliberate and must stay: this repository is PUBLIC and so are its Actions logs, |
| 12 | +# so a finding must be reported by rule, file and line and never echoed. |
| 13 | +# |
| 14 | +# Scope, stated plainly: this scans the COMMITS IN THIS PUSH/PR RANGE. It answers "did this |
| 15 | +# change add a secret?" and is blind to anything committed before it existed. It reports |
| 16 | +# NOT_CHECKED (a warning, not a pass) if the range holds no commits. |
| 17 | + |
| 18 | +on: |
| 19 | + push: |
| 20 | + branches: ['**'] |
| 21 | + pull_request: |
| 22 | + |
| 23 | +permissions: |
| 24 | + contents: read |
| 25 | + |
| 26 | +jobs: |
| 27 | + gitleaks: |
| 28 | + name: gitleaks |
| 29 | + runs-on: ubuntu-latest |
| 30 | + timeout-minutes: 10 |
| 31 | + steps: |
| 32 | + - name: Check out full history |
| 33 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 34 | + with: |
| 35 | + fetch-depth: 0 |
| 36 | + persist-credentials: false |
| 37 | + |
| 38 | + - name: Install gitleaks (checksum-pinned) |
| 39 | + env: |
| 40 | + GITLEAKS_VERSION: 8.30.1 |
| 41 | + GITLEAKS_SHA256: 551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb |
| 42 | + run: | |
| 43 | + set -euo pipefail |
| 44 | + curl -sSfL --retry 3 -o "$RUNNER_TEMP/gitleaks.tar.gz" \ |
| 45 | + "https://github.com/gitleaks/gitleaks/releases/download/v${GITLEAKS_VERSION}/gitleaks_${GITLEAKS_VERSION}_linux_x64.tar.gz" |
| 46 | + echo "${GITLEAKS_SHA256} $RUNNER_TEMP/gitleaks.tar.gz" | sha256sum -c - |
| 47 | + tar -xzf "$RUNNER_TEMP/gitleaks.tar.gz" -C "$RUNNER_TEMP" gitleaks |
| 48 | + "$RUNNER_TEMP/gitleaks" version |
| 49 | +
|
| 50 | + - name: Scan the commits this event adds |
| 51 | + env: |
| 52 | + EVENT_NAME: ${{ github.event_name }} |
| 53 | + PR_BASE: ${{ github.event.pull_request.base.sha }} |
| 54 | + PR_HEAD: ${{ github.event.pull_request.head.sha }} |
| 55 | + PUSH_BEFORE: ${{ github.event.before }} |
| 56 | + PUSH_AFTER: ${{ github.sha }} |
| 57 | + DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} |
| 58 | + run: | |
| 59 | + set -euo pipefail |
| 60 | + zero=0000000000000000000000000000000000000000 |
| 61 | +
|
| 62 | + # Work out WHICH commits this event adds. |
| 63 | + if [ "$EVENT_NAME" = "pull_request" ]; then |
| 64 | + range=("${PR_BASE}..${PR_HEAD}") |
| 65 | + elif [ "$PUSH_BEFORE" = "$zero" ]; then |
| 66 | + # New branch: everything not already on the default branch. |
| 67 | + range=("$PUSH_AFTER" --not "origin/${DEFAULT_BRANCH}") |
| 68 | + elif git cat-file -e "${PUSH_BEFORE}^{commit}" 2>/dev/null; then |
| 69 | + range=("${PUSH_BEFORE}..${PUSH_AFTER}") |
| 70 | + else |
| 71 | + # Force-push: the old tip is not in this clone, so scan the new tip only. |
| 72 | + range=(-1 "$PUSH_AFTER") |
| 73 | + fi |
| 74 | +
|
| 75 | + in_range=$(git rev-list --count "${range[@]}") |
| 76 | + echo "range: ${range[*]} (${in_range} commit(s) in range)" |
| 77 | +
|
| 78 | + # --exit-code 2 = leaks found. A gitleaks crash exits 1, so "found a secret" and |
| 79 | + # "the scanner broke" are different codes; both leave the job red. |
| 80 | + report="$RUNNER_TEMP/gitleaks-report.json" |
| 81 | + log="$RUNNER_TEMP/gitleaks.log" |
| 82 | + rc=0 |
| 83 | + "$RUNNER_TEMP/gitleaks" git . \ |
| 84 | + --log-opts="${range[*]}" \ |
| 85 | + --redact --no-banner --no-color --exit-code 2 \ |
| 86 | + --report-format json --report-path "$report" 2>&1 | tee "$log" || rc=$? |
| 87 | +
|
| 88 | + # Take the count from gitleaks itself, not from `git rev-list`. gitleaks diffs |
| 89 | + # commits, and a merge commit has no diff, so a range of only merge commits has a |
| 90 | + # non-zero rev-list count and 0 commits scanned. Reporting the first as the second |
| 91 | + # is how a scan "passes" over nothing. |
| 92 | + scanned=$(grep -oE '[0-9]+ commits? scanned' "$log" | head -n1 | grep -oE '^[0-9]+' || true) |
| 93 | +
|
| 94 | + if [ "$rc" = "2" ]; then |
| 95 | + { |
| 96 | + echo "### secret-scan: FAILED - secret(s) found" |
| 97 | + echo "${scanned:-?} commit(s) scanned. Values are redacted; rule, file and line only:" |
| 98 | + echo |
| 99 | + jq -r 'def s: gsub("[\r\n%]"; ""); .[] | "- `\(.RuleID|s)` at `\(.File|s):\(.StartLine)` in \(.Commit[0:8]|s)"' "$report" |
| 100 | + } >> "$GITHUB_STEP_SUMMARY" |
| 101 | + jq -r 'def s: gsub("[\r\n%]"; ""); .[] | "::error file=\(.File|s),line=\(.StartLine),title=gitleaks \(.RuleID|s)::possible secret (redacted) in commit \(.Commit[0:8]|s)"' "$report" |
| 102 | + echo "FAILED: gitleaks reported finding(s); see the summary for rule, file and line" |
| 103 | + exit 1 |
| 104 | + fi |
| 105 | +
|
| 106 | + if [ "$rc" != "0" ]; then |
| 107 | + echo "FAILED: gitleaks exited ${rc}, which is a scanner error and not a finding" |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | +
|
| 111 | + if [ -z "$scanned" ] || [ "$scanned" = "0" ]; then |
| 112 | + echo "::warning title=secret-scan NOT_CHECKED::gitleaks scanned ${scanned:-an unreported number of} commits in range (${range[*]}); nothing was verified" |
| 113 | + { |
| 114 | + echo "### secret-scan: NOT_CHECKED" |
| 115 | + echo "gitleaks reported ${scanned:-no} commits scanned for \`${range[*]}\` (${in_range} in range). No findings, but nothing was verified either." |
| 116 | + } >> "$GITHUB_STEP_SUMMARY" |
| 117 | + exit 0 |
| 118 | + fi |
| 119 | +
|
| 120 | + { |
| 121 | + echo "### secret-scan: VERIFIED" |
| 122 | + echo "${scanned} commit(s) scanned (\`${range[*]}\`), 0 findings." |
| 123 | + } >> "$GITHUB_STEP_SUMMARY" |
| 124 | + echo "VERIFIED: ${scanned} commit(s) scanned, 0 findings" |
0 commit comments