Why a VITE_ prefix puts your secret in the browser
Vite builds your front end into plain files that every visitor downloads. Anything that ends up in those files can be read by anyone who opens the browser's developer tools. Vite has one rule about which environment variables get in, and it is easy to trip over.
The rule
Vite only copies variables whose names start with VITE_ into the code it sends to the browser. Variables without that prefix stay on your machine. The prefix is a signal that says "this value is public."
So a variable named VITE_STRIPE_PUBLISHABLE_KEY is fine, because that key is designed to be shown to visitors. A variable named VITE_OPENAI_API_KEY is a leak: the value is written into your JavaScript and anyone can copy it.
Next.js works the same way with the NEXT_PUBLIC_ prefix, and other tools have their own versions.
Why this goes wrong
A page needs to call a service, and the call fails in the browser with a "key not found" error. The quickest way to make it work is to rename the variable so it starts with VITE_. The error disappears, the feature works, and the secret is now public.
Hosting settings do not change this. The value may be stored as a private setting on your host, but the build reads it and writes it into the files anyway.
Which keys are safe in the browser
Some keys are meant to be public:
- Publishable keys from payment services, usually marked with the word publishable.
- Map keys that you have restricted to your own domain.
- Analytics IDs.
- The public key of a database service, as long as access rules are switched on for every table.
Some keys must never be in the browser:
- Secret or server keys for payment services.
- AI service keys, which charge your account for every request.
- Email service keys, database admin keys and private tokens.
- Anything described as secret, private, admin or service role.
If you are unsure, read the provider's documentation for that key. It says whether the key is safe to publish.
Step 1: List your VITE_ variables
Search your project for the prefix:
grep -rIn "VITE_" .env* src vite.config.* 2>/dev/null
Read each name and ask: would you be comfortable printing this value on your home page? If not, it must not have the prefix.
Step 2: Look at the built files
The built files are the truth. Build the project, then search the output:
npm run build
grep -rIlE "sk_live|sk-[A-Za-z0-9]{20,}|secret|private_key" dist/
A search like this is only a starting point, because keys come in many shapes. Open the files it lists and read around the match. You can also open your live site, press F12, go to the Sources tab, and search all files for the name of a service you use.
Step 3: Move the call to a server
A secret key belongs in code that runs on a server. The browser asks your server, and your server calls the service with the key.
- Create a small server route or serverless function that makes the call.
- Store the key in your host's environment settings under a name with no
VITE_prefix. - Change your front end to call your own route instead of the service.
- Add limits to that route so strangers cannot use it freely: require sign-in where it makes sense, and limit how often one caller can use it.
The last step matters. A route that anyone can call and that spends your money is only slightly better than a leaked key.
Step 4: Rotate any key that was public
If a secret was in the bundle, assume it has been copied. Create a new key in the provider's dashboard, put it on the server, and delete the old one. Then check the provider's usage page for requests you did not make.
What to do today
- List every
VITE_variable in your project and in your.envfiles. - Rename any variable that holds a secret so it has no prefix, and move its use to a server.
- Build the project and search the output folder for the names of the services you use.
- Rotate every secret that was ever shipped to the browser.
- Add sign-in or usage limits to the server routes that call paid services.
Check your own site
Attack Shield looks for these mistakes on your website and shows how to fix each one.
Scan your site