Sub topic 2
How secret scanning works
This chapter explains what secret scanning does, what it scans, and how it relates to push protection. It assumes you know basic Git (commits, pushes, branches, history), can use repository settings on GitHub, and know what API keys and tokens are.
What secret scanning does
Secret scanning is a GitHub feature that looks for credentials that have been committed to a repository. A credential can be an API key, an access token, a password, or a private key. When the scanner finds a match, it creates an alert that repository admins and other authorized people can review.
Secret scanning is a detection tool. It runs after content reaches GitHub, so it tells you about a secret that is already on the remote. Push protection is the preventive counterpart. It checks a push before the content is accepted. The two features use the same detection patterns, so they complement each other. Push protection reduces new leaks, and secret scanning finds the ones that still get through or that were committed before you enabled protection.
What gets scanned
Secret scanning does not look only at the latest version of your files. It scans:
- The full Git history, across all branches.
- Newly pushed commits.
- Content outside the code itself, such as issue descriptions and comments, pull request titles, descriptions and comments, and discussions.
The history scan matters because a secret you deleted in a later commit is still stored in earlier commits. Anyone who can read the repository can retrieve it. Removing a line from the current file does not undo the leak.
The detection flow
When content arrives, GitHub compares it against a set of patterns. Each pattern describes the shape of a credential, such as a fixed prefix followed by a run of characters. A match creates an alert. Some matches trigger further steps, which later chapters cover in detail.
Where scanning applies
Secret scanning is available for different repository types:
| Repository type | Availability |
|---|---|
| Public repositories | Free, and GitHub scans them for supported provider patterns |
| Private and internal repositories | Available when your plan includes GitHub Secret Protection, and an admin must enable it |
Plan names and packaging change over time. Check the current GitHub documentation for what your plan includes.
What an alert tells you
An alert records the secret type, the location where it was found, and the commit that introduced it. For some secret types it also shows whether the credential is still active. A finding is only the start of the work. The credential must still be revoked at the provider, and the alert must be closed with an accurate reason.
Common mistakes
- Treating a deleted secret as safe. The value stays in history. Treat any detected credential as exposed.
- Assuming scanning blocks the push. Secret scanning alerts after the fact. Blocking is the job of push protection.
- Assuming every secret is detected. The scanner finds known patterns. An internal token format with no matching pattern is missed unless you add a custom pattern.
- Ignoring alerts in public repositories. Once a secret is public, assume someone or something has already copied it.
Exercise
A developer commits an API key, notices the mistake, and removes it in the next commit before anyone reviews the pull request. Is the key still exposed, and will secret scanning still raise an alert?
Show answer
Yes to both. The key remains in the earlier commit in Git history, so anyone with read access can retrieve it, and secret scanning checks history, so it raises an alert. The key should be revoked at its provider.
Exercise
What is the main difference between secret scanning and push protection?
Show answer
Secret scanning detects secrets already stored in the repository and raises alerts. Push protection checks a push before it is accepted and blocks it if it contains a supported secret.