How to scan repositories for exposed secrets and respond
Secret scanning looks for credentials accidentally committed to repositories. Learn to define an authorized scope, scan current changes and Git history, triage findings without exposing values, and revoke and replace a real leaked credential.
How to scan repositories for exposed secrets and respond
Secret scanning looks for credentials accidentally placed in code repositories and development workflows, including API keys, passwords, tokens, and private keys. It can prevent and detect leaks, but it cannot find every secret or make an exposed value safe again. Scan current files, Git history, and delivery pipelines; when a real credential is found, disable or replace it first.
This guide is for repositories you own or are explicitly authorized to scan. It does not explain how to probe credentials found in someone else’s public repository or submit a real token to a service to see whether it works.
What does a scanner detect?
A secret is sensitive information a system uses to verify identity or grant access. Examples include API keys, database connection strings, cloud credentials, access tokens, SSH private keys, certificates, and signing keys. Removing a value from the current file does not erase copies in earlier commits, clones, build logs, forks, or packages.
Scanners combine known token formats, identifiers, keywords, rules, and sometimes provider verification to find candidates. A finding means “review this,” not “a breach is confirmed.” Test strings and documentation examples can trigger false positives; new formats, obfuscation, split values, or unsupported providers can be missed. GitHub documents that its coverage depends on supported patterns, configuration, and token types. Some credential pairs are detected only when both parts occur in the same file.
Separate scanning from secret management
Scanning detects exposed values in code and development artifacts. A secrets manager controls which approved applications can obtain a value and records who accessed it. Repository scanning alone cannot tell you who owns a credential, which service uses it, what permissions it has, or how to revoke it. Track each secret’s consumer and owner.
Apply checks at complementary points. An editor or pre-commit hook gives a developer fast feedback. CI checks changes before review or merge. A hosting platform’s push protection may stop supported values from reaching a remote repository. Scheduled scans of repositories and Git history help find existing exposures. Incident-response scans help bound an active leak. One layer does not replace the others.
Define scope and scan safely
Before scanning, record which organization-owned repositories, default and long-lived branches, commit history, forks or mirrors, submodules, CI configuration, and build artifacts are included. Identify owners, permission, timing, result viewers, and an incident contact. History scans may find values thought to have been removed, so protect reports as if they contain secrets.
**Choose detection coverage.** List provider patterns, generic rules, organization-specific formats, and standard test strings. Check the tool’s official documentation for supported file types, branches, history, and tokens.
**Scan changes and history separately.** A change scan aims to stop new leaks before merge; a history scan looks for older exposure. Gitleaks documents `git`, `dir`, and `stdin` modes, Git patch-history scanning, and using a report as a baseline to distinguish old findings from new ones. A baseline can hide known findings, so review and approve it and ensure those items do not reappear in later scans.
**Add checks at safe stages.** Use local hooks for fast feedback, but they can be bypassed and should not be the only control. Configure CI checks before merge and protect their output. Confirm that a host’s push protection is enabled for your repository, secret type, and plan; coverage and licensing differ by product.
**Record findings without recording secret values.** Track tool and version, repository, branch, commit location, rule type, time, review status, owner, and action. Never copy the detected value into a report or ticket. Use standard fake test strings to check that logs and CI output mask values.
Triage false positives and real exposure
First decide whether a candidate is a placeholder, documentation example, public identifier, or actual credential. Review the path, commit, line, and pattern type, but do not paste the value into chat, an issue, or logs. When the provider supports it, use its account security records or credential console to check ownership and status. Do not test a token by putting it into an API request; that can expose an active credential further.
For a false positive, limit an exception to the necessary path and test data, and record its reason and approver. A broad allowlist for an entire value or repository can hide real leaks. Rescan to confirm a standard fake test string is excluded only by the intended rule.
If the value may be real, start leak response without waiting for a scanner score or remote-push block. Deleting a line or rewriting a commit does not invalidate copies of the credential.
Respond to a real leak
**Restrict access.** Notify the credential owner and service owner through an approved private channel. Share only the repository, commit, exposure scope, and time needed; do not share the value.
**Assess impact.** Identify the credential type, permissions, connected service, exposure period, access logs, repository visibility, and likely copies. Do not investigate systems beyond your authorization.
**Revoke or replace.** Use the provider’s official process to revoke the old key or token. If needed, issue a least-privilege replacement and deploy it through an approved secrets manager. If both values must briefly coexist, document the transition and retirement time, then disable the old value as soon as possible.
**Verify service health.** Deploy the replacement in a limited scope and monitor normal operation. Review audit records for failed use of the old credential and unexpected access. If key rotation requires data re-encryption or other system changes, follow that service’s change plan.
**Clean copies and prevent recurrence.** Remove the value from the repository and, where required, clean history under organizational and hosting-platform procedures. Clones and caches may still retain copies, so history rewriting alone is not resolution. Improve pre-commit checks, CI gates, push protection, developer guidance, and least privilege, then rescan history.
**Close recovery and evidence.** Record deployment status, access-log review, revocation confirmation, exceptions, follow-up owner, and completion time. Keep evidence masked and location-based; do not retain the credential itself.
Example: a cloud API key committed by mistake
Suppose a developer adds a cloud API key to a configuration file and pushes it to a protected branch. If push protection supports that key type and is enabled, it may block the push. The developer should move the value to an approved secrets manager, leave only a safe configuration reference in the file, and rescan the change—not bypass the block to continue.
If the key was already pushed, remove it from the repository while revoking it, and investigate its permissions and service access records. Fix the deployment path and scan the full history. GitHub detection covers supported patterns and settings; older tokens or very large pushes can fall outside push-protection blocking. A push that was not blocked is therefore not evidence that the key is safe.
Operational checklist
Did you define owned repositories, branches, history, artifacts, and result viewers?
Do local feedback, pre-merge CI, host push protection, and scheduled history scans each have a clear role?
Are tool versions, rules, false-positive exceptions, and baselines reviewed and recorded?
Are plaintext values kept out of logs and tickets?
Is there a named owner for revocation, replacement, impact review, deployment verification, and history cleanup?
Are findings and evidence access-controlled and retained only as long as necessary?
References
OWASP Cheat Sheet Series, [Secrets Management Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html) — detection, lifecycle, least privilege, and response principles.
GitHub Docs, [About secret scanning alerts](https://docs.github.com/en/enterprise-cloud@latest/code-security/concepts/secret-security/about-alerts) — alert types and limits of generic-pattern detection.
GitHub Docs, [Push protection](https://docs.github.com/en/code-security/concepts/secret-security/push-protection) — blocking supported secrets and recording bypass alerts.
GitHub Docs, [Secret scanning detection scope](https://docs.github.com/en/code-security/reference/secret-security/secret-scanning-scope) — detection scope and blocking limitations.
Gitleaks, [README](https://github.com/gitleaks/gitleaks/blob/master/README.md) — Git history, directory, standard-input scans, and baselines.
Google Cloud, [Secret Manager best practices](https://docs.cloud.google.com/secret-manager/docs/best-practices) — least privilege, versioned deployment, reversible disablement, and rotation.