Lockhound
Blog

Why API keys end up in code, and what it costs

Nobody means to leak a secret. How API keys end up in repositories, bundles and chat logs, and why a leaked key gets used so fast.

Dec 1, 2026 · 3 min read

Nobody decides to publish their Stripe secret key. It happens in small, reasonable-looking steps, usually when someone is in a hurry.

How it happens

  • "Just to test it." A key is pasted straight into a controller to see if an integration works. It works, the commit goes out, and the key stays.
  • The .env gets committed. Often in the very first commit, before .gitignore was set up, or in a side project that later becomes public.
  • It goes to the browser. A secret key is put in a frontend variable so JavaScript can call an API directly. Everything sent to the browser is public, minified or not.
  • It's shared to get help. Pasted into Slack, a GitHub issue, a Stack Overflow question, a screenshot, or an AI chat, along with the error message.
  • AI-generated code. An assistant that was given a real key, in the prompt or in a file it can read, may write it inline as $apiKey = 'sk_live_...' instead of reading it from config. Read what it wrote before you commit.
  • A config file nobody thinks about. docker-compose.yml, a CI workflow, a Postman collection, a config/services.php with a fallback value: env('STRIPE_SECRET', 'sk_live_...').

Why it's dangerous

Bots watch public code on GitHub, npm and Docker Hub for anything that looks like a key, and they are fast. A leaked cloud key can be used long before you notice.

What happens next depends on the key:

  • Cloud keys get used to start expensive machines for crypto mining. The bill is yours.
  • Payment keys depend on the type. A Stripe secret key (sk_live_...) has full access to the account: it can issue refunds, read customer details or create charges. A restricted key (rk_live_...) can do only what it was granted. A publishable key (pk_live_...) is meant to be public.
  • Mail and SMS keys get used for spam and phishing from your domain, which wrecks your sender reputation.
  • Database credentials give access to your users' data.
  • Laravel's APP_KEY is the key your app encrypts and signs with. An attacker who has it can decrypt what the app encrypted with it, such as cookies and encrypted database columns, and forge cookies and signed URLs. Depending on the Laravel version and the session setup, that has gone as far as running code on the server.
  • Tokens with write access to GitHub or a registry let an attacker change your code or publish packages in your name.

Making a private repository public doesn't reset anything

If a secret was ever committed, it's in the history. Deleting the line in a new commit doesn't remove it, and neither does making the repository private again: forks, clones and caches keep it. The fix is to rotate the secret, then check the provider's logs for anything it was used for while it was exposed.

What actually helps

  • Keep secrets in .env, uncommitted, or in your host's secret store. Read them with env() only in config/*.php, which keeps them working once the config is cached, and never give one a real value as the fallback.
  • Never send a secret key to the browser. If the frontend needs an API, call it through your backend.
  • Use GitHub secret scanning and push protection. Secret scanning looks for known key formats in a repository, history included, and runs on public repositories for free. Push protection blocks many of those formats before they're pushed: it is on by default for your own pushes to public repositories, and it can be bypassed. On an organisation's private repositories both need GitHub Secret Protection, and push protection has to be turned on.
  • Run a scanner like gitleaks in CI and before making a repository public.
  • If your repositories are on Lockhound, turn on its secret check alongside these (paid plans, off by default). Each scan looks for provider keys, private keys, database URLs with a password and real values in a committed .env. It alerts on new critical and high ones on your default branch, and its pull request check fails when a pull request adds one. To block the merge, make that check required in GitHub. It reads the files of the current commit, not the history. It never stores the value: it keeps the file, line and kind, a masked preview and a one-way fingerprint to recognise the secret again.
  • Use keys with the least access they need, and with an expiry where the provider supports it.

Read next