Skip to content

Commit 0ff7585

Browse files
authored
Merge pull request #15 from BrainCOGS/tabedzki/ci/gitleaks-pr-scan
ci: scan the commits of the PR with gitleaks
2 parents b93c4eb + 603b7ef commit 0ff7585

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env bash
2+
# Scan only the changes being introduced for secrets, not content that was already
3+
# scanned elsewhere. Run by the gitleaks hook in .pre-commit-config.yaml.
4+
#
5+
# - GITLEAKS_BASE set (CI): the commits in GITLEAKS_BASE..HEAD, plus the lines that
6+
# merge commits in that range introduced themselves (e.g. conflict resolutions),
7+
# not what they brought in from the merged branch. Set but empty: an error, so a
8+
# run without a base cannot pass without scanning anything.
9+
# - Merge commit in progress: what differs from the branch being merged in (this
10+
# branch's changes and the conflict resolutions), not everything merged in.
11+
# - Otherwise: the staged changes (the upstream hook's behavior).
12+
set -euo pipefail
13+
14+
# Lines of a combined diff (git show --cc) that are in none of the merge's parents,
15+
# with the file headers. With N parents those lines start with N "+".
16+
merge_introduced_lines() {
17+
local merge=$1 parents
18+
parents=$(($(git rev-list --parents -n 1 "$merge" | wc -w) - 1))
19+
git show --cc --format= "$merge" | awk -v n="$parents" '
20+
BEGIN { plus = sprintf("%" n "s", ""); gsub(/ /, "+", plus) }
21+
/^diff --cc / { print; next }
22+
/^\+\+\+ (b\/|\/dev\/null)/ { next }
23+
substr($0, 1, n) == plus { print }
24+
'
25+
}
26+
27+
if [[ -n "${GITLEAKS_BASE+set}" ]]; then
28+
if [[ -z "${GITLEAKS_BASE}" ]]; then
29+
echo "gitleaks: GITLEAKS_BASE is set but empty, refusing to pass without scanning" >&2
30+
exit 1
31+
elif git rev-parse --quiet --verify "${GITLEAKS_BASE}^{commit}" >/dev/null; then
32+
range=("${GITLEAKS_BASE}..HEAD")
33+
else
34+
# e.g. first push of a branch (all zeros) or a base that was force-pushed away
35+
echo "gitleaks: base ${GITLEAKS_BASE} not found, scanning the last commit only" >&2
36+
range=(-1 HEAD)
37+
fi
38+
39+
status=0
40+
echo "gitleaks: scanning commits ${range[*]}"
41+
gitleaks git --redact --verbose --log-opts="${range[*]}" || status=1
42+
43+
# git log -p (used by gitleaks git) shows no diff for merge commits
44+
for merge in $(git rev-list --merges "${range[@]}"); do
45+
echo "gitleaks: scanning the lines merge commit ${merge} introduced"
46+
merge_introduced_lines "$merge" | gitleaks stdin --redact --verbose || status=1
47+
done
48+
exit "$status"
49+
fi
50+
51+
if git rev-parse --quiet --verify MERGE_HEAD >/dev/null; then
52+
# Added lines only (with the file headers), removed lines are not introduced
53+
git diff --cached --unified=0 MERGE_HEAD | { grep -E '^(diff --git |\+)' || true; } | gitleaks stdin --redact --verbose
54+
exit
55+
fi
56+
57+
exec gitleaks git --pre-commit --redact --staged --verbose

‎.github/workflows/lint.yml‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@ jobs:
2121
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
2222
with:
2323
persist-credentials: false
24-
# the AI attribution check reads the commits since the base
24+
# gitleaks and the AI attribution check read the commits since the base
2525
fetch-depth: 0
2626

2727
- uses: astral-sh/setup-uv@c18668ad3cf93ea998bef934396af7bb5c839dc7 # v10.2.0
2828
with:
2929
enable-cache: true
3030

3131
- name: Run prek hooks
32+
env:
33+
# gitleaks scans staged changes, of which there are none here: it runs
34+
# in the next step on the commits of the PR instead
35+
SKIP: gitleaks
3236
run: uvx prek run --all-files --show-diff-on-failure
3337

3438
- name: Check AI attribution in the commits of the PR (or push)
@@ -37,3 +41,10 @@ jobs:
3741
# On PRs, HEAD is GitHub's synthetic merge commit; check up to the PR's tip.
3842
HEAD_SHA: ${{ github.event.pull_request.head.sha || github.sha }}
3943
run: python3 .github/scripts/check_ai_attribution.py --since "$BASE" --head "$HEAD_SHA"
44+
45+
- name: Scan the commits of the PR (or push) for secrets
46+
# a manual run has no base to scan from
47+
if: github.event_name != 'workflow_dispatch'
48+
env:
49+
GITLEAKS_BASE: ${{ github.event.pull_request.base.sha || github.event.before }}
50+
run: uvx prek run gitleaks --all-files --verbose

‎.pre-commit-config.yaml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ repos:
5353
- repo: https://github.com/gitleaks/gitleaks
5454
rev: v8.30.1
5555
hooks:
56+
# Scans only the changes being introduced: the staged changes (or a
57+
# merge's own changes) on commit, the commits of the PR in CI (lint workflow).
5658
- id: gitleaks
59+
entry: .github/scripts/gitleaks-changes.sh
5760

5861
- repo: local
5962
hooks:

0 commit comments

Comments
 (0)