All guides

Secret scanning: Gitleaks and GitHub push protection

Push protection blocks a push that contains a secret before it reaches the repository. Gitleaks scans earlier, in a pre-commit hook, and again in CI. GitHub secret scanning covers what is already stored.

Hands typing on a laptop

Gitleaks as a pre-commit hook

Gitleaks has three scanning commands: git scans Git repositories, dir scans directories or files, and stdin reads from standard input. Version 8.19.0 deprecated detect and protect. Those two are still available but hidden in the --help menu.

Create .pre-commit-config.yaml at the root of the repository:

YAML

repos:
  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.30.1
    hooks:
      - id: gitleaks

Shell

pre-commit autoupdate
pre-commit install

pre-commit autoupdate updates the configuration to the latest version of each repository, and pre-commit install installs the hook. v8.30.1 is the newest Gitleaks release on GitHub today.

The README notes that prepending SKIP=gitleaks to the commit command skips the hook, so scan again in CI.

The same README carries a notice from the author: Gitleaks is feature complete, no new features will be merged, and future releases will contain security patches only. It adds that the author is shifting focus to a project named Betterleaks.

Gitleaks in CI

This workflow comes from the README of gitleaks/gitleaks-action, without its manual and scheduled triggers and with actions/checkout at v7:

YAML

name: gitleaks
on:
  pull_request:
  push:
jobs:
  scan:
    name: gitleaks
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          fetch-depth: 0
      - uses: gitleaks/gitleaks-action@v3
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITLEAKS_LICENSE: ${{ secrets.GITLEAKS_LICENSE }}
  • fetch-depth: 0 fetches all history for all branches and tags.
  • GITLEAKS_LICENSE is required for repositories that belong to an organization account. The README describes the license key as free. Repositories that belong to a personal account need no key.
  • Since v2.0.0 the action is no longer under the MIT license. It has its own license file.

Outside the action, run the binary or the ghcr.io/gitleaks/gitleaks image:

Shell

gitleaks git -v
gitleaks dir -v path_to_directory_or_file

The exit code when leaks have been encountered is 1 by default. The --redact flag redacts secrets from logs and standard output.

GitHub secret scanning

Secret scanning scans the entire Git history on all branches of a repository for hardcoded credentials. GitHub also scans issues, pull requests, GitHub Discussions, wikis and secret gists.

  • Public repositories. Secret scanning runs automatically for free.
  • Organization-owned private and internal repositories. These get it when GitHub Secret Protection is enabled, on GitHub Team or on GitHub Enterprise Cloud.

GitHub Secret Protection is a paid product, and buying it requires a GitHub Team plan or a GitHub Enterprise plan.

Push protection

Push protection stops a push that contains a secret before the secret reaches the repository. The push protection page describes two forms:

  • Push protection for users is enabled by default. It stops you from pushing secrets to public repositories on GitHub.
  • Push protection for repositories is disabled by default. Four roles can turn it on: repository administrator, organization owner, security manager and enterprise owner. The same page says it requires GitHub Secret Protection to be enabled.

Two GitHub pages differ on that last point. The feature table on "About GitHub Advanced Security" lists push protection as available for a public repository without GitHub Secret Protection.

By default, write access to the repository is enough to bypass a block, and the person gives one of these reasons:

  • "It's used in tests" creates a closed alert, resolved as "used in tests".
  • "It's a false positive" creates a closed alert, resolved as "false positive".
  • "I'll fix it later" creates an open alert.

What to do with a finding

  1. The push was blocked and the secret is in the latest commit. Remove the secret from the code, run git commit --amend --all, then git push.
  2. The secret is in an earlier commit. Find the earliest commit that contains it with git log, start git rebase -i <COMMIT-ID>~1, and mark that commit to edit. Remove the secret, then run git add ., git commit --amend, git rebase --continue and git push.
  3. The secret reached the repository. GitHub's rule: treat a secret as compromised from the moment it is committed to a repository. It is not sufficient to remove the secret from the codebase. Revoke it, update the services that use it, and check the security logs for unauthorized activity.
  4. The alert stays open. Secret scanning does not automatically close an alert when the token is removed from the repository. Close it with "Close as", and mark it "Revoked" once that is true.

Baselines and allow lists

Gitleaks ignores any old findings that are present in a baseline, and a baseline can be any Gitleaks report:

Shell

gitleaks git --report-path gitleaks-report.json
gitleaks git --baseline-path gitleaks-report.json --report-path findings.json

The second report contains only new issues. GitHub's rule still applies to the secrets in the baseline: they were committed, so treat them as compromised and rotate them.

Gitleaks has three ways to ignore a finding:

  • A gitleaks:allow comment on the line tells Gitleaks to ignore that secret.
  • A fingerprint added to a .gitleaksignore file at the root of the repository ignores that specific finding. The README marks this feature as experimental.
  • An allow list in the configuration file skips commits, paths or patterns:

TOML

[extend]
useDefault = true

[[allowlists]]
description = "global allow list"
paths = ['''gitleaks\.toml''']

The path in this example comes from the README, so put your own there. [[allowlists]] replaced [allowlist] in v8.25.0.

Where to start

  1. Check whether push protection is enabled on your repositories and on your own account.
  2. Add the Gitleaks job to CI with a baseline, so that only new findings are reported.
  3. Add the pre-commit hook, and ask each collaborator to set it up.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides