Security
R
CI/CD
Foundations series · part 6

Secret scanning with gitleaks

Assume a token will slip in one day, and make sure a machine notices

Scan commits and history for accidental credentials before code leaves your machine and on CI.
Published

2026-09-26

Modified

2026-09-27

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 .Renviron committed along with everything else in git 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 only

Licensing, 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, .quarto
Warninggit_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.

# 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-recommend in .secretlintrc.json. In our probe, staging a fake ghp_ 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 precommit package:
# .pre-commit-config.yaml
repos:
  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.30.1
    hooks:
      - id: gitleaks        # runs: gitleaks git --pre-commit --redact --staged

The 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:allow comment on that line;
  • a .gitleaksignore file listing the finding’s fingerprint.

Never “fix” a finding by disabling the workflow.

If it fires on a real secret

  1. Rotate or revoke it at the source now. Assume it’s compromised.
  2. Replace the value wherever it’s legitimately used (.Renviron, Connect env vars, Secret Manager).
  3. Remove it from the code in a normal PR.
  4. 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

GitHub’s built-in scanning

R package documentation

NoteUpdates
  • 2026-09-26: Closed the pre-commit gap identified during initial probe testing in our npm-based repo using secretlint (secretlint --maskSecrets via lint-staged with @secretlint/secretlint-rule-preset-recommend). A re-probe with a fake ghp_ 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.