Secret env keys in client #10584
-
Hii, I want to use some secret keys loaded from .env in client side but those are not public urls So, adding env to the script, leaking the env keys through elements and window object in browser I tried a way like sending secret env through loader --> This is working fine and not leaking data in window object but the problem is loader calls are visible in network tab. Can anyone help me out here, Your help & time is greatly appreciated !! Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
There's no way to send env variables to the browser without leaking them. If you inline them in the code at build time, they will be leaked in the source code When you send them to the browser you're leaking them, you could decide to encrypt them before sending them, but then to decrypt them you have to leak the key used to decrypt, which allows anyone to decrypt it too. If you don't want to leak them, the safest thing is to do whatever you need server-side and send the result to the browser. E.g. if your keys are API keys use them to fetch the API server-side and send the API response data as a result. |
Beta Was this translation helpful? Give feedback.
In that link they say's the key is designed to be public, so there's no issue in sending it to the browser.
They even say that unless your repo is public you should inline the key in the code.
So don't overthink this, keep it in the code or if you use an env variable return it from your loaders, or even use
VITE_
prefixed env variables to hard-code it at build time.