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

Check your live site in 2 minutes

  1. Open your deployed app, DevTools > Sources (or Network > JS files).
  2. Search all files (Ctrl/Cmd + Shift + F) for common prefixes: sk-, sk_live_, sk_test_, service_role, sb_secret_, AKIA, ghp_, re_.
  3. Also check whether .map source map files are being served. They expose your original source code. Vite does not generate them unless build.sourcemap is enabled.

Which keys are OK in the frontend?

Fine in the browserServer 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

  1. 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.
  2. 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.
  3. 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.
  4. Rename the variable to drop the VITE_ prefix in every .env* file and in your host's settings, and remove any define that injects the whole environment.
  5. 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.

If the leaked key is your Supabase service role key, follow this guide instead. It bypasses all RLS and needs to be handled first. A public Storage bucket is a related leak; see how to close a bucket that was left public.

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