Stripe secret keys in public notebooks and repos: find yours first
A live Stripe secret key starts with sk_live_. Anyone who has it can act on your Stripe account through the API. People search public code for that prefix every day, so it's worth searching your own code first.
Home › Guides › Stripe secret keys in public notebooks and repos: find yours first · Last updated · By Ember, SniffPup
Where Stripe keys leak
- Notebooks. A Jupyter notebook saves cell outputs with the file. If a cell ever printed your key, or set it in plain text, the key is in the
.ipynbfile even after you delete the cell's code. - Public repos. A key committed once stays in git history after you remove it from the latest version.
- Front-end code. A secret key used in browser code is visible to every visitor. Only the publishable key (
pk_live_) belongs there. - Config files. A
.envfile that gets committed or deployed.
Restricted keys (rk_live_) can leak the same ways. They can do less, but they still need rotating.
Search your own code
In each repo, search the current files and the full history:
git grep -n "sk_live_\|rk_live_" git log -p --all -S "sk_live_" | head
Open-source scanners such as gitleaks and TruffleHog check whole repo histories for keys. For notebooks, clear all outputs before you commit, or use a tool like nbstripout to strip them automatically.
On your live site, open the browser's developer tools and search the loaded scripts for sk_live_. Our find exposed API keys on your website guide shows how, and the free scan at sniffpup.com checks your live site from outside.
If you find one
- Roll the key now in the Stripe Dashboard under Developers > API keys. Rolling it creates a new key. Choose to expire the old one now, not later.
- Update your server with the new key, stored as an environment variable or secret, never in code.
- Check for misuse. Look through recent payments, refunds, payouts and API request logs for anything you don't recognize.
- Clean up the source. Remove the key from the file, clear notebook outputs and, if the repo is public, consider rewriting history. Rolling the key matters more, because copies may already exist.
Our leaked Stripe secret key guide goes through each step in more detail.
Stop it happening again
- Keep keys in environment variables, and add
.envto.gitignore. - Strip notebook outputs before every commit.
- Use restricted keys with only the permissions each service needs.
- Check your repos with SniffPup's repo scan before making them public.
FAQ
Where do live Stripe secret keys leak?
A live Stripe secret key starts with sk_live_. Notebooks can keep a key in saved cell output, a public repo keeps a committed key in git history, front-end code shows a secret key to every visitor, and a committed or deployed .env file can hold one too. Only the publishable key (pk_live_) belongs in browser code.
What should I do if I find a live Stripe secret key?
Roll the key now in the Stripe Dashboard under Developers > API keys. Rolling it creates a new key. Choose to expire the old one now, not later. Update your server with the new key, stored as an environment variable or secret, never in code.
Does removing the key from the latest file remove it from git history?
A key committed once stays in git history after you remove it from the latest version. Rolling the key matters more, because copies may already exist.