Sub topic 5
Turning on secret scanning and push protection
This chapter assumes you can use Git and the gh CLI, you know your way around repository settings, and you have admin rights on a repository. It shows where each feature is enabled and how to check that it worked.
Where the features can be enabled
Secret scanning and push protection are separate settings. Secret scanning looks at content already in the repository. Push protection checks a push before the remote accepts it. You can turn on either one without the other, but most teams want both.
Availability depends on repository visibility:
| Repository type | Secret scanning | Push protection |
|---|---|---|
| Public | Available at no cost | Available at no cost |
| Private or internal | Needs a GitHub secret protection license on the organization or enterprise | Needs the same license |
Public repositories often have these features on already. Check the settings page before you change anything.
Enabling in the web interface
- Open the repository and choose Settings.
- Open the Advanced Security page (labelled Code security in some plans).
- Switch on Secret scanning.
- Switch on Push protection.
You need admin access to the repository. If a switch is missing or greyed out, you are usually in one of two situations: the license is not available to the repository, or an organization policy controls the setting.
Enabling at scale
Organizations can apply settings to many repositories at once. Use Code security configurations in the organization settings. A configuration is a named bundle of settings, such as "secret scanning on, push protection on". You attach it to selected repositories or make it the default for new ones. Enterprise owners can apply the same idea across organizations.
Prefer a configuration over toggling each repository by hand. It keeps new repositories covered and shows which ones drift from the standard.
Enabling with the REST API
For a single repository, call the API through gh. The caller needs admin access, and the feature must be available for that repository.
gh api --method PATCH repos/OWNER/REPO \
-f 'security_and_analysis[secret_scanning][status]=enabled' \
-f 'security_and_analysis[secret_scanning_push_protection][status]=enabled'Replace OWNER/REPO with your repository. A successful call returns the repository object. To confirm the result, read the settings back:
gh api repos/OWNER/REPO --jq '.security_and_analysis'Look for "status": "enabled" under both features.
Common errors
- 404 Not Found. The token cannot see the repository, or lacks admin rights. GitHub returns 404 instead of 403 for hidden resources.
- 422 or a missing field. The feature is not available for that repository, often because a private repository has no license.
- Setting reverts or cannot be changed. An organization configuration is managing it. Change the configuration, not the repository.
- Old secrets not reported. Enabling secret scanning triggers a scan of the full history, but results can take a few minutes. Do not assume the repository is clean right after you turn it on.
Exercise
You run the gh api PATCH command on a private repository and get an error saying the feature is unavailable. The command syntax is correct and you are a repository admin. What is the most likely cause, and where would you look next?
Show answer
The private repository probably has no secret protection license, because those features are not free for private or internal repositories. Check whether the organization or enterprise has the license and whether a code security configuration is managing the repository. Then retry after the license or configuration is fixed.