Sub topic 5
Turning on secret scanning and push protection
This chapter covers where the two features are switched on, what each level of control affects, and how to confirm the settings took effect. Later chapters cover how push protection behaves once it is on.
Prerequisites
This chapter assumes you can:
- Open a repository's Settings page and read its tabs.
- Run
ghcommands in a terminal aftergh auth login. - Tell the difference between a repository, an organization and an enterprise on GitHub.
You also need to know that secret scanning finds credentials already in a repository, and push protection blocks them before they are pushed. The earlier chapters in this tutorial introduce both.
What you are switching on
Secret scanning and push protection are two separate settings. Turning on one does not turn on the other.
| Setting | What it does | Where it acts |
|---|---|---|
| Secret scanning | Scans the repository's contents and history and raises alerts | After content is in the repository |
| Push protection | Checks commits during a push and blocks supported secrets | Before content reaches the remote |
Run both. Secret scanning without push protection finds leaks only after they are public to everyone with read access. Push protection without secret scanning leaves you blind to secrets that were committed before you enabled it.
Availability and licensing
Availability depends on repository visibility and your plan.
- Public repositories: secret scanning is on by default and free. Push protection is also available for free.
- Private and internal repositories: both features require a paid GitHub product. GitHub currently sells them as GitHub Secret Protection, which was previously part of GitHub Advanced Security. Check your organization's billing page for the plan names that apply to you.
- Personal accounts: you can turn on push protection for yourself. It then applies when you push to public repositories, whatever the repository's own setting.
If the license is missing, the toggles are absent or disabled. An API call that tries to enable them fails with an error, as shown later.
Three levels of control
You can enable the features at the repository, organization or enterprise level. A higher level can set defaults for the levels below it.
Enable on a single repository
You need admin access to the repository.
- Open the repository and select Settings.
- In the Security section of the sidebar, select Advanced Security. Older interfaces call this page Code security and analysis.
- Next to Secret Protection (or Secret scanning), select Enable. Confirm if asked.
- Under the secret scanning options, switch on Push protection.
When you enable secret scanning on a repository that already has history, GitHub scans the full history, not just new commits. Expect an initial batch of alerts. Plan time to triage them, and do not enable the feature just before a release freeze.
Enable across an organization
Organization owners and security managers use security configurations. A configuration is a named bundle of settings that you apply to a set of repositories.
- Open the organization, then Settings.
- Select Advanced Security, then Configurations.
- Start from the GitHub-recommended configuration, or create your own. Make sure secret scanning and push protection are set to Enabled.
- Select Apply to and choose all repositories or a filtered set.
- Under Default configuration for newly created repositories, choose the configuration so that new repositories start protected.
Applying a configuration changes many repositories at once. A configuration also reports which repositories could not be changed, usually because a license seat is missing. Check that list rather than assuming every repository was covered.
You can choose whether a configuration is enforced. An enforced configuration stops repository admins from changing the settings it controls. Use enforcement when you must guarantee coverage, and leave it off when teams need to manage their own exceptions.
Enable for an enterprise
Enterprise owners can set policies and apply configurations across all organizations. The steps mirror the organization flow, under the enterprise account's Settings and Code security pages. Enterprise-level settings take precedence where the policy is enforced.
Enable with the REST API
For many repositories, or for repeatable setup, use the API. The security_and_analysis object on the repository resource holds the status of each feature.
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 the repository. The caller needs admin access. The feature must also be available for that repository, meaning it is public or has the license described above.
A few points about this call:
-fsends each value as a string. The bracketed names build the nested JSON body that the API expects.- The call changes only the fields you send. Other settings stay as they are.
- A
422response usually means the feature is not available for that repository, for example a private repository without a license. A404usually means you lack permission or have the wrong name, since GitHub hides repositories you cannot access.
Verify the result
Do not rely on the absence of an error. Read the settings back.
gh api repos/OWNER/REPO --jq '.security_and_analysis'The output should show "status": "enabled" for secret_scanning and secret_scanning_push_protection. If security_and_analysis is missing from the response, your token lacks admin access to the repository.
To enable across several repositories, loop over a list of names. Keep the loop simple and check each response:
for repo in api web worker; do
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' \
--silent && echo "enabled: $repo" || echo "FAILED: $repo"
doneFor a whole organization, prefer security configurations over a loop. A configuration also covers repositories created later, while a loop covers only the names you list.
Related options on the same page
Several other settings sit beside the two main toggles. Know what they are so you can decide on each one deliberately.
- Validity checks: ask the provider whether a detected secret is still active. This helps you prioritize alerts.
- Non-provider patterns: detect generic secrets such as private keys and connection strings. These patterns produce more false positives than provider patterns, so you may want them on for alert scanning and off for push protection.
- Custom patterns: your own regular expressions for internal token formats.
- Delegated bypass: requires a reviewer to approve a push protection bypass instead of letting the pusher decide.
Later chapters explain each of these. For now, enabling the two main toggles is enough to get coverage, and you can tune the rest after you see the first alerts.
Common errors
- The toggle is greyed out. The repository is private and has no license seat, or an organization or enterprise policy controls the setting. Ask an owner.
- Push protection is on, but a teammate's push was not blocked. The secret may not match a supported pattern. They may also have used a bypass. Check the alert for the
push_protection_bypassedfield. - New repositories are unprotected. The organization has no default configuration for new repositories. Set one.
- The API call succeeded but nothing changed. The request body was wrong, often because the bracketed field names were mistyped. Read the settings back.
Exercise
You run the PATCH call from this chapter against a private repository in an organization with no paid security license, and the API returns 422. What is the most likely cause, and how do you confirm the settings of a repository where the call did succeed?
Show answer
The feature is not available for that repository, because private repositories need a license for secret scanning and push protection. For a repository where the call succeeded, run gh api repos/OWNER/REPO --jq '.security_and_analysis' and check that secret_scanning and secret_scanning_push_protection both show "status": "enabled".
Exercise
Your organization wants every existing and future repository to have both features on, and wants repository admins unable to turn them off. Which mechanism fits better, a shell loop over repository names or a security configuration, and which option stops admins from changing the settings?
Show answer
A security configuration fits better. It can be applied to all existing repositories and set as the default for new ones, which a loop cannot do. Enforcing the configuration stops repository admins from changing the settings it controls.