Back to results for “Environment Variables & Configuration Management”
Next.js › Environment Variables & Configuration Management

What security risks are associated with environment variables, and how do you avoid them?

Advanced

Answer

The biggest risk is accidentally exposing a secret to the browser by using the NEXT_PUBLIC prefix on something sensitive, or committing an env file containing real secrets to version control. You avoid this by carefully reviewing which variables are public, adding your env files to .gitignore, and using your hosting platform's secret management for production values.
# .gitignore
.env.local
.env.production.local
Real-world example A company accidentally committed a database password to their git repository history, so they rotated the credential immediately and added stricter rules to prevent env files from being tracked in the future.

Common follow-ups: What should you do if a secret is accidentally committed to version control?;How do secret management tools differ from plain environment variable files?