Exposed API keys in a Vite / React bundle: why VITE_ variables are public and how to fix it
By AI App Medic · Updated · Security · Vite · API keys
Lovable, Bolt and most Cursor-built frontends use Vite. Vite only exposes environment variables whose names start with VITE_ to browser code, and it does so by pasting the value into the built JavaScript. Minification does not hide strings. If a secret has a VITE_ prefix, every visitor can read it.
How secrets usually end up in the bundle
- The AI got
undefinedforimport.meta.env.OPENAI_API_KEYand "fixed" it by renaming it toVITE_OPENAI_API_KEY. - The frontend calls a paid API directly (OpenAI, Anthropic, Resend, Stripe secret API). Then the key has to be in the browser, whatever it is called.
vite.configusesloadEnv(mode, process.cwd(), '')and passes the result intodefine, which inlines every variable.- A key was hard-coded "just for testing".
Check your live site in 2 minutes
- Open your deployed app, DevTools > Sources (or Network > JS files).
- Search all files (Ctrl/Cmd + Shift + F) for common prefixes:
sk-,sk_live_,sk_test_,service_role,sb_secret_,AKIA,ghp_,re_. - Also check whether
.mapsource map files are being served. They expose your original source code. Vite does not generate them unlessbuild.sourcemapis enabled.
Which keys are OK in the frontend?
| Fine in the browser | Server only |
|---|---|
Supabase URL and anon / sb_publishable_ key (with correct RLS) | Supabase service_role / sb_secret_ key |
Stripe publishable key (pk_live_, pk_test_) | Stripe secret key (sk_), restricted keys (rk_), webhook secrets (whsec_) |
| Browser keys the provider lets you restrict by domain (for example some maps APIs) | OpenAI, Anthropic, Gemini and most AI keys; email/SMS keys; database URLs |
The fix, in order
- Revoke or rotate the key at the provider now. Removing it from the code does not un-publish it. Check the provider's usage page for unexpected spend.
- Move the call to a server. With Supabase, create an Edge Function that stores the key as a secret (
supabase secrets set OPENAI_API_KEY=...) and calls the provider. On Vercel/Netlify, use a serverless function. The browser calls your function, never the provider. - Protect the new endpoint. Check the user's session inside the function and add rate limits, otherwise anyone can call your function and spend your credits.
- Rename the variable to drop the
VITE_prefix in every.env*file and in your host's settings, and remove anydefinethat injects the whole environment. - Rebuild and redeploy, then repeat the search above on the live site.
A Vite server.proxy setting does not help in production: it only exists in the dev server, not in the built static files.
FAQ
Are VITE_ environment variables secret?
No. Vite replaces import.meta.env.VITE_* with the literal value at build time, so the value ends up in the JavaScript files every visitor downloads.
Which keys are fine in the browser?
Keys designed to be public: your Supabase URL and anon/publishable key (protected by RLS), a Stripe publishable key (pk_...), and browser keys that the provider lets you restrict by domain.
I removed the key from the code. Am I safe?
No. The key was already served to visitors and may be cached. Revoke or rotate it at the provider first, then remove it and redeploy.
Related guides
- Supabase storage bucket public by mistake: close it and switch to signed URLs
- Netlify build fails with "Secrets scanning found secrets in build": tell a public key from a real leak
- Supabase service role key exposed in your frontend: what to do right now
- Bolt.new works in preview but breaks on Netlify: missing environment variables
- Cursor app Vercel build failed: fix the error that npm run dev never showed