.env and the Consequences of Its Usage

.env and the Consequences of Its Usage

Daily short news for you
  • Lately, there's a new "fork" of VSCode, everyone trae.ai 😆. I've heard that this is a product of ByteDance - the parent company of TikTok.

    I've just skimmed through it, and there's no pricing yet, so it seems like they are offering it for free with no limits. After installation, when I opened it up, the introduction section looked very similar to Windsurf, except the interface, once it has been touched by the "wizards," has a distinctly different vibe that you can't mistake for anything else.

    I haven't seen anything outstanding in this version at all, and it's even less smooth than Windsurf. Oh, and technically, everyone can come here to ask about Claude Sonet 3.5 or GPT-4o as well 😆

    » Read more
  • Wow! This is really cool, everyone. helicone.ai is an open-source tool used to track and record all activities related to API calls to LLM platforms like OpenAI, Gemini, Groq... and many more.

    Why use it? Simply because it helps you document every single detail of each API call. Including content, responses, success or failure, the number of tokens used... and a few other pieces of information. This way, it helps you manage quality and control how the system is operating, whether it's effective or not!

    Oh! It's open-source, everyone. You can self-deploy or use their limited free version in the cloud 😁

    » Read more
  • Deepseek has not yet passed, but Moonshot AI Kimi k1.5 has arrived. I don't know who's showing off to whom, so I'll just leave some pictures here for everyone to evaluate.

    The first image is the reasoning model, the second image is the character guessing model. And the bright blue column on the far left, well, you all know who that is 🫣

    » Read more

Issue

There's advice that any configuration that can change depending on the deployment environment should be turned into environment variables (OS Environment). Typically, we use a .env file to store these variables. The .env file is usually placed in .gitignore and is only created when deploying the application to the Internet environment or running it locally. A .env file may look like this:

DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=password
...

However, sometimes we need more flexibility with .env, where it can change based on an environment variable passed in. For example, it could change through the NODE_ENV variable. In that case, we have .env.local, .env.development, .env.production, and when running, you only need NODE_ENV=local node index.js, and .env.local will be automatically used.

Another solution besides using .env is to store configuration in any file format such as JavaScript, JSON, yml... using it is similar to .env, but it allows nested configurations for clearer configuration partitioning. However, both methods still create files to store environment variables.

Currently, I'm still using .env in some of my projects, and I can't deny its convenience. However, .env is not without its drawbacks.

Firstly, there's the issue of security. .env files are no different from unencrypted text files, so anyone can read them. People often copy or send .env files directly to each other through messaging apps, potentially exposing or intentionally leaking the content.

Secondly, .env files don't have access control, meaning anyone with permission can access them. This inadvertently reveals the content of all variables. For example, if a fellow developer requests permission to start a project locally pointing to the staging environment, we may have to send the entire .env content for the staging environment.

Since .env is considered sensitive, it's usually not committed to Git. This makes it difficult to track changes to variables in .env through this tool. Instead, we often add a file like .env.example to store the possible values in .env. However, this approach only provides insight into the structure of .env without managing its content. Adding a new variable and forgetting to update it in other environment files can lead to application errors.

It can be disastrous if we accidentally commit .env to the project or forget to exclude it in .dockerignore. This is a human error but is one of the issues that can cause security leaks if not detected early.

So, do we have a better solution?

Alternative to Traditional .env

With the existing advantages mentioned above, we naturally have many alternative solutions to efficiently manage environment variables. One such solution is Azure Secrets Manager or Vault. However, these tools seem more suitable for complex configurations and future scalability. If you need a simpler tool focused on managing security variables, Infisical is a much easier and time-saving solution.

Infisical is an open-source end-to-end encryption solution that can be used to synchronize environment variables within a team and infrastructure. By using Infisical, we can address most of the issues mentioned at the beginning of the article.

The source code of Infisical can be found at Infisical Github, and we can deploy our own server using various methods such as Docker, AWS, DigitalOcean, etc. Alternatively, Infisical provides a completely free Cloud service with some limitations. Interested readers can sign up at Infisical Signup.

Infisical

Infisical supports many Cloud services like AWS, Vercel, Netlify, or Frameworks/Libraries like React, Vue, SvelteKit, etc.

For example, in Node.js, after adding environment variables to Infisical, you can start a Node.js project by:

$ infisical init
$ infisical run -- npm start

At this point, environment variables will be passed into the Node application.

To see a full list of integrations and usage in other projects, readers can check Infisical Integrations.

Conclusion

.env is one of the ways to manage environment variables when deploying applications to different environments. However, the approach via .env is somewhat outdated and can lead to various issues. Infisical can be considered a simple and more efficient solution compared to Vault or Azure Secrets Manager.

Premium
Hello

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (1)

Leave a comment...
Avatar
Ẩn danh1 year ago
Hay lắm a ơi. Mong a ra tiếp những bài viết hữu ích. Cảm ơn a
Reply
Avatar
Xuân Hoài Tống1 year ago
Cảm ơn e, nhớ ghé thăm blog thường xuyên nhé :D
Scroll or click to go to the next page