Sub topic 1
Why Committed Secrets Are a Problem
Prerequisites
This chapter assumes you know basic Git: committing, pushing, branches and history. You should have a GitHub account and know where repository settings live. You should also understand what an API key, token or password does. No setup is needed yet. Later chapters cover enabling the features.
What counts as a secret
A secret is any value that proves identity or grants access without further checks. Common examples:
- API keys and personal access tokens
- Cloud provider access keys
- Database connection strings with passwords
- Private keys and signing certificates
- Webhook signing secrets
Anyone who holds the value can act as the owner of that credential. The system that accepts it cannot tell the difference.
Why a commit makes it worse
A leak in source control is harder to undo than a leak in a chat message, for three reasons.
- Git keeps history. Deleting a secret in a later commit removes it from the current files only. The old commit still contains it, and anyone who clones the repository can read it.
- Copies spread. Forks, clones, CI caches and local checkouts each hold their own copy. You cannot recall them.
- Public repositories are watched. Automated scrapers monitor public pushes and try new credentials within minutes. A private repository is safer, but it can become public later, and everyone with read access can see the secret.
Treat any committed secret as compromised, even if you removed it seconds later.
A worked example
Suppose a developer hard-codes a key to test locally:
api_key = "acme_live_key_example"
response = call_payments_api(api_key)They commit and push. Later they notice and push a second commit that changes the line to read the key from the environment. The current file is clean. But this command still shows the original value:
git log -p --all -S "acme_live_key_example"The -S option searches for commits that add or remove that exact string. It finds the first commit, so the key is still readable by anyone with access to the repository.
What the real cost looks like
The damage depends on what the credential can do. A read-only token for a test service may cost little. A cloud key with broad permissions can allow data theft, unexpected compute charges, or access to other systems. Even a low-privilege token can be a foothold for further attacks.
The cost also includes response work. You must find where the credential is used, revoke it, issue a new one, update every system that depends on it, and review logs for misuse. This is much more expensive than preventing the commit.
Where GitHub fits
GitHub offers two features that address this problem at different points in time:
| Feature | When it acts | What it does |
|---|---|---|
| Secret scanning | After content is in the repository | Finds known secret formats and creates alerts |
| Push protection | During a push | Blocks the push when it contains a detected secret |
Push protection prevents a leak. Secret scanning detects the ones that still get through, including older content. The next chapters explain how each works and how to use them together.
Exercise
A developer commits a cloud access key, notices the mistake, and pushes a new commit that deletes the key from the file. Is the key safe? Explain why, and say what the first corrective action should be.
Show answer
No. The key remains in the earlier commit, so anyone who can read the repository history, or who already cloned or forked it, can still get it. The first action is to revoke the key at the cloud provider (then issue a replacement). Cleaning history can happen afterward.