ContactBook a call

Security

Exposed API keys in AI-built apps: find and fix them

How API keys leak from Lovable, Bolt, v0, Replit and Cursor apps, how to search your bundle and Git history for them, and how to rotate and move them safely.

Cover for “Exposed API keys in AI-built apps: find and fix them”, with the line: A key in browser code belongs to everyone who opens the page, and deleting it later does not take it back.
A key in browser code belongs to everyone who opens the page, and deleting it later does not take it back. Image: SBD.

An API key is exposed when it reaches code that runs in a visitor's browser or a repository other people can read. In AI-built apps this usually happens because a payment, email or AI key was put in an environment variable whose name tells the build tool to ship it to the browser, such as one starting with VITE_ or NEXT_PUBLIC_. Anyone who opens developer tools can copy it. The fix has three parts: move the call that uses the key onto a server, rotate the key with the provider, and check nobody has used it in the meantime. You can search for exposed keys yourself in under an hour.

This post is for founders and product leads with an app built in Lovable, Bolt, v0, Replit, Cursor or a similar tool. It explains how keys leak, how to find them, how to fix a leak properly, and when to bring in help.

The short answer

Build your app, then search the output folder for the first characters of every secret key you use, along with common prefixes such as sk_, sk-, re_ and sb_secret_. Run a secret scanner over your Git history as well. For each key you find, rotate it with the provider first, then move the code that uses it into a server or edge function. Check the provider's usage logs for calls you did not make.

Which keys are meant to be public

Some keys are designed to sit in the browser. The Supabase publishable key (the anon key on older projects), a Stripe publishable key starting with pk_, and a Google Maps key restricted to your domain are all expected to be visible. Their safety comes from somewhere else: row-level security for Supabase, and restrictions set in the provider’s dashboard for the others.

Secret keys are different. A Stripe secret key, a Resend or SendGrid key, an OpenAI or Anthropic key, and the Supabase secret or service role key all act with full authority on your account. Supabase’s documentation says its secret key bypasses row-level security and must never be placed in a browser or source control. Whoever holds one of these can send email as you, charge or refund through your Stripe account, or run up your AI bill.

How keys leak in AI-built apps

There are four routes we see repeatedly.

Public environment variable prefixes. Build tools copy certain variables into the browser bundle on purpose. Vite exposes anything starting with VITE_, Next.js exposes anything starting with NEXT_PUBLIC_, and Expo does the same with EXPO_PUBLIC_. When an AI builder needs a key to make a feature work in the preview, putting it in one of these is the quickest route, and the key then ships to every visitor. Bolt’s documentation makes the distinction clearly: its Secrets are available only to server functions, while values in the project’s .env file can be read by browser code.

Keys pasted into code. Someone pastes a key into a prompt, and the builder writes it straight into a source file. It then sits in the repository and in the bundle.

Keys in Git history. A key removed in a later commit is still in the history. If the repository was ever public, or is shared with contractors, the old commit is readable.

Keys in logs and error messages. A server function that logs the full request or returns a raw error can echo a key back to the browser or into a logging tool more people can see.

How common this is

GitGuardian scans public GitHub every year. Its State of Secrets Sprawl report, published in March 2025, counted 23,770,171 new hardcoded secrets added to public repositories during 2024, and found that 70% of secrets it first detected in 2022 were still valid when it checked again. These are GitGuardian’s own figures from its own scanning method, and they cover public GitHub as a whole, so read them as a sign of scale rather than a rate for AI-built apps. The second figure is the one to remember: most leaked keys are never rotated.

The checks to run yourself

Set aside an hour. You need the code, a terminal and access to each provider’s dashboard.

  1. List your keys. Write down every external service the app uses and which key each one needs. Mark each key as public by design or secret.
  2. Read your environment variables. In your builder or host, list every variable. Any secret with a VITE_, NEXT_PUBLIC_ or EXPO_PUBLIC_ name has shipped to browsers.
  3. Search the built bundle. Run your production build, then search the output folder (dist for Vite, .next/static for Next.js) for sk_, sk-, re_, sb_secret_, service_role and the first six characters of each secret on your list. Anything found is public.
  4. Check the live site. Open your app, open developer tools, and search the loaded JavaScript in the Sources panel for the same strings. Some hosts inject variables at runtime, so the live site can differ from your local build.
  5. Scan the Git history. Run an open-source scanner such as gitleaks or TruffleHog over the full repository history. It will find keys removed from current files but still present in old commits.
  6. Turn on push protection. GitHub’s push protection blocks pushes that contain recognised secrets. GitHub’s documentation says it is on by default for your personal account when you push to public repositories, while push protection for a repository has to be switched on. If your host offers build scanning, as Netlify does, leave it on.
  7. Check provider logs. For each key you found exposed, look at the provider’s usage or API logs for calls from places you do not recognise.

Fixing a leaked key properly

