How to Scan for Secrets in CI
Keep credentials out of the repository by scanning every pull request for secret patterns and blocking merges on high-confidence findings.
On this page
Introduction
A committed cloud key or database password is one of the few security mistakes that survives deletion. Once a secret is pushed, it is in the git history, in every fork and clone, and in any backup. Deleting the file in a later commit does not remove it from history, so the only safe response is to rotate the credential. The way to avoid that scramble is to detect secrets before they are committed and to block the merge when a high-confidence finding appears. This article covers pattern-based scanning, entropy and custom rules, where enforcement belongs, and the triage that turns a finding into a rotated credential rather than just a deleted line.
Why Committed Secrets Are Serious
Automated attackers scan public repositories continuously and also scrape private ones after any leak. A key committed today can be used within minutes. Because git history is immutable in practice, rotating is the only reliable remediation: rewrite history and you invalidate every clone and break every developer’s local repository. The cost of a leaked secret is therefore proportional to how long it lives, which is why detection at the earliest possible point matters.
Secrets also arrive by accident through means that pattern scanning alone cannot prevent: a test fixture with a realistic key, a copied sample configuration with a real value, a debug log committed by mistake. Scanning catches these too, provided the rules are tuned so real findings are not drowned in false positives.
Pattern-Based Scanning
The first line of defense is matching known credential formats: cloud access key identifiers, source-host tokens, private key headers, chat-app tokens, and provider-specific patterns. Tools such as gitleaks and TruffleHog ship curated rule sets and scan both staged changes and full history. Run them over the whole history once to find what is already there, then over each pull request to stop new ones.
# Scan the entire git history once for existing secrets
gitleaks detect --source . --redact --report-format sarif --report-path gitleaks.sarif
# In CI: scan only the commits added by the pull request (fast and focused)
gitleaks detect --source . --log-opts="origin/main..HEAD" --redact --exit-code 1
# Pre-commit hook (runs before the commit is created)
gitleaks protect --staged --redact --exit-code 1
Entropy and Custom Rules
Pattern matching misses secrets in formats the tool does not know. Entropy checks fill part of that gap by flagging long, high-entropy strings that look like random credentials. Entropy alone produces false positives on hashes, base64 assets and test data, so combine it with context: a high-entropy string assigned to a variable named token or secret is far more suspicious than one in a minified asset.
Write custom rules for your own token formats. If your platform issues credentials with a recognizable prefix, a regex for that prefix catches them with high confidence. Encode the rule with a fingerprint of the known test values you allow, so the rule can be strict without failing on fixtures that intentionally look like secrets.
# Custom rule plus scoped allowlist
[[rules]]
id = "acme-service-token"
description = "ACME service token"
regex = '''acme_(live|test)_[0-9a-zA-Z]{32}'''
tags = ["acme", "token"]
[allowlist]
description = "Known test fixtures and placeholder docs"
paths = ['''tests/fixtures/.*''', '''docs/examples/.*''']
regexes = ['''acme_test_[0]{32}''']
Pre-Commit Versus CI Enforcement
A pre-commit hook gives the fastest feedback: the developer learns before the secret enters history and can fix it with no cleanup. But hooks can be skipped with –no-verify and are not installed on every machine, so they cannot be the only control. CI enforcement runs on the server, where it cannot be bypassed, and it is the gate that actually stops a secret from merging. Use both: the hook for speed, CI for guarantee.
Make the CI check blocking for high-confidence findings and non-blocking for low-confidence ones that need review. A single unblockable pipeline that fails on every entropy hit trains people to ignore it; a tiered policy keeps the signal trustworthy. Report the finding with the file, line and rule id, and never print the secret value itself, because CI logs are stored and shared.
Triage and Rotation, Not Just Deletion
A finding is not resolved by deleting the line. The credential was exposed and must be treated as compromised: rotate it at the provider, update the deployment configuration to use the new value, and only then remove the old value from the code. If the secret is in history, decide explicitly whether to rewrite history, which invalidates clones, or to accept that the old value remains and rely on rotation. Rotation is the part that actually reduces risk.
Move the credential to a secret manager and inject it at runtime through the environment or a mounted file, so the repository never needs to contain it. Use short-lived, scoped credentials where the provider supports them, because a credential that expires in an hour limits the damage of any future leak. This is the same principle behind never storing secrets in config files.
Handling False Positives
False positives erode trust faster than missed findings. Handle them with a scoped allowlist that references the exact path or pattern, not a global off switch. When a fixture must contain a realistic-looking value, give it an obvious placeholder prefix and allowlist that prefix. Review the allowlist periodically, because it is a place where real secrets can hide if it grows unchecked.
Measure the tool’s signal over time: how many findings were real, how many were false, and how long the pipeline spends scanning. If the false-positive rate is high, tighten the rules rather than disabling the tool. If the scan is slow on a large history, scan only the pull request range in CI and keep the full-history scan as a scheduled job.
Conclusion
Secret scanning is a cheap control with a large payoff: run a curated scanner over full history once and over every pull request thereafter, add custom rules for your own token formats, and keep entropy checks contextual. Block on high-confidence findings in CI and use a pre-commit hook for speed, but remember that detection alone is not remediation. Rotate every exposed credential, move secrets to a secret manager, and keep the allowlist small so the signal stays trustworthy. Then a leaked key becomes a rotated key with no incident, not a scramble through git history.
Sources: gitleaks, TruffleHog, GitHub secret scanning.
FAQ
- Is deleting the file enough after a secret is committed?
- No. The secret remains in git history and in every clone and fork, so it must be treated as compromised. Rotate the credential at the provider, update deployments to use the new value, and only then remove the old value from the code.
- Should the secret scan block CI or just warn?
- Block on high-confidence findings so a real secret cannot merge, and warn on low-confidence entropy hits that need review. A pipeline that blocks on every guess trains developers to bypass it, which loses the signal you are trying to protect.
Report a problem with this articleOpens a pre-filled issue on GitHub.