Files
ci-actions/gitleaks/action.yml
T
tthandClaude Opus 5.5 67a711fe3c gitleaks: fetch the parent of a single scanned commit in shallow checkouts (v3)
A dispatch or cron run has no before-sha, so one commit is scanned; in a
depth-1 checkout it has no parent and gitleaks diffed it as a root commit,
reporting the whole tree (corescope: 3 upstream files).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 19:34:32 +01:00

134 lines
5.5 KiB
YAML

name: gitleaks
description: >
Scan the commits a push brings for secrets with gitleaks. Only the
first-parent line is scanned, so a merge of upstream into a fork reports
our commits, not the upstream history. Findings are always redacted.
inputs:
mode:
description: "block = fail the step on findings, warn = report only."
default: block
log-opts:
description: >
git log range/options passed to gitleaks. Empty = the commits of this
push (before..sha) on the first-parent line, else the last commit.
default: ""
config:
description: >
gitleaks config in the repo (allowlists); used when it exists. Without
one, the default rules apply with test files allowlisted (fixtures
carry fake keys by design and the noise would bury a real one).
default: .gitleaks.toml
skip:
description: Emergency bypass (workflow_dispatch input); logged loudly.
default: "false"
outputs:
findings:
description: Number of findings.
value: ${{ steps.scan.outputs.findings }}
runs:
using: composite
steps:
- id: scan
shell: bash
env:
MODE: ${{ inputs.mode }}
LOG_OPTS: ${{ inputs.log-opts }}
CONFIG: ${{ inputs.config }}
SKIP: ${{ inputs.skip }}
BEFORE: ${{ github.event.before }}
SHA: ${{ github.sha }}
GITLEAKS_VERSION: "8.30.1"
GITLEAKS_SHA256_X64: 551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb
GITLEAKS_SHA256_ARM64: e4a487ee7ccd7d3a7f7ec08657610aa3606637dab924210b3aee62570fb4b080
run: |
set -euo pipefail
if [ "$SKIP" = "true" ]; then
echo "::warning::GITLEAKS SKIPPED (manual bypass)"; echo "findings=0" >>"$GITHUB_OUTPUT"; exit 0
fi
case "$MODE" in block|warn) ;; *) echo "::error::mode must be block or warn"; exit 1 ;; esac
work="${RUNNER_TEMP:-/tmp}/gitleaks"; mkdir -p "$work"
if ! command -v jq >/dev/null; then
(apt-get update -qq && apt-get install -y -qq jq) >/dev/null 2>&1 || { echo "::error::jq is required"; exit 1; }
fi
git config --global --add safe.directory "$PWD" 2>/dev/null || true
# Release binary, verified by SHA-256 (no Docker needed: the repo is
# local to the job, wherever the job runs).
bin="$work/gitleaks"
if [ ! -x "$bin" ]; then
case "$(uname -m)" in
x86_64) arch=x64; sum="$GITLEAKS_SHA256_X64" ;;
aarch64|arm64) arch=arm64; sum="$GITLEAKS_SHA256_ARM64" ;;
*) echo "::error::no pinned gitleaks binary for $(uname -m)"; exit 1 ;;
esac
curl -fsSL -o "$work/gl.tgz" \
"https://github.com/gitleaks/gitleaks/releases/download/v${GITLEAKS_VERSION}/gitleaks_${GITLEAKS_VERSION}_linux_${arch}.tar.gz"
echo "$sum $work/gl.tgz" | sha256sum -c --quiet
tar -xzf "$work/gl.tgz" -C "$work" gitleaks
fi
opts="$LOG_OPTS"
if [ -z "$opts" ]; then
zero=0000000000000000000000000000000000000000
if [ -n "$BEFORE" ] && [ "$BEFORE" != "$zero" ]; then
# Checkouts are often shallow: deepen until the old head is there.
for _ in 1 2 3 4 5 6; do
git cat-file -e "$BEFORE^{commit}" 2>/dev/null && break
git fetch -q --deepen=100 origin 2>/dev/null || break
done
if git cat-file -e "$BEFORE^{commit}" 2>/dev/null; then
opts="--first-parent --diff-merges=off $BEFORE..$SHA"
fi
fi
if [ -z "$opts" ]; then
# One commit (dispatch/cron). In a depth-1 checkout it has no
# parent and would be diffed as a root commit, i.e. the whole
# tree (upstream files of a fork included): fetch its parent.
head="${SHA:-HEAD}"
git rev-parse -q --verify "$head^" >/dev/null 2>&1 || git fetch -q --deepen=1 origin 2>/dev/null || true
opts="--diff-merges=off -1 $head"
fi
fi
if [ -n "$CONFIG" ] && [ -f "$CONFIG" ]; then
cfg=(--config "$CONFIG")
else
cat >"$work/default.toml" <<'TOML'
[extend]
useDefault = true
[[allowlists]]
description = "test files and fixtures"
paths = [
'(^|/)(tests?|__tests__|testdata|fixtures?|e2e)/',
'_test\.(go|dart|py)$',
'(^|/)test_[^/]*\.py$',
'\.(test|spec)\.(ts|js|mjs|tsx)$',
'(^|/)test-[^/]*\.js$',
]
TOML
cfg=(--config "$work/default.toml")
fi
echo "gitleaks over: git log $opts"
rc=0
"$bin" git --no-banner --redact --log-level warn "${cfg[@]}" --log-opts "$opts" \
--report-format json --report-path "$work/report.json" . || rc=$?
[ "$rc" -le 1 ] || { echo "::error::gitleaks failed (exit $rc)"; exit 1; }
n=$(jq length "$work/report.json" 2>/dev/null || echo 0)
echo "findings=$n" >>"$GITHUB_OUTPUT"
if [ "$n" -gt 0 ]; then
jq -r '.[] | [.RuleID, .File, (.StartLine|tostring), .Commit[0:10]] | @tsv' "$work/report.json" \
| { printf 'RULE\tFILE\tLINE\tCOMMIT\n'; cat; } \
| { if command -v column >/dev/null; then column -t -s $'\t'; else cat; fi; }
if [ "$MODE" = block ]; then
echo "::error::gitleaks: $n secrets in the pushed commits - rotate them, then allowlist or rewrite"
exit 1
fi
echo "::warning::gitleaks (warn mode): $n secrets in the pushed commits would block"
else
echo "gitleaks: no secrets in the pushed commits"
fi