What you see
Your Lovable, Bolt, v0 or Cursor app calls OpenAI, Stripe or Supabase directly from the browser, and it works. Then one of these happens:
- You (or a scanner, or a stranger in your inbox) find
sk_live_...,sk-proj-...orsb_secret_...in a file underdist/assets/or in the Sources tab of DevTools. - Opening
https://your-site.com/.envdownloads a file full ofNAME=valuelines. - There’s a
.mapfile next to every JavaScript file, and DevTools shows your original source,.envvalues included. - Your OpenAI or Google Cloud bill jumps and you didn’t do anything.
That last one is not hypothetical. In anthropics/claude-code #52008, a solo founder describes wiring a Gemini key into a Vite app as VITE_GEMINI_API_KEY: “Within days bots scraped the bundle, extracted the key, and ran up ~€800 of Google Cloud charges” before they noticed and rotated it. They thought they had a spending limit; it was only a billing alert.
Why it happens
Prefixed variables are copied into the JavaScript
Front-end frameworks only hand environment variables to browser code if the name has a public prefix, and they do it by pasting the value into the bundle at build time:
- Vite (Lovable, Bolt and most React SPAs): the docs say variables prefixed
VITE_“will be exposed in client-side source code after Vite bundling”, and “VITE_*variables should not contain sensitive information such as API keys. The values of these variables are bundled into your source code at build time.” - Next.js (v0 and many Cursor projects): a
NEXT_PUBLIC_value is inlined at build time “into any JavaScript sent to the browser”. - Expo: “Do not store sensitive info, such as private keys, in
EXPO_PUBLIC_variables. These variables will be visible in plain-text in your compiled application” (Expo docs).
The prefix isn’t a security setting, it’s the opposite: it means “publish this”. So when an AI builder writes import.meta.env.VITE_OPENAI_API_KEY to make a call work, the key ships to every visitor. Keeping .env out of git doesn’t help, because the value is already inside the built file. In eyeix/meilisearch-ui #161, a user set VITE_SINGLETON_API_KEY, ran pnpm build, and found the Meilisearch master key in dist/assets/zustand-B4CGb1Vr.js.
A .env file inside public/
Vite copies everything in public/ “to the root of the dist directory as-is” (static asset docs). A .env (or .env.production, or a backup copy) saved there is deployed with the site and served at /.env. The same happens if your host publishes the project root instead of the build folder.
Source maps in production
Source maps let DevTools show your original code. Vite’s build.sourcemap defaults to false; true writes a separate .map file per chunk, and 'hidden' “works like true except that the corresponding sourcemap comments in the bundled files are suppressed” (build options), so the .map files are still there to upload. Next.js keeps browser source maps off in production “to prevent you leaking your source on the client” unless you set productionBrowserSourceMaps, and then it serves them next to the JavaScript. The bug report in OpenNSWM-Lab/FAROS #28 shows both problems together: VITE_API_KEY is “a string literal in dist/assets/*.js”, and because vite.config.ts sets sourcemap: true, “it will also sit in the published .map files in readable form.”
Which keys are fine in the browser
Not every key is a secret. These are designed to be public:
- Supabase publishable key (
sb_publishable_...) and the legacy anon key. Supabase’s API keys page says the publishable key is safe for “anything you ship”, and it only reaches what Row Level Security allows. That’s why your RLS policies matter. - Stripe publishable key (
pk_live_...,pk_test_...). Stripe’s keys docs: “Only publishable keys are safe to expose outside your application’s back end.”
These must never be in browser code: Supabase secret keys (sb_secret_...) and the legacy service_role key, which “bypass Row Level Security”; Stripe secret (sk_...) and restricted (rk_...) keys; and AI provider keys. OpenAI’s key safety guide says: “Never deploy your key in client-side environments like browsers or mobile apps.”
Find out what’s exposed
Do this from your project folder after a fresh build. It prints file names, not the keys:
npm run build
# common secret prefixes (add your own provider's)
grep -rlE "sk_live_|sk_test_|rk_live_|sk-proj-|sk-ant-|sb_secret_|service_role" dist/
# dotfiles and source maps that would be deployed
find dist -name ".*" -o -name "*.map"
Use build, out or .next/static instead of dist if that’s your output folder. Then check the live site:
- Open
https://your-site.com/.env. If you seeNAME=valuelines, the file is public. If you see your app’s HTML, that’s the SPA fallback answering, not a leak. - In DevTools > Sources, look for
.mapfiles or a folder tree of your originalsrc/files. - Run the free production-readiness checker on your live URL. It downloads the JavaScript your homepage loads and flags secret key patterns (Stripe, OpenAI, Anthropic, Supabase secret and service_role keys, and others) with a masked preview, so you can see what a stranger sees.
Fix it
1. Rotate the key first
Removing the key from your code doesn’t help if someone already copied it. GitHub’s guide to removing sensitive data puts it plainly: “as a first step you need to revoke and/or rotate that secret.” The same applies to a bundle, an old deploy, or a cached .map file.
- Stripe: on the API keys page, open the ⋯ menu on the key, choose Rotate key, and pick an expiration. By default the old and new keys both work for up to 7 days; for a compromised key, choosing Now deletes the old key immediately (Stripe docs). Copy the new live key when it’s shown, because you can’t reveal it again later.
- Supabase: create a new secret key under Settings > API Keys, switch your server code to it, then delete the old secret key. If the leak was the legacy
service_rolekey, deactivate the legacy keys and move your frontend to a publishable key. If someone is actively using the leaked key, Supabase says to retire it right away even though things break until you replace it. - OpenAI and other AI providers: “If you believe your key has been leaked, rotate your key immediately from the API Keys page” (OpenAI). Check the usage page for calls you didn’t make, and set a hard spend limit, not just an alert.
Supabase also warns to fix the root cause before you rotate, or the new key leaks the same way. In practice: rotate now, do step 2 before you paste the new key anywhere.
2. Move the call to server-side code
The browser should call your function, and your function calls the provider with a key that has no public prefix. Vite’s docs suggest exactly this: “consider a backend server or serverless/edge functions to properly secure secrets.”
With Supabase Edge Functions, store the key as a function secret and read it with Deno.env.get (Edge Function secrets):
supabase secrets set OPENAI_API_KEY=sk-proj-... # the NEW key, in your terminal, not in code
// supabase/functions/ask/index.ts
Deno.serve(async (req) => {
const { question } = await req.json();
const res = await fetch('https://api.openai.com/v1/responses', {
method: 'POST',
headers: {
Authorization: `Bearer ${Deno.env.get('OPENAI_API_KEY')}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ model: 'gpt-6.2-astra', input: question }), // the model you already use
});
return new Response(await res.text(), { status: res.status, headers: { 'Content-Type': 'application/json' } });
});
The frontend then calls supabase.functions.invoke('ask', { body: { question } }) and never sees the key. Before you go live, make the function check who is calling (a signed-in user, a rate limit), or anyone can use it to spend your credits.
In Next.js, the same thing is a Route Handler (app/api/ask/route.ts) that reads process.env.OPENAI_API_KEY. Without the NEXT_PUBLIC_ prefix the value is “only available in the Node.js environment” (Next.js docs).
A prompt for your AI builder: “Move every call that uses a secret key out of the frontend into a server function. The key must come from a server-only environment variable without the VITE_, NEXT_PUBLIC_ or EXPO_PUBLIC_ prefix. Keep only the Supabase publishable/anon key and the Stripe publishable key in browser code. Never paste key values into this chat.”
Then delete the old VITE_... / NEXT_PUBLIC_... secret variables from your .env files and your host’s dashboard, so the next build can’t pick them up again.
3. Stop shipping .env files and source maps
- Move any
.env*file out ofpublic/, and check your host publishes the build folder (dist,build), not the repo root. Netlify, for example, only deploys the publish directory (dist does not exist guide). - In Vite, remove
build.sourcemap: true(the default isfalse). If you need source maps for an error tracker, upload them there and don’t deploy the.mapfiles. In Next.js, leaveproductionBrowserSourceMapsunset. - Redeploy, then repeat the checks above:
/.envshould be gone and the.mapfiles should 404.
4. Add a guardrail so it doesn’t come back
- Netlify: mark the variable as secret (Contains secret values). Netlify’s secret scanning then searches the repository code and build output for that value (plain, base64 and URI-encoded) and fails the build if it finds it, before anything is published.
- Any host: run a secret scanner such as Gitleaks on the build folder and the git history in CI. If the key was ever committed, it’s still in history, which is another reason step 1 comes first.
- GitHub: push protection blocks pushes that contain recognized secrets to public repositories by default.
Check it worked
- Call the provider with the old key: it should be rejected. For Stripe,
curl https://api.stripe.com/v1/charges -u sk_live_OLD:(the authentication docs use this form) should fail. - Build again and re-run the
grepabove: no matches. https://your-site.com/.envshows your HTML fallback or a 404, and no.mapfiles are served.- The feature still works on the live site, now going through your server function.
- Run the checker again: the “Secret keys in your JavaScript” check should pass.
A key in the bundle is rarely the only shortcut an AI builder took. If you want someone to go through your keys, Row Level Security, server functions and payment flow before real users arrive, get a written pre-launch audit. It starts with a quick check, and the audit + fix tier includes making the changes.