Are NEXT_PUBLIC_ environment variables secret? What leaks, and how to fix it
NEXT_PUBLIC_ variable into the JavaScript it sends to the browser at build time, so anyone can read the value. Use the prefix only for public values, such as a Supabase or Stripe publishable key. Keep secrets in unprefixed variables, read them only in server code and rotate any that were ever prefixed.What does NEXT_PUBLIC_ actually do?
The Next.js docs say Next.js can "inline" a value, at build time, into the js bundle that is delivered to the client, and that all NEXT_PUBLIC_ variables are frozen with the value evaluated at build time. The value is written into a JavaScript file that any visitor can open.
Variables without the prefix "are only available in the Node.js environment, meaning they aren't accessible to the browser." In a Client Component, Next.js replaces an unprefixed variable with an empty string. That is why an AI assistant, asked to fix an empty process.env.OPENAI_API_KEY in a Client Component, often adds NEXT_PUBLIC_ to the name. That makes the error go away and puts the key in the bundle.
The vulnerable pattern and the fix
Vulnerable: a secret read in a Client Component
// app/upgrade/UpgradeButton.tsx
"use client";
import Stripe from "stripe";
const stripe = new Stripe(process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY!); // in the bundle
Fixed: the secret lives in a server-only module
// lib/stripe.ts
import "server-only"; // importing this file from a Client Component fails the build
import Stripe from "stripe";
export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
The browser calls a route handler instead
// app/api/checkout/route.ts runs on the server
import { stripe } from "@/lib/stripe";
export async function POST() {
const session = await stripe.checkout.sessions.create({
mode: "subscription",
line_items: [{ price: process.env.STRIPE_PRICE_ID!, quantity: 1 }],
success_url: "https://example.com/thanks",
});
return Response.json({ url: session.url });
}
The Next.js docs say that once a module imports server-only, importing it into a Client Component causes a build-time error. That turns a silent leak into a failed build.
Which values are fine behind the prefix?
| Value | Behind NEXT_PUBLIC_? | Why |
|---|---|---|
Supabase URL, anon key, sb_publishable_ key | Yes | Designed for the browser. Row level security protects the data. |
Supabase service_role or sb_secret_ key | No | Bypasses row level security. |
Stripe publishable key (pk_live_...) | Yes | Stripe says to embed publishable keys in client-side code. |
Stripe secret or restricted key (sk_live_, rk_live_) | No | Can create charges, refunds and read customer data. |
| OpenAI, Anthropic, Resend, Twilio API keys | No | These providers have no browser-safe key. Anyone with it spends your money. |
Firebase web config (apiKey, authDomain) | Yes | Public by design. Security rules protect the data. |
| Analytics IDs (PostHog, Sentry DSN, GA measurement ID) | Usually yes | Meant to identify your project, not grant access. |
Fix it, step by step
- List every public variable your code uses:
grep -rhoE "NEXT_PUBLIC_[A-Z0-9_]+" --exclude-dir=node_modules --exclude-dir=.next . | sort -u - For each one, look up the value and decide from the table above whether it is publishable or secret.
- Rename each secret one without the prefix and move the code that uses it into a route handler, server action or a module that imports
server-only. - Rotate every secret that was ever built with the prefix. It is inside every bundle you have deployed since, so renaming it is not enough.
- Update the variables on your host and redeploy. A
NEXT_PUBLIC_value is frozen at build time, so changing it in the dashboard does nothing until the next build. - On Vercel, store the secrets as the Secret type. Vercel says Secret values are write-only after saving, are allowed in Development, and values of 32 characters or more are redacted in build logs.
Check it automatically
Caelo flags a browser-prefixed variable name in your code when the name looks like a secret (SECRET, SERVICE_ROLE, PRIVATE, PASSWORD) or belongs to a provider that only has server keys (OpenAI, Anthropic, Resend, Stripe secret and others). The rule is browser.public-secret-name. It skips names that are public by design, such as ANON, PUBLISHABLE and FIREBASE, and it ignores names that only appear in documentation. A generic name such as NEXT_PUBLIC_API_KEY is reported as medium, because Caelo cannot tell from the name whether the key is publishable.
Caelo output for a file that uses NEXT_PUBLIC_STRIPE_SECRET_KEY
- Secret-named value exposed to the browser [needs review]
src/ai.ts:2 (browser.public-secret-name)
value: NEXT_PUBLIC_STRIPE_SECRET_KEY
why: NEXT_PUBLIC_STRIPE_SECRET_KEY has a browser-public prefix and a secret-sounding name. Anything behind that prefix is bundled into the JavaScript every visitor downloads.
- Install Caelo: npx caelo . or the MCP server
- How Caelo compares with gitleaks, TruffleHog, Semgrep and others
1. Run the free local check
npx caelo .
Caelo is a free, open source (MIT) command line tool and MCP server from Irth Labs that checks the files of an AI-built app for exposed API keys, Supabase and Firebase misconfigurations and .env files with real values. It runs on your machine, uploads nothing and uses no AI model. Each finding comes with a plain-English fix.
2. Act on a finding
Follow the fix in your report and run Caelo again. If Git could commit an untracked file containing secrets, preview a US$19 quick fix for that file.
For findings that need an engineer, Irth Labs offers a US$490 App Health Check. You receive a written report within 48 hours and a fixed repair quote. The fee counts toward the repair if you proceed.
Questions
Is NEXT_PUBLIC_SUPABASE_ANON_KEY safe?
Yes, as long as row level security is on for every table and the policies are narrow. The anon key is meant to be public. Our anon key guide covers how to check it.
Does marking a variable as a Vercel Secret protect a NEXT_PUBLIC_ value?
No. The Secret type controls who can read the value in the Vercel dashboard. If the name starts with NEXT_PUBLIC_, Next.js still writes the value into the browser bundle at build time.
Why is my unprefixed variable undefined in the browser?
Because Next.js only sends NEXT_PUBLIC_ variables to the browser. That is working as intended for secrets. Read the variable in a route handler or server action and send the browser only the result.
I removed the prefix. Am I safe now?
Not until you rotate the key. Every deployment built while the prefix was there contains the old value, and anyone who loaded your site could have copied it.
Sources
- Next.js docs: Environment variables (bundling for the browser).
- Next.js docs: Server and Client Components (server-only and environment poisoning).
- Vercel docs: Sensitive and Secret environment variables.
- Stripe docs: API keys best practices.
- Supabase docs: Understanding API keys.
- Caelo source: every rule in lib/rules.js.