All writing
10 min read

Secrets scanning in CI: stop API keys reaching your repo

Secrets scanning in CI works in three layers: a pre-commit hook, push protection and a scan of the full Git history. When a key gets through, rotate it first.

Asghar Ali · Chief Technology Officer, Kakushin.io LTD

Secrets scanning in CI works best as three layers, not one tool: a pre-commit hook that catches a key on the developer's machine, push protection that blocks it at the Git server, and a CI job that scans the full history of every branch and fails the build. Each layer can be skipped or can miss something, which is why you want all three. When a secret gets through anyway, rotate it first and clean the history second, if at all. A revoked key in your history is untidy. A live one is an incident.

This is for the small team, often building with AI assistants, with an API key or two in a private repository. It covers what each layer catches and misses, the tools by what they cover, a rollout order and a runbook for the day one gets through. A key in the JavaScript or app package you ship is a different problem: see your API keys are in the browser and the relay fix for mobile apps.

Why do API keys end up in the repository?

Because pasting the key into a file is the fastest way to make a feature work. GitGuardian, which sells secret scanning, counted 28.65 million new hardcoded secrets in public GitHub commits in 2025, with AI-service secrets at 1,275,105, up 81% on the year (State of Secrets Sprawl 2026, read on 27 November 2026). It also measured a 3.2% secret-leak rate on commits assisted by Claude Code against a 1.5% baseline for all public commits, and cautions that this is not simply a tool failure: people still decide what gets pushed.

A private repository is not the fix. The same report found internal repositories roughly six times more likely than public ones to contain hardcoded secrets, and more than 64% of the credentials it confirmed valid in 2022 were still valid when retested in January 2026. Everyone with clone access, every CI runner and every old laptop holds a copy of the history.

Diagram of three layers of secrets scanning between a developer and a live key. Layer 1, a pre-commit hook on the laptop, stops a key before the commit exists but can be skipped with no-verify or never installed. Layer 2, push protection on the Git server, blocks known key patterns for everyone but can be bypassed with a reason and does not look at old history. Layer 3, a CI scan of the full history on every pull request, fails the build and finds old keys, but by then the key has left the laptop. If a key gets through: rotate it, check the provider's logs, find every copy, then decide whether to clean the history.
Each layer is cheaper than the next one and easier to get around. The runbook at the bottom applies whichever layer catches it.

What does each layer catch, and what does it miss?

Layer 1: a pre-commit hook on the developer's machine

The cheapest catch is before the commit exists. OWASP's Secrets Management Cheat Sheet recommends detection at the developer level, including "via pre-commit hook" (read on 27 November 2026). gitleaks, TruffleHog and detect-secrets all document a pre-commit hook, so a commit containing a key fails on the laptop and never enters history.

What it misses: whatever the developer skips. Git's own hooks documentation says the pre-commit hook "can be bypassed with the --no-verify option", gitleaks documents SKIP=gitleaks for the same purpose, and a contractor who never ran the install step has no hook at all. It helps with honest mistakes; it is not a control.

Layer 2: push protection at the Git server

Push protection runs on the server, so it does not depend on anyone's setup. GitHub's push protection blocks a push that contains a supported secret, from the command line, the web interface or the REST API (read on 27 November 2026). Push protection for users is on by default for every GitHub.com account, but only stops pushes to public repositories. Push protection for repositories, which covers private ones, "is disabled by default" and needs GitHub Secret Protection. GitLab's secret push protection does the same job in the pre-receive hook, on the Ultimate tier (read on 27 November 2026).

What it misses: anything without a recognisable pattern, and anything someone pushes past. On GitHub, by default anyone with write access can bypass it by giving a reason; the bypass is logged and alerted. GitLab does not check binary files, files or diffs over 1 MiB, or the initial push that created the repository. Neither looks back at what is already in history.

Layer 3: a CI scan of the full history

The third layer is a job that scans every commit, not just the latest tree, and fails the build on a new finding. GitHub's secret scanning does this on the server: it scans "your entire Git history on all branches", plus issues, pull requests, discussions and wikis. It runs free and automatically on public repositories; private and internal repositories in an organisation need GitHub Secret Protection on GitHub Team or Enterprise Cloud (read on 27 November 2026).

Without that licence, a scanner in the pipeline does it. gitleaks' git mode reads the patches with git log -p, so the CI checkout needs the full history (fetch-depth: 0 in its GitHub Action's example), not a shallow clone that holds only the last commit. A baseline file records findings you have already dealt with, so the build fails only on new ones. TruffleHog has a CI mode that scans the commits between your default branch and the pull request branch and fails when it finds something.

What it misses: by the time CI sees a key, the key has left the laptop. If the branch was pushed, treat the key as exposed, private repository or not.

Which tool covers which layer?

Versions, dates and licences checked on the projects' GitHub pages on 27 November 2026. "Live check" means the tool can ask the provider whether a key still works.

ToolPre-commit hookBlocks the pushFull-history scanLive checkLicence and cost
GitHub secret scanning and push protectionNoYes: public repos by default, private with Secret ProtectionYes, all branches, plus issues and pull requestsOptional validity checksFree on public repos; private organisation repos need GitHub Secret Protection
GitLab secret push protectionNoYes, in the pre-receive hookPipeline secret detection, a separate jobNot covered hereUltimate tier
gitleaks v8.30.1 (21 March 2026)YesNoYes, git mode, with a baselineNoMIT; its GitHub Action needs a free licence key for organisation repos
Betterleaks v1.10.1 (12 November 2026)Not in its READMENoYes, git mode and GitHub or GitLab sourcesYes, optionalMIT
TruffleHog v3.100.1 (23 November 2026)YesNoYes, git, GitHub organisations, buckets, imagesYes, classifies over 800 secret typesAGPL-3.0
detect-secrets v1.5.0 (6 May 2024)YesNoNo: baseline plus new diffs, by designSome pluginsMIT

