Public environment variables
| Class | Secrets |
|---|---|
| OWASP Top 10 | A02:2025 Security Misconfiguration |
| Prefixes | NEXT_PUBLIC_, VITE_, EXPO_PUBLIC_, NUXT_PUBLIC_, REACT_APP_, VUE_APP_ |
| Typical severity | None for values the browser needs; Critical when the variable holds a server key |
| Affects | Front ends built with Next.js, Vite, Expo, Nuxt, Create React App and Vue CLI |
A front-end project keeps its settings as environment variables, often in a .env file, and some of them are needed by the code that runs in the visitor's browser: the address of the API, an analytics ID, a publishable key. Frameworks decide which variables to hand over by their names. A variable whose name starts with an agreed prefix, such as NEXT_PUBLIC_ in Next.js or VITE_ in Vite, is a public environment variable: the build copies its value into the JavaScript bundle, which every visitor downloads as part of the browser code.12 Variables without the prefix stay on the server. The build looks only at the name, never at what the value contains.
Replacement at build time
The copying happens when the app is built, not when a page is requested. Next.js replaces each reference to process.env.NEXT_PUBLIC_... with the literal value found in the environment where next build runs.1 Vite does the same with import.meta.env.VITE_..., which it replaces statically during the build.2 Given this file and code:
# .env
VITE_API_URL=https://api.example.com
PAYMENT_SECRET=example-secret
// src/api.js
const api = import.meta.env.VITE_API_URL;
const secret = import.meta.env.PAYMENT_SECRET;the built file contains "https://api.example.com" where the first reference was, and no value at all for the second, because Vite exposes only prefixed names to client code.2 Next.js, in a module that ends up in the browser, turns an unprefixed variable into an empty string.3 Expo replaces process.env.EXPO_PUBLIC_... in the app bundle the same way, and states that such values can be read in plain text in the compiled app.4
Only a plain dot reference such as process.env.NEXT_PUBLIC_X is swapped. Next.js and Expo leave process.env[name] alone, and also a value read from a copied or destructured process.env.14 The value is also fixed into the files. A single Docker image deployed to staging and production carries the value from the machine that built it, and a change to the variable later has no effect until the next build.1
Prefixes by framework
| Tool | Public variables | Server-only variables |
|---|---|---|
| Next.js | NEXT_PUBLIC_ prefix, copied in at build time | Any other name |
| Vite | VITE_ by default; changed with envPrefix | Any other name |
| Expo | EXPO_PUBLIC_ prefix | Any other name |
| Nuxt | Keys under runtimeConfig.public, set by NUXT_PUBLIC_ variables | Top-level runtimeConfig keys |
| Create React App | REACT_APP_ prefix | Any other name |
| Vue CLI | VUE_APP_ prefix | Any other name |
Sources: framework documentation for Next.js, Vite, Expo and Nuxt;1546 a security firm's blog post for the last two rows.7
Nuxt works differently. Its values live in the runtime config, which environment variables such as NUXT_PUBLIC_API_BASE override, and Nuxt adds the public part to the data sent with each page.6 Vite accepts a list of prefixes in envPrefix and refuses an empty one, which would expose every variable in the environment.5
How server keys reach the browser
One path is a rename. A call from the browser fails because the key is missing there, and adding VITE_ or NEXT_PUBLIC_ to the variable makes it work.7 The value is then in a minified code file that is served to everyone. When the renamed key carries server rights, such as a Supabase service_role key, every visitor holds them; Privileged key in client-side code treats that case.
Plugins and settings can also break the convention. A 2026 study of 10 million rendered websites found 1,748 working credentials for 14 providers and told the site owners. Of the 40 owners who explained the cause, eight named a bundling problem. In one Nuxt app, the code read keys from process.env instead of its runtime config, and the @nuxtjs/dotenv module copied every variable into the build, including private ones the browser never used.8 The same study found that 62% of the keys in JavaScript files sat inside bundles made by build tools, and that a key stayed public for 12 months on average.8 A scan of the source repository misses a key that appears only after the build.
Case: a Vite build on a production site
In February 2026 the penetration testing firm Sprocket Security described a client whose production JavaScript held an AWS access key and a CircleCI token, both read from import.meta.env.VITE_* variables. The AWS key belonged to a deployment user with read and write access to S3 buckets that stored the full production code, server side included. The CircleCI token gave control of the build pipeline, its stored secrets and in the end the client's GitHub repositories. The client, who was not named, fixed it within hours of the report.7
Keeping secrets on the server
A secret gets a name without the public prefix and is read only in server code: a Next.js route handler or server component, a Nuxt server route through the top-level runtime config, a serverless function behind a Vite front end.26 The browser calls that server code, which calls the provider. Some frameworks enforce the boundary at build time:
- In Next.js, a module that starts with
import 'server-only'stops the build when a client component imports it.3 - SvelteKit treats files named
*.server.js, files in aserverdirectory and its private environment module as server-only, and stops with an error when browser code imports one.9
// lib/payments.ts (Next.js)
import 'server-only';
export async function charge(amount: number) {
const key = process.env.PAYMENT_SECRET; // no NEXT_PUBLIC_ prefix
// call the payment provider with key
}Of the study's recommendations to developers, the one with the most support in the owners' replies (20) was a CI step that scans both the source and the built bundles and fails the build when it finds a key.8 A key that was already in a bundle has been sent to every visitor; Responding to a leaked API key covers revoking and replacing it, and Secrets management covers where keys live.
Prompt for a coding agent
List every environment variable in this project whose name starts with NEXT_PUBLIC_, VITE_, EXPO_PUBLIC_, NUXT_PUBLIC_, REACT_APP_ or VUE_APP_, and every key under runtimeConfig.public. Check envPrefix in the Vite config. For each value, say whether a browser needs it (a site URL, an analytics ID, a publishable key) or whether it is a secret (a secret or service_role key, a database password, a token for a cloud, CI or AI service). Rename each secret without the public prefix, move the code that uses it into a server route or function, add import 'server-only' to that module where the project uses Next.js, and change the browser code to call the new route. List the keys that were public so they can be rotated.
In SecFlash
SecFlash reports a Supabase service_role key in a NEXT_PUBLIC_, VITE_ or EXPO_PUBLIC_ variable or other browser code as SF-SEC-724, and a secret in a public-prefixed value built into a mobile app as SF-SEC-728, both on the Security page. In the JavaScript the live site serves, it reports a secret key as SF-EXP-301, a token as SF-EXP-302, and keys that are meant to be public as SF-EXP-306, on the Exposure page.
See also
- Client-side code exposure
- Privileged key in client-side code
- Source maps in production
- Exposed credentials in source code repositories
- Secrets management
- Abuse of leaked API keys
- Securing a Next.js and Supabase application
- Securing an application made with an AI app builder
- Security checklist before launch
- Category: Secrets
References
- a b c d e "How to use environment variables in Next.js". Next.js Docs. Vercel.
- a b c d "Env Variables and Modes". Vite documentation.
- a b Vercel. "Server and Client Components". Next.js Docs.
- a b c "Environment variables in Expo". Expo Documentation. Expo.
- a b "Shared Options" (envPrefix). Vite documentation.
- a b c "Runtime Config". Nuxt Docs.
- a b c Sprocket Security (16 February 2026). "Hunting Secrets in JavaScript at Scale: How a Vite Misconfiguration Lead to Full CI/CD Compromise". Sprocket Security blog.
- a b c Demir, Nurullah; Vekaria, Yash; Smaragdakis, Georgios; Durumeric, Zakir (2026). "Keys on Doormats: Exposed API Credentials on the Web". ACM CCS '26; arXiv:2603.12498.
- back "Server-only modules". SvelteKit Docs.
External links
- How to use environment variables in Next.js, Next.js Docs
- Env Variables and Modes, Vite documentation