Skip to content

Commit 2f0561e

Browse files
ci: fix Gitleaks — PR mode scans only PR commits, not full history
The baseline approach (scanning full history + skipping known findings) was fragile: CI's merge-checkout includes commits that differ from the local clone used to generate the baseline, causing 2 unmatched fingerprints. Better architecture: - PR mode: `gitleaks detect --log-opts="base_sha..HEAD"` scans ONLY the commits in the PR. Historical secrets are naturally ignored since they were introduced in older commits not in the PR's range. - Push-to-main mode: `--log-opts="before..HEAD"` scans only the pushed commits. Falls back to baseline if the before-SHA is unavailable (e.g. new branch push). This matches how professional secret scanning works: check what's new, not what's old. The .gitleaksbaseline is kept for the push-to-main fallback path. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b4cc97c commit 2f0561e

1 file changed

Lines changed: 38 additions & 13 deletions

File tree

.github/workflows/secret-scan.yml

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ name: Secret Scanning
33
# =============================================================================
44
# Gitleaks Secret Scanning Workflow
55
#
6-
# Scans every push to main and every pull request for leaked secrets,
7-
# API keys, tokens, and internal URLs. Uses the project-level
8-
# .gitleaks.toml for RunAnywhere-specific patterns on top of gitleaks'
9-
# built-in detectors.
6+
# Scans for leaked secrets, API keys, tokens, and internal URLs.
7+
# Uses the project-level .gitleaks.toml for RunAnywhere-specific patterns.
108
#
11-
# Uses the gitleaks CLI directly (MIT-licensed, free for all) instead of
12-
# gitleaks-action@v2 which requires a paid license for organization
13-
# accounts.
9+
# Strategy:
10+
# - Pull requests: scan ONLY the PR's commits (not full history). Catches
11+
# new secrets without tripping on the 49 known historical findings.
12+
# - Push to main: scan the pushed commits with a baseline file so known
13+
# findings are skipped and only genuinely new leaks fail CI.
14+
#
15+
# Uses gitleaks CLI directly (MIT-licensed, free) instead of
16+
# gitleaks-action@v2 which requires a paid license for organizations.
1417
# =============================================================================
1518

1619
on:
@@ -40,10 +43,32 @@ jobs:
4043
gitleaks version
4144
4245
- name: Run Gitleaks
46+
env:
47+
EVENT_NAME: ${{ github.event_name }}
4348
run: |
44-
ARGS="--source . --redact --verbose"
45-
# Use project config if present
46-
[ -f .gitleaks.toml ] && ARGS="$ARGS --config .gitleaks.toml"
47-
# Use baseline file to ignore known historical findings (only flag NEW leaks)
48-
[ -f .gitleaksbaseline ] && ARGS="$ARGS --baseline-path .gitleaksbaseline"
49-
gitleaks detect $ARGS
49+
CONFIG_ARG=""
50+
[ -f .gitleaks.toml ] && CONFIG_ARG="--config .gitleaks.toml"
51+
52+
if [ "$EVENT_NAME" = "pull_request" ]; then
53+
# PR mode: only scan the commits in this PR, not full history.
54+
# This naturally ignores all historical secrets without needing
55+
# a baseline file (which can drift between local and CI clones).
56+
echo "PR mode: scanning only PR commits"
57+
gitleaks detect --source . $CONFIG_ARG --redact --verbose \
58+
--log-opts="${{ github.event.pull_request.base.sha }}..HEAD"
59+
else
60+
# Push-to-main mode: scan the pushed commits. Fall back to
61+
# baseline-based full scan if the before SHA is unavailable.
62+
echo "Push mode: scanning pushed commits"
63+
BEFORE="${{ github.event.before }}"
64+
if [ -n "$BEFORE" ] && [ "$BEFORE" != "0000000000000000000000000000000000000000" ]; then
65+
gitleaks detect --source . $CONFIG_ARG --redact --verbose \
66+
--log-opts="${BEFORE}..HEAD"
67+
elif [ -f .gitleaksbaseline ]; then
68+
gitleaks detect --source . $CONFIG_ARG --redact --verbose \
69+
--baseline-path .gitleaksbaseline
70+
else
71+
echo "::warning::No baseline and no before-SHA; running full scan"
72+
gitleaks detect --source . $CONFIG_ARG --redact --verbose
73+
fi
74+
fi

0 commit comments

Comments
 (0)