vibe-audit.lab

guides · 2026-10-11

Secret Hygiene Before Release: Stop API Key Leaks

Every leaked API key has the same epitaph: "it was only temporary." Keys pasted into client-side code, committed to git, dropped into AI chat prompts — each one is a credential that now belongs to everyone. Here's where secrets actually escape, and the hygiene habits that keep them contained.

Where secrets leak

1. Client-side code

The number-one leak vector. const STRIPE_KEY = "sk_live_..." in a JavaScript bundle isn't a secret — it's a string anyone can read with View Source. Frontend code can only safely hold publishable keys (designed to be public, like Stripe's pk_ keys); anything with sk_, "secret", "private" or "admin" in the name belongs on a server.

2. Git history

Committing a key and then deleting it in the next commit doesn't help — it's in the history forever, and secret-scanning bots watch public repos in real time. AWS, GitHub and Stripe all run scanners that revoke or alert on keys found in public commits, sometimes within minutes. Private repos aren't safe either: every clone is a copy, every ex-employee a risk.

3. AI chat prompts and generated code

Pasting production code into an AI assistant to "fix this auth bug" sends your secrets to a third party. Many AI tools train on or retain conversations; some have suffered prompt-data exposures. Treat AI chat like a public forum: sanitize before you paste. And review AI-generated code for hardcoded placeholder keys the model "helpfully" filled in — developers have shipped sk-test-123-style keys that turned out to be real keys from training data.

4. Logs, error messages and URLs

Keys end up in log files (logged request bodies), error trackers (stack traces with env dumps), and URLs (query parameters in access logs and browser history). Log the key's prefix for debugging, never the key.

5. CI/CD and chatops

Secrets in CI config files, echoed in build logs, or pasted into Slack "just for a sec" — each is a leak with an audience. CI secrets belong in the platform's secret store, masked in logs, with minimal scope.

The rotation runbook

When a key leaks — assume it has, the moment it touched anywhere public — rotate immediately:

  1. Generate the new key first in the provider's dashboard.
  2. Deploy the new key to every service that uses it.
  3. Revoke the old key — only after the new one is live everywhere, to avoid downtime.
  4. Check for abuse: review the provider's usage logs for the exposure window. Unexpected spikes mean someone found it.
  5. Remove the leaked value from code, history (rotate + purge, or accept history is burned), chats and logs.

Speed matters more than elegance. Most providers make rotation a three-click operation — the bottleneck is always human hesitation.

Building leak-proof habits

Before you release: the pre-release secret scan

Checklist added 2026-11-23 · Guide reviewed 2026-11-23

Habits are the first line of defence; a release is where they get proven. API release hygiene means no build ships until the release candidate itself — not just today's code — has been scanned for secrets. Run these steps, in this order, on every release:

  1. Scan the full git history, not just the working tree. A key that was committed and then "removed" in the next commit is still in history and still live. Run a history-aware scanner over every branch you are about to publish — for example gitleaks git or trufflehog git pointed at the repo — and treat any verified finding as an exposed key, not a false alarm to wave through.
  2. Scan the working tree and the release artefacts. Run the scanner over the files themselves (source, config, docs, example files), then over what you actually ship: built JavaScript bundles, mobile app packages and container images. Keys get baked into build output and Docker image layers even when the source is clean — check the artefact, not just the repo.
  3. Check the env files explicitly. Confirm every .env, .env.local and .env.production is gitignored and untracked (git ls-files | grep env should return only placeholders such as .env.example), that example files contain dummy values only, and that no real key sits in CI configuration files, release notes or changelog text.
  4. Rotate anything the scan finds — before you ship. Deleting the string does not un-leak it. Follow the rotation runbook above: generate the new key, deploy it everywhere, revoke the old one, then check the provider's usage logs for the exposure window. Only then clean the code and, if the key reached a public repo, purge or rewrite the history knowing the old value is burned either way.
  5. Gate the release on the scan in CI. Make the secret scan a required CI job on the release branch: the pipeline runs the scanner and a finding fails the build and blocks the release — the same blocking pre-commit hook, enforced where nobody can skip it locally. Keep CI's own secrets in the platform secret store, masked in logs, so the gate does not become the leak.
  6. Re-scan after fixes and record the clean run. Fix, rotate, then run the whole scan again on the exact commit you are tagging. A release is clean when that final scan passes — note the date and result in your release notes so the next release starts from a known-good baseline.

Scanning is the safety net

Habits fail; automation doesn't get tired. Vibe-Code Audit scans pasted code and live sites for 32 secret patterns (AWS, GitHub, Stripe, OpenAI, Supabase, Twilio, private keys, JWTs and more), shows each leak's blast radius, and Pro adds the exact rotation runbook for the key type found. Run it on every AI-generated change before merge — it's the cheapest incident you'll ever prevent.

Sources

Frequently asked questions

Where do API keys and secrets most often leak?

In client-side code (anything shipped to the browser is a public string), in git history (deleting a key in the next commit leaves it in history forever, and scanning bots watch public repos in real time), and in AI chat prompts and generated code pasted back and forth during debugging.

Is a key in a private repository safe?

Safer, not safe. Every clone, fork, CI log and ex-collaborator laptop is a copy. Keep real values in environment settings or a secret manager and commit only a placeholder example file.

Which keys are safe to ship in frontend code?

Only publishable keys designed to be public, like Stripe's pk_ keys. Anything with secret, private or admin in the name — sk_ keys, service-role keys, signing secrets — belongs on a server, never in a bundle.

What should I do the moment a key leaks?

Rotate it immediately — treat exposure as compromise, because you cannot prove nobody copied it. Then audit the provider's logs for the whole exposure window, and fix the path it escaped through so the replacement does not follow it.

How do I stop secrets reaching AI chat tools?

Treat AI chat like a public forum: redact keys before pasting code, use placeholder values in examples, and give the assistant the shape of the code rather than the production file. Rotate any key that has already been pasted.


Informational explainer, not a security guarantee. Scan your code free before every deploy.

← all guides

← Back