Logo

dev-resources.site

for different kinds of informations.

Next JS might be exposing your backend environment variables

Published at
11/8/2023
Categories
webdev
nextjs
javascript
env
Author
thecodeinfluencer
Categories
4 categories in total
webdev
open
nextjs
open
javascript
open
env
open
Author
17 person written this
thecodeinfluencer
open
Next JS might be exposing your backend environment variables

I recently got embarrassed by next.js at work when some API keys were found on the client-side browser cache. Even though these variables were used on the backend side, they were being exposed and it was quite confusing to me. I later did extensive research and discovered a rule of thumb to use in future to avoid such a scenario in future. I wrote this post to help you avoid such a scenario and to share the rule I came up with.

The problem

Apparently, it is not a good idea to share variables between the client and server. This was the root problem for me. Here's an example of what I did.

I had a backend file called say utils.ts which was getting values from the .env as below:

const environment = {
    someApiURL: `${process.env.SOME_API_URL}`,
    someClientKey: "someClientValue"
};

export default environment;
Enter fullscreen mode Exit fullscreen mode

Then on the client, we use the someClientValue as below:

import environment from "@/utils.ts"

export default function App(){
    const [state,setState] = useState(environment.someClientKey)

    return <div>{state}</div>
}
Enter fullscreen mode Exit fullscreen mode

In the examples above, the client build will also include the someApiURL and even expose its value from the .env to the client. To confirm this, we can open the browser dev tools, go to sources tab and into _next/static/pages/chunks/thepageyourcurrentlyin and try to search the someApiURL. You will find the .env value exposed.

Fix

The fix is as simple as to not mix server variables with client variables. Except that more often than not, you will not be keen enough. For me, I came up with a rule of thumb to separate frontend and backend utility and config functions to different folders so I'm always sure.

This might not seem as a common or significant issue until you expose your company's credentials to potential hackers.

Hope this helps someone.

env Article's
30 articles in total
Favicon
How to Use Environment Variables in a React.js App with Vite
Favicon
next-runtime-env usage in Documenso source code
Favicon
How to Build an Elm Land Project for Production
Favicon
การใช้ GitLab สำหรับแชร์ Configuration ให้คนในทีม โดยไม่ใช้แชท
Favicon
How to create a fullstack application using Django and Python Part 5
Favicon
Exploring DotenvX
Favicon
Setting up a React environment
Favicon
Using environment variables in React and Vite
Favicon
Exploring Node.js 20.6: Built-in Support for .env Files
Favicon
docker-compose will always load .env
Favicon
How to fix 'process' is not defined (React+Vite)
Favicon
Virtual Environments in Python Applications
Favicon
Decoding the Matrix: The Evolution of Environment Variables in Software
Favicon
Use Doppler instead of traditional .env files 🍕
Favicon
Além do básico: Uso de Variáveis de Ambiente em Aplicações Node e Nest
Favicon
Running scripts in Production
Favicon
Better DX for .env
Favicon
Navigating Environment Variables in Flutter Projects: Strategies for Effective Integration
Favicon
TIL - Today I Learn 12-11 18-11
Favicon
Generate a Random JWT Secret Key
Favicon
Next JS might be exposing your backend environment variables
Favicon
Environment variables and configuration anti patterns in Node.js applications
Favicon
Node(20.6.0) now supports built-in .env files
Favicon
How to keep your tokens secret?
Favicon
Criando um comando Artisan personalizado para definir valores de variáveis no .env no Laravel
Favicon
How to Effectively Remove .env File from Git Repo/History
Favicon
Vite environment variable's type casting/transforming
Favicon
Did you know docker-compose only takes environment variables from`.env` only?
Favicon
[HUGO]: How to use variables from .env
Favicon
Python – AWS Secrets Manager: Remote env vars

Featured ones: