Sensitive Secret Prefixed with NEXT_PUBLIC_ or VITE_
Detects private API keys, database URLs, or service_role tokens accidentally prefixed with NEXT_PUBLIC_, VITE_, REACT_APP_, or PUBLIC_, which causes bundlers to inline them into public browser JavaScript.
- Rule Slug
- nextjs-public-env-secret-leak
- Affected Files
- *
- Detection Method
- Deterministic Regex
Vulnerable Pattern vs. Hardened Fix
# .env.local
# CRITICAL: Bundler inlines these secrets directly into public browser JS!
NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoic2VydmljZV9yb2xlIn0.k9XmP2vL8qR5nW3zB7cF1jH4
VITE_OPENAI_API_KEY=sk-proj-9aB3cD5eF7gH9iJ1kL3mN5oP7qR9sT1uV3wX5yZ7# .env.local (Server-only secrets have NO public prefix)
SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
OPENAI_API_KEY=sk-proj-...
# Only genuinely public values carry NEXT_PUBLIC_ / VITE_:
NEXT_PUBLIC_SUPABASE_URL=https://xyzcompany.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=eyJhbGciOi...Why This Fails in Production
Modern meta-frameworks such as Next.js, Vite, Astro, Nuxt, and Create React App intentionally prevent accidental secret leaks by hiding server environment variables from browser bundles unless they carry an explicit public prefix (NEXT_PUBLIC_, VITE_, PUBLIC_, REACT_APP_, or NUXT_PUBLIC_). Unfortunately, when developers encounter undefined while reading process.env.MY_API_KEY inside a React or Vue component, the fastest "fix" is often adding NEXT_PUBLIC_ or VITE_ to the variable name, silently compiling the private secret straight into the public client-side JavaScript bundle.
Unlike an unignored .env file (which leaks only if pushed to Git), a NEXT_PUBLIC_ or VITE_ secret leaks straight to every visitor of your live website even if your Git repository is 100% private and your .env file is properly ignored! At build time, Webpack, Turbopack, or Vite replaces every reference to process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY or import.meta.env.VITE_SUPABASE_SERVICE_ROLE_KEY with the literal plaintext string inside /_next/static/chunks/*.js or /assets/index-*.js.
One of the most devastating real-world examples is prefixing Supabase’s SUPABASE_SERVICE_ROLE_KEY, OpenAI’s OPENAI_API_KEY, Stripe’s STRIPE_SECRET_KEY, or an admin JWT secret with NEXT_PUBLIC_ or VITE_. Anyone can open Chrome DevTools -> Sources, search for the key in 5 seconds, and completely bypass Row-Level Security (RLS) or drain paid API quotas.
Any credential that grants write access, admin bypass, or billable private API access must NEVER carry a public frontend prefix. Move the logic consuming that secret into a Next.js Route Handler / Server Action, Astro server endpoint, Cloudflare Worker, or backend API route.
Remediation Steps
- 1Remove the NEXT_PUBLIC_, VITE_, REACT_APP_, or PUBLIC_ prefix from the sensitive variable name immediately across your codebase and hosting dashboard (Vercel, Netlify, Cloudflare Pages).
- 2Move all API calls using that secret into a server-only endpoint (Next.js app/api/*/route.ts, Server Actions, or backend controller) so the credential never reaches browser JavaScript.
- 3If the build was already deployed publicly, rotate the leaked secret immediately. The old key was published inside your compiled JS chunks and may be cached on CDNs or web archives.
- 4Use public prefixes exclusively for intentionally public identifiers like analytics measurement IDs, Stripe publishable keys (pk_live_), or Supabase anon keys protected by strict Row-Level Security.
Learn safe server-vs-client environment variable patterns on BackendRecipes.
Frequently Asked Questions
- If my GitHub repo is private, is NEXT_PUBLIC_API_SECRET still dangerous?
- Yes! Even with a private repository and properly ignored .env file, Next.js and Vite embed NEXT_PUBLIC_* and VITE_* values verbatim into the static JavaScript files downloaded by every browser visiting your website.
- Is NEXT_PUBLIC_SUPABASE_ANON_KEY safe to expose?
- The Supabase anon key is designed to be public when PostgreSQL Row-Level Security (RLS) policies are enabled on every table. The SUPABASE_SERVICE_ROLE_KEY, however, bypasses RLS completely and must NEVER be prefixed with NEXT_PUBLIC_ or VITE_.
- How can I verify no secrets leaked into my built frontend bundle?
- Scan your project with PushCheck before deploying, and avoid using any public env prefix for keys, tokens, secrets, passwords, or service roles.