Sub topic 11
Scanning for Secrets Before and After They Reach the Repository
This chapter assumes you know basic Git, can read a GitHub Actions workflow, and understand that a committed token is exposed to everyone who can read the repository. Secret scanning is the safety net for the leak paths covered earlier. It finds credentials that slipped past your habits.
Why scanning needs two layers
Git keeps history. Deleting a secret in a later commit does not remove it from earlier commits, forks, or clones. So you want to catch a secret before the push. You also want to detect any that got through, including old ones in history.
Layer 1: before the push
A pre-commit hook scans staged changes on the developer's machine. It is the cheapest place to catch a mistake, because nothing has left the laptop.
The example below uses the open-source tool gitleaks through the pre-commit framework. Save it as .pre-commit-config.yaml:
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.18.4
hooks:
- id: gitleaksThen install the hook once per clone:
pip install pre-commit
pre-commit installHooks run on the developer's machine, so a developer can skip them with git commit --no-verify. Treat hooks as a convenience, not as enforcement.
Enforcement belongs on the server. Many hosts offer push protection, which rejects a push that contains a known secret pattern. On GitHub this is a repository setting that is part of secret scanning. Turn it on for every repository you can.
Layer 2: in CI
A CI scan checks every pull request and cannot be skipped by a local flag. This workflow runs gitleaks on each pull request and push:
name: secret-scan
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
jobs:
gitleaks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run gitleaks
run: |
docker run --rm -v "$PWD:/repo" \
zricethezav/gitleaks:v8.18.4 \
detect --source /repo --redact --verboseTwo details matter here:
fetch-depth: 0fetches the full history. The default shallow checkout contains only one commit, so the scanner would miss anything older.--redacthides the secret value in the scanner's own output. Without it, the scan log would print the very secret you are trying to protect.
Layer 3: history and existing leaks
Your repository may already contain secrets from years ago. A full-history scan finds them. Run one when you adopt a scanner, then run it on a schedule, because detection rules improve over time.
gitleaks detect --source . --redact --report-path findings.jsonReview the report, and treat every real finding as a leaked secret. Do not rewrite history and consider the matter closed. Revoke the credential first. The next chapters cover rotation and incident response in detail.
Tuning: false positives and misses
Scanners match patterns and measure randomness (entropy), so they make mistakes in both directions.
| Problem | Cause | Response |
|---|---|---|
| False positive | A test fixture or example key looks like a real one | Add a narrow allowlist entry for that path or fingerprint |
| False negative | A custom token format has no built-in rule | Add a custom rule with a regex |
| Alert fatigue | Broad allowlists or noisy rules | Keep allowlists small and review them in pull requests |
Common errors
- Scanning only the latest commit and missing history.
- Allowing an entire directory instead of one fingerprint.
- Printing unredacted findings into CI logs.
- Assuming a removed secret is safe. Once pushed, assume it is compromised.
Exercise
Your CI scan passes, but a teammate says a token was committed and deleted six months ago. Why might the scan miss it, and what should you do about the token?
Show answer
The checkout is probably shallow, or the scan only inspects the diff, so old commits are never read. Rerun with full history (fetch-depth: 0 or a local full-history scan). The token is exposed in history, forks, and clones whether or not the scanner finds it, so revoke and rotate it first.