Sub topic 2
How secret scanning works
This chapter explains what secret scanning looks at, when it runs, and what it produces. It assumes you know basic Git (commits, branches, history), can find repository settings on GitHub, and understand what an API key or token is. You also need a terminal with the GitHub CLI (gh) for the one example near the end.
What secret scanning is
Secret scanning is a GitHub service that searches repository content for strings that look like credentials. When it finds one, it creates an alert for people who can manage the repository's security. It is a detection tool. It reports leaks that have already been pushed, and it does not stop the push. Blocking a push before the content lands is the job of push protection, which a later chapter covers.
Two points shape everything else in this chapter:
- Scanning is based on patterns. A pattern describes what a credential from one provider or service looks like. The service finds text that matches a pattern, and it does not understand what the code does.
- Scanning covers history, not just the latest version of a file. A secret that you deleted in a later commit is still in Git history, so it is still found and still dangerous.
Where it looks
Secret scanning does not only read the files on your default branch. It examines:
- The full Git history across all branches, including commits that are no longer part of the latest file contents.
- Content outside the code, such as issue descriptions and comments, pull request titles, descriptions and comments, discussions, and wikis. Contributors paste tokens into these places by accident, often while pasting a log or an error message.
The exact list of covered locations changes over time. Check the GitHub documentation for the current list before you assume a given location is covered.
Secret scanning does not see anything that never reaches GitHub. A token in a file on your laptop, in a local commit you have not pushed, or in a private chat message is invisible to it.
When it runs
Scanning happens at three moments.
- When you turn the feature on. GitHub scans the existing history of the repository. This backfill is why enabling the feature on an old repository can produce many alerts at once.
- On new content. After each push, GitHub scans the new commits. Scanning is asynchronous, so an alert appears shortly after the push (usually within minutes) and not during it.
- When patterns change. If GitHub adds or updates a provider pattern, or you add a custom pattern, existing content is scanned again for that pattern.
The consequence of the second point is easy to miss. Because detection happens after the push, the secret is already on the remote when the alert appears. Anyone who can read the repository could have copied it already. For a public repository, assume it was copied within seconds.
The diagram shows the flow from a push to an alert.
How matching works
Each pattern is, in essence, a regular expression plus some context rules. A provider pattern is usually specific. For example, a token format with a fixed prefix and a fixed length is rarely matched by accident. That specificity is why provider alerts tend to be high-confidence.
GitHub also detects some generic secrets, such as passwords embedded in code, that have no fixed format. These rely on broader analysis and produce more false positives. GitHub keeps them separate from provider patterns and, in the interface, generally shows them under a distinct category so you can treat them differently.
You can add your own patterns for internal credentials. A later chapter covers custom patterns. Provider patterns are covered in the next chapter, along with the list of what GitHub recognizes.
One alert per secret, many locations
An alert is tied to the secret, not to each place it appears. If the same token is committed in three files across two branches, GitHub shows a single alert with several locations. When someone commits the same value again, the new location is added to the existing alert and no new alert is created. This keeps the alert list manageable, and it also means that fixing one location does not close the alert. The alert stays open until you resolve it.
Each alert records:
- A number, unique within the repository.
- The secret type, such as the name of the provider token.
- The secret value, visible to people allowed to view alerts.
- The locations where it was found.
- A state, either
openorresolved. A resolved alert also carries a resolution reason. - Whether it was found by a push that bypassed push protection.
- For supported providers, a validity status (active, inactive, or unknown) when validity checks are enabled.
Who can see the alerts
Alerts contain the secret itself, so access is restricted. Repository administrators, organization owners, security managers, and people given a custom role with the right permission can view them. Ordinary contributors cannot. When you write a process around alerts, keep this in mind: the person who pushed the secret may not be able to see the alert about it.
Availability
Secret scanning is available for free on public repositories. For private and internal repositories in an organization, it is part of GitHub's paid security offering (GitHub Secret Protection, formerly part of GitHub Advanced Security). Plan names and bundling have changed before, so confirm the current terms for your account type.
In public repositories there is an additional step. When the matched token comes from a participating provider, GitHub reports the match to that provider, who can revoke the token automatically. The partner program chapter covers that path.
Seeing what scanning produces
Once the feature is on, you can list open alerts from the terminal. This command prints the alert number, the secret type, the validity status and whether push protection was bypassed:
gh api 'repos/OWNER/REPO/secret-scanning/alerts?state=open' \
--jq '.[] | {number, secret_type_display_name, validity, push_protection_bypassed}'Replace OWNER/REPO with your repository. The caller needs permission to view alerts, so the command returns an error for an account without it. A repository with no open alerts returns nothing.
Limits and common mistakes
Knowing what the scanner cannot do prevents most wrong assumptions.
- It is not preventive. Detection follows the push. If you want blocking, you need push protection.
- It does not remove anything. The alert points at the leak. The secret remains in history until you act. Deleting the file in a new commit changes nothing for the history.
- Closing an alert does not revoke a credential. An alert is a record of a finding. Resolving it with the wrong reason makes your reporting inaccurate and may hide a live credential.
- Forks and clones multiply the exposure. Scanning one repository does not tell you about copies elsewhere.
- False positives happen. Test fixtures and documentation often contain dummy tokens that match a pattern. Path exclusions, covered in a later chapter, reduce this noise.
- Pattern coverage is finite. Unknown formats are missed.
Summary of the working model
Secret scanning is a pattern-matching process over everything GitHub stores for a repository: history, branches, and discussion text. It runs when you enable it, after pushes, and when patterns change. It records findings as one alert per secret with all of its locations, and it shows those alerts only to people with security access. It reports a problem that already exists, so a good response depends on you: rotate the credential, then close the alert with an accurate reason.
Exercise
A developer commits an API key on a feature branch, notices the mistake, and pushes a second commit that deletes the file. No one has enabled secret scanning yet. A week later an administrator enables it. Will an alert be created, and does deleting the file mean the alert can be resolved as fixed? Explain.
Show answer
Yes, an alert will be created. When secret scanning is enabled it scans the full Git history of all branches, and the key still exists in the first commit even though a later commit removed the file. The deletion does not make the secret safe, because anyone with access could already have copied it. The alert should stay open until the key is revoked at its provider and rotated. Only then should it be resolved, with a reason such as "revoked".
Exercise
Two commits on different branches contain the same token. How many alerts does GitHub create for it, and what happens if the same token is committed a third time next week while the alert is still open?
Show answer
GitHub creates one alert for the token, listing both locations. The third commit adds another location to that same alert and does not create a new one.