Order matters. Rotate first: create a new key, update the server with it, then revoke the old one. Deleting the key from your code without rotating it leaves a working key in every copy of the old bundle, in caches and in Git history. GitHub’s guidance on removing sensitive data says the same, and treats rewriting history as a secondary step after the credential is revoked.

Then move the code. The call that uses the key goes into a server function: a Supabase Edge Function, a Bolt server function, a Next.js server action or route, a Netlify or Vercel Function, or your Replit app’s server. The browser calls your function, your function checks who is asking, and only your function holds the key.

Finally, limit the damage of any future leak. Use restricted keys where the provider offers them (Stripe does), set spending limits on AI providers, give development and production separate keys, and keep one key per service so a leak means rotating one thing.

What the checks do not cover

Finding and moving keys closes the most expensive leak. It does not check that the new server function verifies the caller, that the database rules are right, or that a function cannot be called a thousand times a minute to run up the same bill another way. A function that forwards any prompt to OpenAI for anyone who asks can cost you as much as the leaked key did.

When to get a second pair of eyes

If you have found a live secret key in a public bundle, rotate it today and then get the rest of the app checked, because the conditions that put it there usually produced other gaps. Our vibe-coded app audit starts at $500, and you get a written report within a week covering keys, database rules, auth, storage, rate limits and backups. Harden starts at $1,000 if you want us to make the fixes, and Rebuild starts at $8,000 for apps that need a proper server layer.

For checks specific to your builder, see Your Lovable app is probably public, Is my Bolt.new app secure?, Taking a v0 app to production, Replit app to production and Cursor app security review. For database rules, see Supabase row-level security for AI-built apps, or go straight to the audit page.

Questions we get asked

My repository is private. Is a key in the code still a problem?

Less urgent, but yes. Everyone with access to the repository, including former contractors and any tool connected to it, can read the key, and a key in the code usually ends up in the bundle too.

Can I hide a key by encoding or splitting it in the front end?

No. The browser has to reassemble the key to use it, so anyone can do the same. Keys that must stay secret belong on a server.

I rotated the key. Do I still need to rewrite Git history?

Once the old key is revoked it is useless, so rewriting history is tidy-up rather than protection. Do it if the repository is public or will be shared.

Which keys matter most?

Anything that can move money, send messages as you or spend on your account: payment, email, SMS and AI keys, and any database key that bypasses access rules.

Sources and further reading

  • Vibe coding
  • Security
  • MVP audit

Keep reading

Cover for “Cursor app security review: what to check”, with the line: Review the app, the packages it pulled in, and the files that tell the editor what to do.

Security

Cursor app security review: what to check

Reviewing an app built with Cursor: secrets in the repo, auth on API routes, hallucinated packages, rules file and MCP injection, and the 2025 Cursor CVEs.

Cover for “Is my Bolt.new app secure? The checks to run”, with the line: The database is reachable from the browser, so the rules on each table decide who sees what.

Security

Is my Bolt.new app secure? The checks to run

How to check a Bolt.new app before launch: row-level security on Supabase or Bolt Database, keys in the bundle, Netlify secrets and the Security page.

Cover for “Replit app to production: a checklist”, with the line: Keep the agent working on a copy, and keep production somewhere it can only read.

Security

Replit app to production: a checklist

Taking a Replit Agent app live: separate dev and production databases, production secrets, backups, and what the July 2025 database deletion taught.

Find out what is worth automating

About 95% of AI pilots never reach the profit line. We start with the work your team still does by hand and measure each build by the hours it saves.

See how we work

Cookie preferences

Choose what this site may use on your device. You can change this at any time from “Cookie settings” in the footer.

Strictly necessary

Keep the site working and remember the choices you make here. These cannot be switched off, and none of them track you.

Always on
What this includes (4)
NameProvider PurposeDuration
sbd-consentThis site Remembers your cookie choices12 months
Vercel Web AnalyticsVercel Inc. Counts page views in aggregate. Sets no cookie and stores nothing on your deviceNone
ticker-dismissed, sbd-note-offThis site Remembers that you closed a notice or language tipUntil the tab closes
sbd-voice-resume, sbd-voice-prefillThis site Carries a voice conversation across pages, only if you start oneUntil the tab closes

Analytics

Tell us which pages are read and where visitors leave, so we can improve them. IP addresses are anonymised and nothing is used for advertising.

What this includes (2)
NameProvider PurposeDuration
_gaGoogle Analytics (Google LLC) Distinguishes visitors with a random ID13 months
_ga_FPXDMQPRV1Google Analytics (Google LLC) Keeps the session state for this site13 months

Functional

Embedded services that set their own cookies. Without them the site still works; you get a link to open the service instead.

What this includes (1)
NameProvider PurposeDuration
Booking calendarCal.com Inc. Shows live availability and books the call on the booking pageSet by Cal.com

Advertising

Not used. This site has no advertising, retargeting or social media tracking, and we never sell data.

Not used

Questions about your data: hello@smallbaydigital.com