KKAIROSافتح التطبيق
Practical guideTested4 min read

Secrets Safety Kit

Six steps for keeping secrets out of Git and cleaning up when one leaks. Each command was run in a scratch repository. Every key shown is the fake value sk-test-FAKE123. If a real key was ever exposed, revoke it first.

Advertisement

1. Why .env is still tracked after adding it to .gitignore

Why .env is still tracked after adding it to .gitignore?

.gitignore only affects untracked files. Untrack the file once and keep it on disk.

Command: git rm --cached .env

M .env
?? .gitignore
after:
?? .gitignore

Test: PASS (git 2.34.1, scratch repo, exit 0) Watch out: Run it once per tracked secret file, then commit. Source: https://git-scm.com/docs/gitignore

2. Keep API keys out of the repo from day one

Keep API keys out of the repo from day one?

Real key in .env (ignored), empty template in .env.example (committed).

Command: echo .env > .gitignore; commit .env.example

.env.example
.gitignore

Test: PASS (git 2.34.1, scratch repo, exit 0) Watch out: Check git ls-files before the first commit. If .env is listed, stop. Source: https://stackoverflow.com/questions/21774844/proper-way-to-hide-api-keys-in-git

3. Read keys in Python from environment variables

Read keys in Python from environment variables?

Never hard-code keys. Use os.environ.get and fail with a clear message if missing.

Command: os.environ["API_KEY"]

sk-test...
True

Test: PASS (git 2.34.1, scratch repo, exit 0) Watch out: Fail early with a clear message if the variable is missing. Do not print the whole key in logs. Source: https://docs.python.org/3/library/os.html

4. Deleted .env, but the key is still in history

Deleted .env, but the key is still in history?

Deleting only changes the newest commit. Treat the key as leaked and revoke it.

Command: git show HEAD~1:.env

API_KEY=sk-test-FAKE123

Test: PASS (git 2.34.1, scratch repo, exit 0) Watch out: Editing the file back is not enough. Old commits are public once pushed. Source: https://git-scm.com/docs/git-log

5. Find every commit that contains a key

Find every commit that contains a key?

Revoke the key first, then search history for other copies.

Command: git log --all -S"sk-test-" --oneline

8bc7005 two
b915164 one

Test: PASS (git 2.34.1, scratch repo, exit 0) Watch out: Revoke first. Scanning does not help while the key is still valid. Source: https://git-scm.com/docs/git-log

6. Remove the secret from all history

Remove the secret from all history?

Then force-push. Clones and forks keep old commits, so revocation is still required. git-filter-repo is the tool Git recommends (not run here).

Command: git filter-branch --index-filter "git rm --cached --ignore-unmatch .env" -- --all; then delete refs/original, expire reflog, git gc --prune=now

matches left:
0

Test: PASS (git 2.34.1, scratch repo, exit 0) Watch out: Back up the repository first. Everyone must re-clone afterwards. Source: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository

Worked example: a key that leaked

A key is committed by accident, then more work lands on top. After revoking the key you count the commits that contain it, rewrite history and count again. The code commit stays and the key is gone from every commit.

commits with key:
1
commits with key after cleanup:
0
167b526 code
95cba6b start

Test: PASS (git 2.34.1, scratch repo, exit 0) Source: https://git-scm.com/docs/gitignore

Quick reference

Words used in this kit

Checklist

Note: all outputs are from a scratch repository. Keys shown are fake. Commit hashes differ on your machine.

All full guides: /guide

Back to the guides