Secret scanning with gitleaks
Assume a token will slip in one day, and make sure a machine notices
The problem
Secrets end up in git through ordinary days, not carelessness:
Sys.setenv(GITHUB_PAT = "ghp_…")typed in the console, saved to.Rhistory, committed;- a project
.Renvironcommitted along with everything else ingit add -A; - a Quarto or R Markdown report that printed
Sys.getenv()while debugging, saved in_freeze/or the rendered HTML; - a database connection string pasted into a script “just for now”.
Once pushed, deleting the file does not remove the secret. It stays in history, in every clone and fork, and in GitHub’s references for PRs. We saw this ourselves: after deleting a test branch, its commit was still served by the GitHub API through the closed PR.
The first response to a leak is to rotate the secret, not to rewrite history.
What secret scanning does
A scanner reads diffs (and optionally the whole history) and matches them against a large built-in set of rules for known token formats. Examples are GitHub PATs (ghp_…), AWS keys, Google API keys and private-key headers. It also checks entropy, which catches random-looking strings. It then fails the build and points at the file and line.
gitleaks is the widely used open-source scanner. It is one Go binary with a GitHub Action and a pre-commit hook.
The web-repo way: gitleaks in CI
# .github/workflows/gitleaks.yml
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
pull-requests: write # lets it comment on the offending line
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0 # scan the full history, not just the tip
- uses: gitleaks/gitleaks-action@v3
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# GITLEAKS_LICENSE: ${{ secrets.GITLEAKS_LICENSE }} # organisation repos onlyLicensing, checked in the action’s README (v3): repos on a personal account need no licence. Repos owned by an organisation need a free licence key from gitleaks.io, stored as the GITLEAKS_LICENSE secret.
Proving it works. We planted a random, never-valid ghp_ token in a throwaway PR. gitleaks failed with rule github-pat and pointed at the exact file. A clean PR passed. Run the same test once in every new repo (test your guardrails).
Why not GitHub’s built-in secret scanning?
GitHub’s own secret scanning and push protection are free on public repositories. On private repositories they need the paid GitHub Secret Protection add-on, according to GitHub’s docs at the time of writing. Most internal R packages and Shiny apps are private, so gitleaks in CI is the no-cost baseline. If your organisation already pays for Secret Protection, turn on push protection as well: it blocks the push itself.
The R way
1. Keep the usual suspects out of git
usethis::git_vaccinate() # user-level gitignore: .Rhistory, .Rdata, .httr-oauth, .Rproj.user, .DS_Store, .quartogit_vaccinate() does not ignore .Renviron
We checked the list it writes (usethis 3.1.0). .Renviron isn’t on it, yet it’s the most likely file to hold a token (identity and environment isolation). Add it yourself, per project:
usethis::use_git_ignore(".Renviron")Better still, never type a secret in the console. Put it in the project .Renviron (usethis::edit_r_environ(scope = "project"), secrets management) and read it with Sys.getenv(). Then there’s nothing for .Rhistory to capture.
2. Add the CI scan
Copy templates/github/workflows/gitleaks.yml to .github/workflows/. It needs nothing from R, so it works for packages, Shiny apps, Quarto sites and analysis repos alike.
gitleaks.yml
# Drop into .github/workflows/. Scans the full git history of every PR and push.
# Personal-account repos need no licence. Organisation repos need a free
# GITLEAKS_LICENSE secret from gitleaks.io (uncomment the line below).
name: gitleaks
on:
pull_request:
push:
branches: [main]
permissions:
contents: read
pull-requests: write # lets the action comment on the offending line
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
fetch-depth: 0 # full history, not just the latest commit
- uses: gitleaks/gitleaks-action@v3
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# GITLEAKS_LICENSE: ${{ secrets.GITLEAKS_LICENSE }}3. Scan before the commit exists
CI catches a leak after it has reached GitHub. We saw that first-hand: our initial pre-commit hook only formatted code, so a planted test token passed pre-commit and was only caught once it reached CI.
To close this gap before commits leave your laptop:
- In npm/JavaScript repos: We use secretlint configured via
lint-staged("*" : ["secretlint --maskSecrets"]) with@secretlint/secretlint-rule-preset-recommendin.secretlintrc.json. In our probe, staging a fakeghp_token was blocked immediately before the commit existed ([GITHUB_TOKEN] found GitHub Token, masked). - In R repos: gitleaks ships a hook for the pre-commit framework, which R users often run via the
precommitpackage:
# .pre-commit-config.yaml
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.30.1
hooks:
- id: gitleaks # runs: gitleaks git --pre-commit --redact --stagedThe gitleaks hook builds gitleaks from source with Go (or gitleaks-docker uses a container). Whichever local hook you use, CI remains the gate.
False positives
Test fixtures sometimes contain things that look like secrets. Two escape hatches, both reviewable in a PR:
- an inline
#gitleaks:allowcomment on that line; - a
.gitleaksignorefile listing the finding’s fingerprint.
Never “fix” a finding by disabling the workflow.
If it fires on a real secret
- Rotate or revoke it at the source now. Assume it’s compromised.
- Replace the value wherever it’s legitimately used (
.Renviron, Connect env vars, Secret Manager). - Remove it from the code in a normal PR.
- Record what leaked, when, and what you rotated. In GxP terms this is a deviation with a CAPA.
Rewriting history is optional clean-up. It doesn’t undo the exposure.
Sources
Tools
- gitleaks: github.com/gitleaks/gitleaks
- gitleaks-action licensing (free for personal accounts, licence key for organisations): github.com/gitleaks/gitleaks-action
- secretlint: github.com/secretlint/secretlint
- pre-commit framework: pre-commit.com
- precommit (R): lorenzwalthert.github.io/precommit
GitHub’s built-in scanning
- About secret scanning: GitHub Docs
- About push protection: GitHub Docs
- GitHub Secret Protection pricing (free on public repos, paid add-on on private repos): GitHub Docs: Choosing GitHub Secret Protection
R package documentation
- usethis (
git_vaccinate(),use_git_ignore()): usethis.r-lib.org
- 2026-09-26: Closed the pre-commit gap identified during initial probe testing in our npm-based repo using secretlint (
secretlint --maskSecretsvialint-stagedwith@secretlint/secretlint-rule-preset-recommend). A re-probe with a fakeghp_token was blocked immediately before commit creation ([GITHUB_TOKEN] found GitHub Token, masked), while R repos continue using the gitleaks pre-commit hook. - 2026-09-26: Removed the end-of-post checklist. Marked project-dependent usethis block as .norun.
- 2026-09-27: Added a Sources section.
- 2026-09-27: Linked every template file and showed it on the page.