Two notes. The gitleaks README now says future releases "will be security patches only", and its original author maintains Betterleaks, so check both for a new setup. And OWASP suggests more than one detection tool with the results de-duplicated: cheap if your Git host scans on the server and one open-source scanner runs in CI.

What should a small team roll out first?

Start where nobody can skip it, and find out what is already there before you block anything new.

  1. Day one: scan the full history of every repository once. A fresh full clone of every repo, archived ones included, through a scanner from the table. Rotate every live key it finds first (the runbook below). Most teams skip this step, and it is where the old keys are.
  2. Turn on what the platform gives you. On GitHub, secret scanning and push protection on every public repository; for private ones, decide whether Secret Protection is worth it for your team. Ask everyone to confirm push protection for users is still on in their account.
  3. Week one: a required CI check on every pull request. Full history checked out, fail on a new finding, the baseline from day one for keys already rotated, and secrets redacted in the log.
  4. Week two: a pre-commit hook in the repository, installed by the same setup script that installs dependencies, so new joiners get it without asking.
  5. Then the habits: custom patterns for your own internal tokens, a weekly look at bypassed pushes and open alerts, and the one-page runbook below with a named owner.

Why this order: server-side controls work for everyone once switched on, and the history scan tells you whether you already have a problem. The hook comes last because it is the easiest to skip.

The pipeline holds secrets too. OWASP's cheat sheet asks for short-lived OpenID Connect tokens instead of static cloud keys in CI, and recommends keeping application runtime secrets out of the pipeline altogether. The Binance case shows what one deployment token on one machine can be worth.

What do you do when a secret gets through?

Rotate first, then clean. GitHub's guidance is to "rotate the affected credential immediately", and it calls removing the secret from history "time-intensive and often unnecessary if you've already revoked the credential". OWASP's incident-response section puts revocation and rotation before deletion, and asks for a record of who had access and when the key was used.

StepWhat to doDone when
1. RotateIssue a new key, deploy it, then revoke the old one at the providerThe old key fails when used
2. Check for useRead the provider's usage and audit logs for the whole exposure windowYou know whether it was used, and from where
3. Find every copyOther repositories, CI variables, logs, chat, tickets, wikisEvery copy is listed and the key is dead everywhere
4. Decide on historyRewrite only if the data itself is sensitive or the key cannot be revokedA written decision with the reason
5. Stop the repeatKey read from a secret store, a custom pattern, the CI check requiredThe same mistake fails the build

On step 3, GitGuardian reports that about 28% of incidents start entirely outside repositories, in tools such as Slack, Jira and Confluence. On step 4, GitHub's guide to removing sensitive data lists the costs of rewriting: changed commit hashes, broken diffs on closed pull requests, lost signatures, and recontamination from old clones. A force push does not reach existing clones or forks either. If you do rewrite, GitHub asks for git-filter-repo 2.47 or later with its --sensitive-data-removal flag (read on 27 November 2026).

What never works: deleting the file in a new commit and calling it fixed. The key is still in every earlier commit.

A worked example (illustrative)

An invented case, not a client: a four-person team building an AI support assistant, in a private GitHub repository owned by an organisation without Secret Protection.

A contractor pastes the AI provider's key into a config file to test a prompt and pushes a feature branch. No hook, no repository push protection, so the push goes through. The required CI check runs gitleaks over the branch with full history and fails the pull request, naming the file and commit, with the key redacted.

The lead follows the runbook: a new key with a spending limit goes into the secret store, the old one is revoked, and the provider's usage page shows no unexplained calls during the exposure. A copy in a stale CI variable is removed. The key is dead and the repository private, so the team does not rewrite history and writes down why. What sticks: the config reads the key from the environment, and the setup script installs the hook.

What secrets scanning cannot tell you

  • That there are no secrets in your code. Scanners match patterns. A human-made password or an internal token with no fixed format can pass.
  • That an unverified finding is harmless. Live checks exist only for providers that offer them. GitGuardian reports that 46% of critical secrets are missed when teams prioritise on validation alone.
  • Anything about keys in what you ship. A key injected at build time into a JavaScript bundle or a mobile binary never appears in the repository.
  • Who used a leaked key. Only the provider's logs can tell you that.

Frequently asked questions

Is a private repository safe enough for API keys?

No. Everyone with read access, every CI runner and every old clone has the full history. Keep keys in a secret store or the environment, and scan private repositories exactly as you would public ones.

Should we rewrite Git history after a leak?

Usually not, once the key is revoked. GitHub and OWASP both put revocation first, and rewriting changes every later commit hash and needs every clone cleaned. Rewrite when the data itself is sensitive, such as personal data or a credential that cannot be revoked, and plan it as a coordinated change.

Pre-commit hook or CI scan: which comes first?

The CI scan, because it runs for everyone and cannot be skipped from a laptop. The hook is kinder to the developer, since it catches the mistake before anything leaves the machine, so add it second. Push protection, where your platform offers it, sits between the two.

Does secrets scanning replace a secret manager?

No. Scanning finds keys in the wrong place; a secret manager gives them a right place, with access control, rotation and an audit trail.

If you want this checked

On the customer apps of a European national electricity utility I introduced a relay-proxy architecture that removed every client API key from the shipped binaries, and in a source-code audit the full-history scan is the step I would not skip. Secrets are the second check in a security review of an AI-built app. I can scan your repositories' history, help rotate what is live, and set up the three layers in your pipeline. The scope is on my hire page.