How to Find API Keys Leaked in Your AI-Built App

· 8 min read ·
vibe-coding security api-keys supabase

A man in a green sweater pulling a glowing golden key out of code on a monitor while a small robot detective holds a magnifying glass

The Moltbook breach is the famous example, and most people get its cause wrong. Moltbook’s key was the public one, meant to be in the browser. What opened the database was missing row level security, and 1.5 million other keys came out with it. That is the difference this post is about: some keys are meant to be public, and some must never be.

Underneath all five: your frontend is a public document, every string in it published. AI tools know this and still leak keys, because they fix the error in front of them. “The server can’t read this key” gets fixed by moving the key somewhere the browser can read. Error gone, secret published.

Check these five places, most common first; the bundle is where the AI’s own fix lands the key.

1. The JavaScript bundle

The most common leak is an environment variable with a public prefix. Next.js inlines anything named NEXT_PUBLIC_ into the client bundle. Vite does it with VITE_, Create React App with REACT_APP_, Expo with EXPO_PUBLIC_, Astro and SvelteKit with PUBLIC_. The prefix decides where the value goes. The rest of the name is decoration.

So NEXT_PUBLIC_SUPABASE_SECRET_KEY is public. The name says SECRET, but the prefix says PUBLIC, and the prefix wins.

How to check: open your live site, open DevTools, go to Sources, press Cmd+Shift+F (Ctrl+Shift+F on Windows) and search all files for:

  • sk_live_ and sk_test_ (Stripe secret keys), rk_ (Stripe restricted keys)
  • sk-ant- (Anthropic, documented) and sk-proj- (OpenAI project keys, observed in the wild; OpenAI does not document the prefix)
  • sb_secret and service_role (Supabase)
  • eyJhbGci (every legacy Supabase key starts with this; so does every logged-in user’s session token, so hits are expected and the payload decides)
  • AIza (Google), ghp_ (GitHub), xox (Slack)

What belongs in the bundle: Supabase publishable or anon keys, Stripe publishable keys (pk_...), Firebase config. Those are public by design. What never belongs: anything with secret, service_role, sk_, rk_ or sk- in the name or the value. Search the anchored forms; a bare sk- also matches task- and desk-.

Found a legacy Supabase key and not sure which kind it is? Decode it locally instead of pasting a possibly leaked key into a website:

Terminal window
echo "PASTE_KEY" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null; echo

That prints the payload (anon is fine, service_role is not). If your base64 is picky about URL-safe input, the node form handles it: node -e "console.log(Buffer.from(process.argv[1].split('.')[1], 'base64url').toString())" PASTE_KEY.

2. Source maps

Source maps exist so you can debug production; they also make your original source downloadable by anyone: file names, comments, and values the bundler stripped.

How to check: look for a //# sourceMappingURL= comment at the end of your live JS bundle, or request the bundle URL again with .map at the end. A large JSON answer means your source is public.

Next.js productionBrowserSourceMaps and Vite build.sourcemap both default to false, so the check is that nobody turned them on. Error trackers are the one fine exception: they upload source maps to symbolicate stack traces, safe as long as the maps are not also served publicly.

3. Git history

Deleting the .env file does not delete the past. Two commands tell you the truth:

Terminal window
git log --all --oneline --name-only -- '*.env*'
git log -p -G 'sb_secret' --all

The first lists every commit that touched any env file, including a .env.local or backend/.env that was committed and later deleted (the plainer git log --all -- .env misses those). The second searches history for a string. Use -G, not -S: -S only lists commits where the count of matches changes, so a key rotated in place, old key out and new key in inside one commit, slips past it. (-G takes a regex.) Tested: three commits adding sb_secret_AAAA, replacing it with sb_secret_BBBB, then removing it; -S skipped the middle commit that introduced the second key, -G listed all three. For a full scan, run trufflehog or gitleaks: free, they read every commit’s contents, and they are what the other side runs.

The rule: if the repo was ever public, even for an hour, treat every key in it as leaked; bots scrape new public repositories for key formats within minutes. In Orca Security’s 2023 honeypot experiment, a key planted in a public GitHub repository was used by an attacker within two minutes. GitHub’s push protection catches some providers’ keys, not all.

4. Error trackers and logs

Error trackers capture what your app saw. Sentry scrubs server side by default and removes values whose names contain secret, api_key, auth, token, bearer or password, so a well-named key never arrives. The leak path is everything else: a key in a URL query string, inside an error message, or under an innocent name like config. Open your error tracker and search events for the key’s value prefix (sk_, sb_secret), not just for field names.

5. The places your AI tool cannot see

Your coding tool sees the current repo. It does not see:

  • Old preview deployments. Vercel’s Standard Protection puts preview URLs behind login (their deployment protection docs have the options), and Netlify has similar protection. Test yours: open an old preview URL in a private window. If it loads, so does last month’s bundle, with last month’s keys.
  • Serverless function environment variables. Check your host’s dashboard for variables set long ago and forgotten.
  • Mobile builds. Anything shipped in an app binary is extractable from the binary.

Where the key goes instead

When the AI added VITE_ to fix “the server can’t read this key”, the real fix was never a prefix. Secret keys belong on the server, behind a function the browser calls: a Supabase Edge Function with the key as a function secret; in Lovable the key goes in More, Secrets. The browser never sees the key, only the answer.

Found one? Rotate before you delete

Deleting the key from your code does nothing about the copies already out there, and revoking first locks out your own app. The order:

  1. Create the replacement key.
  2. Deploy the server side with the new key.
  3. Revoke the old key.

For Supabase this lives under Settings, API Keys. Three things to know. First, the new sb_secret_... keys return 401 in a browser because Supabase matches on the User-Agent header (their API keys doc says so). That stops casual misuse, and “works in curl but not in my app” becomes a sign the key is the secret one. It does not stop curl, so a leaked secret key is still a leaked secret key. Second, a leaked legacy service_role key takes more: create a new secret key, move every backend to it, deactivate the legacy keys (which retires the legacy anon key too, so switch the frontend to the publishable key first), then revoke the legacy JWT secret. Supabase’s rotate a leaked API key guide walks the order. Third, the legacy anon and service_role keys are deprecated in early 2027, so rotate onto publishable and secret keys, not onto keys that are going away.

A prompt to start with

Scan this repository for secrets: every environment variable and where each one ends up (client bundle, server only, git history), every hardcoded key or token, and every place a secret could reach the browser. Run git log -G for the key prefixes and grep the built dist folder, do not just read the source; you cannot see what the bundler inlined by reading src. For each, say whether it is safe by design or a leak, and how to rotate it if it is a leak. Do not change any code yet.

The one check an engineer does by hand

The bundle search from step 1: two minutes, no tooling, the same search the other side runs.

If you have not run the 20-minute security check yet, start there: it catches the open database these keys usually point at. And if your Supabase keys are the safe public kind, row level security is what keeps them safe.

Moving a Bolt project out to run it yourself? The VITE_ prefix is the easiest way to leak a key by accident. Leaving Bolt has a test that checks for it.

The Get AI Native ebook goes deeper on this. It is free, and the expected launch is around Christmas. Join the waitlist on the homepage.

Comments