A secret got committed
Last updated
Ana's photo upload script had the family photo site's password typed into it, and she pushed it. (In the sandbox it's fake.) Deleting the line in a new commit doesn't help: history keeps it. In this lesson you'll find the secret the way anyone could, learn the right order of steps, and set things up so it doesn't happen again.
By the end of this lesson you can
- Explain why a committed secret must be treated as leaked
- Respond in the right order: revoke and replace first, then clean up
- Say what git-filter-repo can and can't do once a secret is pushed
- Keep secrets out with environment variables, .gitignore, hooks and host push protection
Deleted, and found in two commands
$ git commit -am "Read the photo password from the environment" [main a82bb01] Read the photo password from the environment 1 file changed, 3 insertions(+), 1 deletion(-) $ git push To https://example.com/ana/kusina.git 3f02b9a..a82bb01 main -> main
a82bb01HEAD →mainorigin/main Read the photo password from the environment3f02b9aAdd halo-halo recipe07d7601Add a script to upload photos67e435dCover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README
and 3 older commits
Automated scanners search public repositories for keys and passwords within minutes of a push. Assume someone has already seen it.
Assume it's leaked
Once a secret has been pushed, especially to a public repository, treat it as known to strangers. The fix isn't in Git at all:
- Revoke and replace it first, where it came from: change the password, delete the key or token and make a new one. That makes the leaked copy useless, whatever happens to the history.
- Then check whether it was used, fix the code so secrets come from outside the repository, and prevent the next one.
Removing it from history is the last step, and often not needed once it's revoked.
Put the response in order
0 of 5 placed right
Change the password on the photo site (revoke the old one).
Check the photo site for anything done with the old password since it was pushed.
Change the script to read the new password from an environment variable, and commit that.
Add
.envto .gitignore and turn on the host’s secret scanning and push protection.Decide with the family whether to rewrite history to remove the old password.
Rewriting history, and its limits
If the secret (or a huge file, lesson 7.5) must leave history, git-filter-repo rewrites every commit without it. Git's own documentation recommends it over the older git filter-branch.
git filter-repo --replace-text replacements.txt git push --force --all git push --force --tags
replacements.txt lists the text to replace. But know the limits:
- Every commit from the first one containing the secret gets a new hash (lesson 6.1). Everyone must clone again; the golden rule (lesson 6.3) is broken on purpose.
- Copies already made (other people's clones, forks, caches, scanners' records) still have it. Hosts may also keep old commits reachable for a while; GitHub asks you to contact support to clear them.
That's why revoking comes first: rewriting can't un-leak anything.
Keeping secrets out
- Read secrets from the environment, not the code:
os.environ["PHOTO_PASSWORD"]in Python. Locally, keep them in a.envfile listed in.gitignore; commit a.env.examplewith the names but no values. - Hooks (lesson 7.3) can scan staged changes for things that look like keys before each commit.
- Push protection on the host (GitHub, GitLab) refuses a push that contains a recognised secret, and secret scanning alerts you about ones already in the repository. Turn them on.
- Find the commits that added or removed the password with
git log -S - Print the old file from history with
git show <commit>:upload_photos.py - Search an old commit with
git grep FAKE <commit>
Working folder
no changes
Staging area
nothing staged
Repository
12 commits on main
Remote
origin: example.com/ana/kusina.git · in step, as of your last fetch
You are in ~/kusina. Two commits ago, a password went into upload_photos.py, and it has been pushed. (It is fake.)
Spot the trap: rewriting but not revoking
Ana runs git-filter-repo, force-pushes, and checks the host: no trace of the password. She doesn’t change the password, because “it’s gone now”.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- GitHub Docs: Removing sensitive data from a repository: revoke first; git-filter-repo; side effects and limits
- git-filter-repo: the tool Git's own documentation recommends for rewriting history
- GitHub Docs: About push protection: the host blocking pushes that contain secrets
- GitLab Docs: Secret push protection: the same on GitLab