CCyberwiki

Secret scanning

Secret scanning
TypeSecurity testing method
Also known asSecrets detection
ExaminesFiles, the full Git history, and text such as issues, pull requests and wikis
DetectsAPI keys, tokens, passwords, private keys, connection strings
Weakness foundCWE-798: Use of Hard-coded Credentials
Example toolsGitHub secret scanning, Gitleaks (MIT), TruffleHog (AGPL-3.0), detect-secrets
Related methodsSAST, SCA, DAST

Secret scanning searches source code, its git history and related text for credentials: API keys, tokens, passwords and private keys. Like Static application security testing, it reads code without running it, though it hunts for values where SAST hunts for flawed logic. It also reads every past version of each file, because a secret deleted in a new commit stays readable in older ones.

Detection methods

A key written into a file is a hard-coded credential, CWE-798.1 Most scanners find one by matching text against a list of rules, each a regular expression for one secret type. The open-source scanner Gitleaks lets each rule add keywords, which a quick string comparison looks for before the expression runs, and a minimum Shannon entropy, a measure of how random the matched characters are, below which a match is not counted as a secret.2 Each finding names the rule, file, line and commit, with a fingerprint that identifies that one secret.

Rules are most precise for credentials whose format was designed to be found. GitHub asks service providers that join its partner program for a regular expression, a test account and an endpoint, and recommends keys with "a uniquely defined prefix", a random body and a 32-bit checksum.3 A password chosen by a person has no such shape, and the consistency that keeps false positives low also means a scanner tuned to it can miss a human-made password.4 GitHub adds generic patterns for private keys and connection strings, custom patterns that an organization writes, and a model that looks for unstructured secrets such as passwords.5

Some tools then test each match against the service it belongs to. TruffleHog classifies over 800 secret types and gives each finding one of three statuses: verified (the service accepted it), unverified (found but not confirmed) or unknown (the check failed, for example on a network error). For an AWS key it calls the AWS GetCallerIdentity API.6 GitHub's validity checks contact the issuing service in the same way to see whether a found credential has been revoked.5

Where scanning runs

A scan can run at four points. The earliest is a pre-commit hook, a script Git runs before it records a commit, which stops a secret while it is still on the developer's machine and is the recommended place to start detecting.4 At the code host, a push check rejects it. In the build pipeline, each change is checked before it is merged. A history scan covers everything already committed.

On GitHub, secret scanning reads the entire history of every branch, plus issues, pull request text, Discussions, wikis and secret gists, and rescans when new secret types are added. It runs free on public repositories; private repositories of an organization need the paid GitHub Secret Protection.5 When a partner's key turns up in public, GitHub sends it to that provider, which can revoke it; such matches go to the provider and do not appear among the repository's alerts.35 GitHub counted more than 39 million leaked secrets on its platform in 2024.7

Push protection rejects a push that contains a detected secret, from the command line, the web editor, file uploads and the REST API. A per-account version is on by default for pushes to public repositories; the per-repository version is off until an administrator enables it. By default anyone with write access can bypass a block by giving a reason. "It's used in tests" and "It's a false positive" close the resulting alert, "I'll fix it later" leaves it open, and every bypass is written to the audit log.8

Running a scanner

Gitleaks has three modes: git, which reads the patches produced by git log -p, dir for plain files, and stdin. It exits with code 1 when it finds a leak, which fails a build step. A first run on an old repository can be saved as a baseline, so that later runs report only new findings:2

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

A known false positive is silenced with its fingerprint in a .gitleaksignore file, or with a gitleaks:allow comment on the line. In a pipeline, TruffleHog can limit the scan to the commits a branch adds since main and, with --fail, exits with code 183 when it finds a valid credential:6

trufflehog git file://. --since-commit main --branch feature-1 --results=verified,unknown --fail

Reading the results

A verified finding is a live credential. An unverified one may be revoked, expired, a test value, or a key that was not checked. An organization that uses one standard test secret per type can recognise its test values at a glance.4 A clean result covers only what the rules describe: GitHub's default patterns miss credentials in a custom format,9 and running more than one detection tool, then merging the results, narrows what any single rule set misses.4

A real finding is handled as an incident. Rotation comes first: once the old key is revoked it no longer grants access, and rewriting history may then be unnecessary.104 A rewrite changes the hash of every later commit, and a collaborator who pushes from an older clone brings the secret back.10

Notable case

In 2022 Toyota disclosed that part of the source code of its T-Connect site, published on GitHub by a subcontractor, held an access key to a server storing email addresses and customer numbers. The key was readable from December 2017 until access to the repository was restricted on 15 September 2022, and covered 296,019 customers; Toyota changed the keys two days later.9

In SecFlash

SecFlash reports a credential in the code or its history as SF-SEC-702 and a key in an AI agent's configuration file as SF-SEC-718, both on the Security page. On the Cloud page, SF-CLD-604 shows a public GitHub repository with secret scanning off, and SF-CLD-605 a repository with push protection off.

See also

References

  1. back "CWE-798: Use of Hard-coded Credentials". Common Weakness Enumeration. MITRE.
  2. a b "Gitleaks" (README). GitHub repository gitleaks/gitleaks.
  3. a b "Secret scanning partner program". GitHub Docs. GitHub.
  4. a b c d e "Secrets Management Cheat Sheet". OWASP Cheat Sheet Series.
  5. a b c d "Secret scanning". GitHub Docs. GitHub.
  6. a b "TruffleHog" (README). GitHub repository trufflesecurity/trufflehog. Truffle Security.
  7. back Havens, Erin (1 April 2025). "GitHub found 39M secret leaks in 2024". The GitHub Blog.
  8. back "Push protection". GitHub Docs. GitHub.
  9. a b Toulas, Bill (10 October 2022). "Toyota discloses data leak after access key exposed on GitHub". BleepingComputer.
  10. a b "Removing sensitive data from a repository". GitHub Docs. GitHub.