Quote
Hi @all, very new one from me about
React
content LOL ๐. Today, I will talk about the problems occur when i try to build container forReact
project and release application to Production, your environments secrets actually expose on bundle code ๐ฑ, so how the heck way to protect these ? Sorry if my knowledge aboutReact
does actually that mean or not. Letโs try and digest !
Problems
Bug
Ever you seen about your
3rd
party API key expose on your web browser, and yup it really exists and a lots.
You can use devtools
to inspect and view index-xxxx.js
, it can bundle
file which compress all of module from node
and your src
into one file, and expose to client side
So Itโs not safe, yup it really not and this actually not recommendation when you want to put anything secrets (such as Private API keys) to .env
variables, for example. You can read more at Adding Custom Environment Variables, to understand why you shouldnโt
But there is not way to can hidden that, and the answer is yes, โYou canโtโ. But you can do a little trick when you want .env
with contains some little secrets like clientid
or backendURL
. I will describe you about vitejs
can help us doing that
Solutions
The solutions which i prefer to you is about vitejs
, the build tools with fascinating the performance for static file like html
, css
and js
With react
, you can simply create react project with command
After completely, you will have directory with structure
That for all, setup is already. Next you need to know about .env
file and environments in Vite
which canโt make different. Read more at VITE - Env Variables and Modes
Info
Vite exposes env variables on the special
import.meta.env
object, which are statically replaced at build time.
Info
Loaded env variables are also exposed to your client source code via
import.meta.env
as strings.To prevent accidentally leaking env variables to the client, only variables prefixed with
VITE_
are exposed to your Vite-processed code. e.g. for the following env variables
For example, if you have
and when you try log to console, the VITE
prefix variables will expose to client side but if not have prefix, your variables will not expose, it mean you can try use this trick to provide hidden way to load environment to your application, bundle it and no exposing anything
If you know you know, any language will exist dotenv
library which help us provide and get variables from .env
file into application, React
with Vitejs
can do same thing. You can install with command
The technologies of dotenv
base on The Twelve-Factor Appย methodology. Storing configuration in the environment separate from code
When you have applied and installed dotenv
, go to vite.config.ts
and import some code to provide the way vite
resolve the library
With provide TEXT_NO_EXPOSE
in your .env
file
After that you can reach to src
directory to declare global variables which can help react
app use the environment, create globals.d.ts
file inside
When you build application with vite
, your source will bundle into static
file include html
css
js
, you can run these commands to perform build
And lastly, you can preview your application with preview
mode of vite
Usually access http://localhost:4173/
on browser
Thatโs default project of vite
but reach to source code to inspect does expose anything or not, finding in the chunks js
file with some context
__ENV_SECRET__
TEXT_NO_EXPOSE
With two context does exist in source code, reason because you donโt use them in app.tsx
but base on theoretical
your .env
is actually burn to your applications, and it reads this in runtime. What does it mean ? When you set __ENV_SECRET__
in app.tsx
, your secrets will mark inside
Run build
again, view on browser your secrets is actually exposing
But when you find context in source, it will not in the source code, just variables will map into
Itโs mean when you run it on build
, it will get variables from .env
file and map to bundle js
, It likes envsubst
in Linux command. Read more at: Linux envsubst Command with Examples
Success
Therefore, with
vite
for specify andreact
for general, you will hard to put your.env
to your source code when you want tobuild
to Production application with bundlejs
. Solution in here, you need clarifymiddleware
which stand in middle between your server hostfrontend
andclient-side
, with exchange the data between them to retrieve the sensitive variables
Another ways
But wait and letโs try, dotenv
support us one tool call dotenv-vault
, here is documentations, with vite
, I will follow up this vite documentations
Following the documentation, you need to install dotenv-vault
or simply use it with npx
, but first of all, you need to create the account by accessed here. If you have account, go to create a new vault repository
Click Add Button to approve, you will have 2 option push
and pull
, but first push your .env
to dotenv
Your actually file .env.vault
with update on UI, you can sync with build
command
.env.vault
will sync with the version on the cloud, so you do not need to set .env
, try get anything with only vault
file (NOTE: you can push this file to repository, AES256
๐ฎโ๐จ), just use command to get the key to decrypt .env
base on your decision
Usually, It will exist 4 environments, but you can create customize
to add your environments
To get the key, you can use command
Output can be
Your prepare is also already, you can set keys output to shell variables, and use it for build
and run development
mode
With Linux
With powershell
Trigger build
progress, and you will see the info to target your .env.vault
to cloud .env
Preview and take a look your variables will change and base on environments decision set on DOTENV_KEY
, not anything secrets in your source code and bundle will not include your secrets but you need set DOTENV_KEY
to decrypt
Therefore, if you integrate process.env
solutions, I think your secrets will wipe out or put somewhere not on application ๐๐๐
Success
Nothing easy, maybe I am wrong or not but put the
.env
and useapikey
in Client side shouldnโt recommend, It means you need to be createdmiddleware
orbackend
side to retrieve the3rd
party applications
Source code to small demo: react-bundle-secret
Conclusion
Note
That all for today, happy to comeback and learn something about
development
skills. But that need for mydevops
and understand why you need to recommenddev
not to put anythingapikey
orsecretkey
on Client Side. Make sure you havemiddleware
or put responsibility forbackend
to handling that job. Maybe i do trust the way or not know but that is best practice, that need to target ๐ ๐ ๐
Quote
Bring back the new things is my happiness and pleasure, so keep go in and learn to new thing, stay safe, wellness hacking and I will meet you on next topics. See yahh @all !!