Learn how to respond effectively to a leaked secret in your GitHub repository.
Secrets, such as API keys, tokens and credentials, can pose significant security risks to your team and organization if inadvertently exposed in your codebase or stored improperly.
You should consider any leaked secret to be immediately compromised and it is essential that you undertake proper remediation steps, such as revoking the secret. Simply removing the secret from the codebase, pushing a new commit, or deleting and recreating the repository do not prevent the secret from being exploited.
This tutorial walks you through what to do if you've accidentally committed a secret to your repository, or if you've been alerted to a secret leak in your repository.
Note
Secret scanning is free for public repositories. It is available as part of GitHub Secret Protection for private repositories on GitHub Team and GitHub Enterprise Cloud plans.
Gather as much information as you can about the leaked secret. This will help you assess the risk and determine the best course of action for remediation.
CODEOWNERS file of the repository to determine the responsible team.git log -S to help search the commit history of your repository to identify who committed the secret.Tip
If you have secret scanning enabled for your repository, the secret scanning alert can provide you with most of these details.
How you approach remediation will be determined by the risk factors associated with the secret leak.
Secret scanning can help you assess the risk associated with an alert, but if you don't yet have secret scanning enabled, you can still perform a risk assessment based on the information available to you.
Review the secret scanning alert associated with the leak, checking the alert labels and any available metadata:
Note
public exposure label to determine if the secret was leaked in a public repository.multiple leaks label to determine if the secret is exposed in multiple locations.If you don't yet have secret scanning enabled for the repository, perform a risk assessment based on the following:
Tip
Organizations on GitHub Team and GitHub Enterprise plans can perform a free secret risk assessment (an on-demand, point-in-time scan) that evaluates their exposure to leaked secrets. See Secret security with GitHub.
The next step depends on the risk assessment you performed in the previous step.
Automated scanners can locate publicly exposed secrets in minutes. They can be exploited by malicious actors within hours. The longer that an active secret is left exposed, the greater the potential for serious breaches.
If the secret is high risk (that is, the secret is still active, is exposed in a public repository, or is a production credential), we would recommend that you:
Prioritize revoking the secret immediately. See Step 4.
If you are concerned about downtime to services, you may want to first generate a new secret with the same permissions, make the application start using the new token, and *then* revoke the old secret.
Communicate with the secret owner (identified in Step 1), repository administrators, and security leads during or after revocation.
If the secret is medium to low risk (that is, the secret is no longer active, is exposed in a private repository, or is a test or development credential), you can plan the remediation strategy accordingly:
It is important to remediate even medium- to low-risk secrets, as they can still pose a risk to both security and compliance if left exposed.
It is not sufficient to simply remove the secret from your codebase. The most important remediation step is revoking the secret with the secret's provider. By revoking the secret, you drastically reduce the potential for the secret to be exploited.
Using the information gathered in Step 1, locate the secret provider's website or documentation.
Follow the provider's instructions for revoking the secret. This typically involves logging into the provider's portal and navigating to the section where the secret is managed.
If you lack access to the provider portal, contact the secret owner or relevant repository administrator to get help with revoking the secret.
Generate a new secret, if necessary, to replace the revoked secret. This is often required to restore functionality for services that relied on the original secret.
Note
GitHub automatically revokes GitHub personal access tokens (PATs) leaked in public repositories.
For leaked GitHub PATs in private repositories, you can report the leak to GitHub directly from within the secret scanning alert by clicking Report leak.
For other secret types, if a secret matching one of GitHub's supported partner patterns is leaked in a public repository, GitHub automatically reports the leak to the secret provider, who may immediately revoke the secret.
Next, you need to coordinate updates to all affected services using the leaked secret and update them with the new secret.
org:YOUR-ORG "SECRET-STRING".repo:YOUR-REPO "SECRET-STRING".A safe way of providing sensitive credentials to your application is through a vault. For example, you can make sensitive credentials available to GitHub actions and workflows through the "Secrets and variables" store under your repository's settings page.
Once services are back up and running, it's important to check for any unauthorized access that may have occurred while the secret was exposed.
Review GitHub's audit logs for events relating to the secret and its usage.
For the organization- and enterprise-level audit logs, you can specifically search for events related to an access token. See Identifying audit log events performed by an access token (organizations) and Identifying audit log events performed by an access token (enterprises).
Access to GitHub's audit logs depends on your role, so you may need to contact an organization owner or enterprise administrator if you don't have the necessary permissions.
Review the secret provider's audit logs.
Although you've now revoked and updated the secret in your codebase, the secret may still exist in your repository's commit history. Ideally, you should search for and remove all instances of the secret from your repository.
However, cleaning Git history can be a destructive and disruptive process, as it may involve force pushing changes to the repository.
Together with your repository's security leads, consider carefully the effects of cleaning the repository's history against your compliance or security obligations. See Removing sensitive data from a repository.
Dealing with secret leaks is often disruptive, complicated, and time-consuming. The focus for secret handling should always be on preventing leaks at all costs: