Can one git push leak a secret key? Turn on push protection

GitHub push protection scans every push before it reaches the repository and blocks it if it contains a key or token. For users it is on by default for public repositories only; for a repository you turn it on in Settings.

Published 3 min read#App security#Developer tools#Supply chain security

Your store's payment key rarely leaks through a clever hack. It leaks when someone on the team pastes it into a config file "just to test quickly" and runs git push. A second later the key is in the repository's history, and removing it afterwards is much harder than stopping it before it lands. This article shows how to make GitHub stop it at the door, and who is protected by default and who is not.

What is GitHub push protection?

Push protection is a GitHub secret scanning feature that checks a push before it reaches the repository, and blocks it if it finds a key or token written into the code.

According to GitHub's page about push protection, the feature is designed to prevent hardcoded credentials from entering a repository, so it stops the leak instead of reporting it after the fact. It does not only work on the command line: it also covers commits made in the GitHub web interface, file uploads, and REST API requests.

Is it already on for you?

Push protection has two levels: for users it is on by default and stops you from pushing secrets to public repositories, and at the repository level it is off by default until someone who runs the repository turns it on.

The same docs page says push protection for users is enabled by default on the GitHub website, and push protection for repositories is disabled by default and is enabled by a repository administrator, organization owner or security manager. Most company and client projects live in private repositories, and the user-level protection does not cover them. So the repository holding your store's code and its payment keys is exactly the one that may not be protected, unless someone turned it on.

How do you turn it on for a repository?

Turning on push protection for a repository takes five clicks from its settings page, and you need to be one of the repository's owners or admins.

From GitHub's page on enabling push protection for a repository:

  1. Open the repository's main page on GitHub.

  2. Click Settings under the repository name (if you don't see it, it is in the More menu).

  3. In the Security and quality section of the sidebar, click Advanced Security.

  4. If Secret Protection is not enabled yet, click Enable next to it.

  5. In the Secret Protection section, click Enable next to Push protection.

What do you do when a push is blocked?

When a push is blocked, you have to remove the secret from every commit it appears in, not only from the latest version of the file, and then push again.

GitHub's page on working with push protection from the command line says the error lists all of the commits that contain the secret, with the file and line. If the secret is in the latest commit, remove it from the code and update that same commit instead of creating a new one:

git commit --amend --all
git push

If it came in through an older commit, find it from the error message and git log, then:

git rebase -i <COMMIT-ID>~1

In the editor, change pick to edit on the commit that introduced the secret, remove the secret from the code, then run git add ., git commit --amend and git rebase --continue, and finally git push.

If you are sure the secret is not real (an example in your docs, say), you can bypass the block and give a reason, then push again from the command line within three hours, according to the same page.

What if the key reached the repository before you turned it on?

Treat a key that reached the repository as burned: rotate it in the dashboard of the service that issued it, then clean the code.

Removing the key from git history does not help if someone cloned the repository or saw the commit before you removed it. Rotating is what closes the door, and cleaning stops anyone from finding it later and assuming it still works.

Where do you start today?

Open the list of your private repositories, turn on push protection for every repository whose code reaches a database, payments or email, and start with the one the most people work on.

If you keep keys in environment variables at a hosting provider, see how to rotate secrets after an environment variable leak. If your workflows use actions from outside, pin their versions with a lockfile. And if you want someone to look for exposed keys in your platform before anyone else does, that is part of the Muhassan security test.

How many of your private repositories would accept a push with a key in it right now, without asking?

FAQ

If I remove the key in a new commit, will the push go through?

No. The key is still in the older commit, and GitHub asks you to remove it from every commit it appears in, with amend or rebase, before you push again.

Is a private repository protected by default?

No. The protection that is on by default for users stops you from pushing secrets to public repositories. A private repository needs one of its admins to turn push protection on.

Can I bypass the block if the key is only a test key?

Yes, anyone with write access can bypass it and give a reason, unless the repository limits bypass to specific people. Make sure the key really opens nothing real.

Share

More articles