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

Leaked API key: revoke first, then scan history

The order decides how much damage is done. Revoke the key at the provider and create a new one before touching Git, because anyone can copy a public repository at any time. Only then search your history for other copies of the same key.

Published:

Advertisement

Scope: response order and finding copies. The rewrite itself is page 15, and why deleting the file is not enough is page 16.

I leaked an API key: what do I do first, and how do I scan my Git history?

Revoke and replace the key first, then scan history for other copies.

Why does git log -S show only some commits?

The -S option lists only commits that change how many times the text appears, meaning the commit that added it and the one that removed it. Commits in between are not listed, so use git grep over every commit to see them all.

Tested commands and output

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

$ echo API_KEY=sk-test-FAKE123 > config.txt
$ git add .
$ git commit -qm one
$ echo ok > b.txt
$ git add .
$ git commit -qm two
$ git rm -q config.txt
$ git commit -qm three
$ git log --all -S"sk-test-" --oneline
9088ad1 three
9381d4d one
$ git grep -n "sk-test-" $(git rev-list --all) | cut -c1-80
fe1cff2ca3a6a0112beb18dc22973330cc456a21:config.txt:1:API_KEY=sk-test-FAKE123
9381d4d7166e1c1dd7b126f32dc13fc0caf3bdfd:config.txt:1:API_KEY=sk-test-FAKE123

Note

Commit hashes on your machine will differ from the ones shown. The key prefix in the command is the fake test value.

Source: https://stackoverflow.com/questions/66730492/accidently-left-up-api-keys-because-i-forgot-to-place-the-file-name-in-git-ignor (Stack Overflow question this page answers) Source: https://git-scm.com/docs/git-log (git log -S docs) Source: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository (GitHub guide to removing sensitive data)

Full kit: Secrets Safety Kit