Guides · Updated 2026-11-23
API_SECRET or WEBHOOK_SECRET in my JavaScript bundle: what does it mean?
Short answer: Yes, treat it as leaked. Everything in the JavaScript your site serves can be read by any visitor, so a line like API_SECRET = "..." or WEBHOOK_SECRET: "..." in that code is public, whatever the name says. It usually gets there in one of four ways: a public env prefix (VITE_, NEXT_PUBLIC_, REACT_APP_), the env option in next.config.js, a Vite define that copies env values into the client, or a value pasted straight into a component. Fix it in this order: rotate the secret, move the code that needs it to a server route or function, remove it from the client build, redeploy and check the live site again.
What you're looking at
Bundlers like Vite and Next.js replace environment variable references with their real values at build time. Vite's docs say import.meta.env values are "statically replaced at build time", and Next.js says it inlines NEXT_PUBLIC_ values "into any JavaScript sent to the browser". So if client code uses a secret, the finished JS file contains the secret itself, not a pointer to it.
That's why you may see something like this in DevTools, in a file such as assets/index-3f9a.js or _next/static/chunks/...:
const API_SECRET = "a8f3c1...";
config = { WEBHOOK_SECRET: "whsec_...", ADMIN_PASSWORD: "..." }The variable name doesn't protect anything. If it's in the file the browser downloads, anyone can copy it.
How it got there (the 4 usual ways)
- A public prefix. VITE_, NEXT_PUBLIC_ and REACT_APP_ all mean "ship this to the browser". Vite's docs say VITE_ variables "should not contain sensitive information such as API keys". More on prefixes: https://leakyvibe.com/guides/is-my-vercel-app-leaking-environment-variables
- The env option in next.config.js. This is a legacy Next.js option, and its docs say variables set there "will always be included in the JavaScript bundle", with or without NEXT_PUBLIC_. Putting SECRET_KEY: process.env.SECRET_KEY there publishes it.
- A Vite define that copies env values into the client. Vite blocks envPrefix: '' because it "will expose all your env variables", but a config that loads every variable with loadEnv and passes it to define (for example define: { 'process.env': env }) does the same thing by another route.
- A hard-coded value. The secret was pasted into a component or a shared config file while testing, and that file is imported by client code.
Server-only variables are safe by default: Next.js says non-NEXT_PUBLIC_ variables "are only available in the Node.js environment", and Vite only exposes variables with the VITE_ prefix (or your envPrefix). The leak happens when one of the four paths above pulls the value into client code.
What a stranger can do with it
It depends on what the secret unlocks. In plain words:
- WEBHOOK_SECRET (for example a Stripe whsec_ signing secret): someone can sign fake webhook events that your server accepts as real, such as "payment succeeded" for an order nobody paid.
- API_SECRET or ADMIN_TOKEN for your own backend: anyone can call the routes it protects, often admin or internal ones.
- A third-party API key or ACCESS_TOKEN (AI, email, maps, storage): they use the service as you, on your bill and your quota.
- SERVICE_ROLE or a database PASSWORD: full read and write access to your data, skipping your access rules.
- A JWT or session SECRET: they can create login tokens your app trusts, for any user.
5-minute check
- Open your live site in a private window.
- Open DevTools (F12) > Sources (Debugger in Firefox) and use Search in all files (Ctrl+Shift+F, or Cmd+Option+F on Mac).
- Search for SECRET, PASSWORD, TOKEN, PRIVATE_KEY, SERVICE_ROLE and WEBHOOK. Ignore names with no value next to them; look for a name followed by a long string.
- In your project, check .env files for secrets with VITE_, NEXT_PUBLIC_ or REACT_APP_ in the name, check next.config.js for an env block, and check vite.config for define.
- After a local build, search the output folder too:
# Vite: dist/ Next.js: .next/static/ grep -rEo "[A-Z0-9_]*(SECRET|PASSWORD|TOKEN|PRIVATE_KEY|SERVICE_ROLE)[A-Z0-9_]*" dist/ .next/static/ | sort -u
Or paste your URL in the free scan at the top of this page. It reads the HTML and JavaScript your site serves and flags secret-looking names with a value next to them (rule: secret-looking env variable compiled into JS), plus known key formats like service_role, sk_live_ and private keys. It shows the name and a redacted value, never the full secret.
The fix
- Rotate the secret first, in the dashboard of whatever service it belongs to (or generate a new one if it's your own). Removing it from the code doesn't help on its own: anyone may already have a copy, and old deployments can keep serving the old file. Step by step: https://leakyvibe.com/guides/leaked-api-key-what-to-do
- Move the code that needs the secret into a server route or function (a Next.js route handler or server action, a Supabase Edge Function, a Netlify or Vercel function). The browser calls your route; only the route uses the secret.
- Store the new value in a server-only env var with no VITE_, NEXT_PUBLIC_ or REACT_APP_ prefix.
- Remove it from next.config.js env and from any Vite define. If the client really needs a non-secret value, pass only that one value.
- Delete hard-coded copies from components and shared config files.
- Rebuild, redeploy, and repeat the 5-minute check on the live site.
// app/api/send/route.ts (Next.js) - the secret never reaches the browser
export async function POST(req: Request) {
// check the user is logged in here first
const res = await fetch("https://api.example.com/send", {
method: "POST",
headers: { Authorization: `Bearer ${process.env.API_SECRET}` }, // no NEXT_PUBLIC_
body: await req.text(),
});
return new Response(res.body, { status: res.status });
}A prompt you can give your AI tool
A secret is compiled into my frontend JavaScript (a variable named like API_SECRET / WEBHOOK_SECRET). Find every place client code reads a secret: env vars with VITE_, NEXT_PUBLIC_ or REACT_APP_ prefixes, the env block in next.config.js, define in vite.config, and hard-coded values in components or shared config. Move every call that needs a secret into server routes or functions that read it from a server-only env var, and add a login check to those routes. Remove the secret from the client build and list the files you changed and the secrets I must rotate.
Sources
Official sources (checked 2026-11-23): Vite, "Env Variables and Modes" (https://vite.dev/guide/env-and-mode): import.meta.env constants are statically replaced at build time; VITE_ variables are exposed in client-side code and should not contain API keys; use a backend or serverless/edge functions for secrets. Vite, "Shared Options" (https://vite.dev/config/shared-options): envPrefix should not be '' because it would expose all env variables; define entries are statically replaced during build. Next.js, "Environment Variables" (https://nextjs.org/docs/app/guides/environment-variables): non-NEXT_PUBLIC_ variables are only available in the Node.js environment; NEXT_PUBLIC_ values are inlined into JavaScript sent to the browser at build time. Next.js, "env" (https://nextjs.org/docs/app/api-reference/config/next-config-js/env): a legacy option; variables set there are always included in the JavaScript bundle.