Sub topic 6
Push protection: blocking secrets before they land
This chapter assumes you know basic Git (commit, push, branches), can change repository settings on GitHub, and know what an API key or token is. It also assumes you have read the earlier chapters on how secret scanning works and how to turn it on.
What push protection does
Secret scanning looks for secrets that are already in a repository. Push protection acts earlier. It scans the content of a push while the push is in progress. If it finds a supported secret, GitHub rejects the push, so the secret never lands in the remote repository.
The two features complement each other. Secret scanning is detection after the fact, and push protection is prevention. A blocked push creates no alert, because the secret was never stored. An alert appears only if someone bypasses the block or the secret arrived by another route, such as a commit made before the feature was on.
What gets scanned
Push protection checks the commits in the push for the high-confidence provider patterns that GitHub supports. It favors patterns with a low false-positive rate, so it does not flag every string that looks random. Custom patterns can be added to push protection, but only after you define them and enable that option for the pattern. A secret in a format GitHub does not recognize will pass through. That is why you still need secret scanning alerts and the prevention practices covered later.
Where it applies
Push protection is enforced on the server, so it covers pushes from the command line, from editors, and from the GitHub web interface. You enable it per repository, or for many repositories at once through organization settings and security configurations. For public repositories it is on by default for the push author, which protects contributors even if the repository owner has not set anything up.
What a blocked push looks like
When a push is blocked, Git prints an error that names the secret type and the file and line where it was found. A typical message looks like this:
remote: error: GH013: Repository rule violations found for refs/heads/main.
remote: - GITHUB PUSH PROTECTION
remote: Push cannot contain secrets
remote: —— GitHub Personal Access Token ——————————————
remote: locations:
remote: - commit: 1a2b3c4d
remote: path: config/settings.py:12Read the commit and path carefully. The secret may be in an earlier commit of the push, not the latest one.
The next chapter covers the options in detail. In short, you either remove the secret and rewrite the commits, or you bypass the block and give a reason.
Limits to remember
- Push protection only sees what is pushed. Secrets pasted into issues or pull request comments are handled by separate scanning.
- A bypass lets the push through. It does not make the secret safe, so treat a bypassed secret as exposed.
- Patterns without push protection support are detected only after the push, by secret scanning.
Exercise
A teammate fixes a blocked push by deleting the API key from the file and committing the change on top. The push is blocked again. Why, and what should they do?
Show answer
Push protection scans every commit in the push, and the earlier commit still contains the key. They need to rewrite that commit, for example with an interactive rebase or an amend, so the key is gone from all commits being pushed. If the key was ever real, they should also revoke it.
Exercise
Push protection blocks a push, so no alert is created. When would a secret scanning alert still appear for a secret that push protection supports?
Show answer
When someone bypasses the block, since the secret then lands in the repository. An alert can also appear if the secret was committed before push protection was enabled.