|
| 1 | +name: Gitleaks |
| 2 | + |
| 3 | +# Secret-scanning guard. Runs the same gitleaks rules as the pre-commit hook |
| 4 | +# (.pre-commit-config.yaml / .gitleaks.toml) in CI so a hardcoded key cannot |
| 5 | +# land even if a contributor has not installed the hook. Complements GitHub's |
| 6 | +# native push-protection / secret-scanning. |
| 7 | + |
| 8 | +on: |
| 9 | + pull_request: |
| 10 | + types: [opened, synchronize, reopened] |
| 11 | + push: |
| 12 | + branches: [main] |
| 13 | + |
| 14 | +# Least privilege: gitleaks only needs to read the checked-out tree. |
| 15 | +permissions: |
| 16 | + contents: read |
| 17 | + |
| 18 | +jobs: |
| 19 | + gitleaks: |
| 20 | + name: Scan for hardcoded secrets |
| 21 | + runs-on: ubuntu-latest |
| 22 | + steps: |
| 23 | + - name: Checkout |
| 24 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 25 | + with: |
| 26 | + # Full history so the commit range below can be diffed. |
| 27 | + fetch-depth: 0 |
| 28 | + |
| 29 | + - name: Run gitleaks (scan PR/push delta only) |
| 30 | + env: |
| 31 | + # Keep in lockstep with the rev in .pre-commit-config.yaml. |
| 32 | + GITLEAKS_VERSION: 8.21.2 |
| 33 | + BASE_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.event.before }} |
| 34 | + HEAD_SHA: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }} |
| 35 | + run: | |
| 36 | + set -euo pipefail |
| 37 | + curl -sSfL "https://github.com/gitleaks/gitleaks/releases/download/v${GITLEAKS_VERSION}/gitleaks_${GITLEAKS_VERSION}_linux_x64.tar.gz" -o /tmp/gitleaks.tar.gz |
| 38 | + tar -xzf /tmp/gitleaks.tar.gz -C /tmp gitleaks |
| 39 | + chmod +x /tmp/gitleaks |
| 40 | + # Scan ONLY the commits introduced by this PR/push. A full-tree scan |
| 41 | + # would re-report the ~600 placeholder keys in *.env.example / test |
| 42 | + # fixtures plus the already-revoked pre-rotation keys in git history, |
| 43 | + # failing every run. This guard's job is to stop NEW secrets from |
| 44 | + # entering the tree. --redact keeps any match value out of the log. |
| 45 | + ZERO="0000000000000000000000000000000000000000" |
| 46 | + if [ -z "${BASE_SHA}" ] || [ "${BASE_SHA}" = "${ZERO}" ]; then |
| 47 | + # First push to a new branch / no resolvable base: scan the tip commit. |
| 48 | + LOG_OPTS="-1 ${HEAD_SHA}" |
| 49 | + else |
| 50 | + LOG_OPTS="${BASE_SHA}..${HEAD_SHA}" |
| 51 | + fi |
| 52 | + echo "Scanning commit range: ${LOG_OPTS}" |
| 53 | + /tmp/gitleaks detect \ |
| 54 | + --source . \ |
| 55 | + --log-opts="${LOG_OPTS}" \ |
| 56 | + --config .gitleaks.toml \ |
| 57 | + --redact \ |
| 58 | + --verbose \ |
| 59 | + --no-banner |
0 commit comments