Netlify build fails with "Secrets scanning found secrets in build": tell a public key from a real leak

By AI App Medic · Updated · Netlify · Secrets · Vite · Bolt

Your site built fine yesterday. Today the Netlify deploy fails and the log ends like this:

Secrets scanning found secrets in build.

Scanning complete. 14235 file(s) scanned. Secrets scanning found 1 instance(s) of secrets in build output or repo code.
Secret env var "VITE_SUPABASE_ANON_KEY"'s value detected:
  found value at line 1 in dist/assets/index-Cjlu-vNY.js

In the Netlify deploy summary the failing step is listed as Secrets scanning. This stops many Bolt, Lovable and Cursor projects the first time someone marks their environment variables as secrets, or turns on stricter security for a team. The fix is quick once you answer one question for each flagged name: is this value supposed to be public?

What the scan does

Netlify's Secrets Controller lets you flag an environment variable with "Contains secret values". From then on, Netlify scans your repository files and the build output before publishing, and fails the build if the value of such a variable appears anywhere. It only looks at values longer than four characters that are not booleans, and it checks the plain value, a base64 version and a URL encoded version.

On Personal, Pro and Enterprise plans there is also smart detection, which looks for strings that match common secret formats even when you did not flag anything. When it finds one, the log says there were instances of "likely secrets".

Here is why a Vite app trips it. Vite copies every environment variable that starts with VITE_ into the JavaScript it ships to browsers. That is the whole point of the prefix. If you marked VITE_SUPABASE_ANON_KEY as secret, Netlify finds it in dist/assets and refuses to deploy. The scan is working correctly. It is also flagging a value that was always going to be in the browser.

Step 1: list what was flagged

Open Deploys, click the failed deploy, and read the Secrets scanning section of the log. For each line that says Secret env var "NAME"'s value detected, write down the name and the file path under it. Values are never printed, only names and locations.

Step 2: sort each name into public or secret

Be honest here. This decision is the whole fix.

If a name in the second list appears in your build output, the scan did its job. Jump to the real leak section below.

Fix A: the flagged values are public

Tell the scanner to ignore those specific names. In Project configuration, Environment variables, add a variable named SECRETS_SCAN_OMIT_KEYS with a comma separated list of names:

SECRETS_SCAN_OMIT_KEYS=VITE_SUPABASE_URL,VITE_SUPABASE_ANON_KEY,VITE_SUPABASE_PUBLISHABLE_KEY

Or set it from a file in your repository, which keeps it visible to your team:

# netlify.toml
[build.environment]
  SECRETS_SCAN_OMIT_KEYS = "VITE_SUPABASE_URL,VITE_SUPABASE_ANON_KEY"

Trigger a new deploy. You can also stop marking those variables as secret. Netlify's documentation says the flag cannot be removed once it is set, so in practice that means deleting the variable and creating it again without "Contains secret values". Either way, a public key does not need secret handling.

Two shortcuts to avoid. Setting SECRETS_SCAN_ENABLED=false switches off all scanning, including the check that would catch a real service role key next week. And SECRETS_SCAN_OMIT_PATHS pointing at dist would skip exactly the folder where a leaked key ends up. Omit names, not folders.

Fix B: the flagged value is a real secret

If a service role key, a secret key or an OpenAI key is in the bundle, it is already public if any earlier deploy included it. Fix it in this order:

  1. Rotate the key first. Create a new one at the provider and revoke the old one. In Supabase this is under Project Settings, API Keys. For Stripe it is in the dashboard under Developers, API keys, with a roll key action. Rotating makes the exposed value useless.
  2. Remove the client prefix. Rename VITE_OPENAI_API_KEY to OPENAI_API_KEY. Without VITE_, Vite will not put it in the bundle.
  3. Move the call to the server. The browser calls your own function, and the function calls the provider:
// netlify/functions/summarize.mjs
export default async (req) => {
  const { text } = await req.json();
  const r = await fetch("https://api.openai.com/v1/chat/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
    },
    body: JSON.stringify({
      model: "gpt-4o-mini",
      messages: [{ role: "user", content: text }],
    }),
  });
  return new Response(await r.text(), {
    status: r.status,
    headers: { "Content-Type": "application/json" },
  });
};

The front end then calls /.netlify/functions/summarize and never sees the key. Add your own checks in the function, such as requiring a signed in user and limiting request size, or anyone can use your function to spend your money. In a Supabase app, an Edge Function does the same job, and the VITE_ keys are public guide explains the underlying rule.

  1. Mark the new server variable as secret. Now the scan protects it, since it should never be found in a build again.
  2. Check usage. Look at the provider's usage and billing for the exposure window. A service role key in a public bundle means anyone could read and write your database during that time, and the Supabase service role key exposed guide lists what to check.

Fix C: the secret is typed into a source file

Sometimes the scan finds the value in a file in your repository, not in dist. AI tools do this: they paste a key into config.ts or supabase.ts to make something work. Move the value into an environment variable, delete it from the file, rotate the key, and remember that git history keeps old versions. A key that was committed to a public repository should be treated as leaked no matter what you do next.

When smart detection flags something harmless

If the log says "likely secrets" and the string is not a secret, for example a long test string or a public ID in a format that looks like a token, add the value to a safelist. Create a variable named SECRETS_SCAN_SMART_DETECTION_OMIT_VALUES and put the strings, not the variable names, separated by commas. Do not disable smart detection for the whole site just to get one deploy through.

Prevention

Keep a short list in your repository of which variables are public and which are server only. Give public ones the VITE_ or NEXT_PUBLIC_ prefix and nothing else. Mark only server variables as secret in Netlify, and list the public ones in SECRETS_SCAN_OMIT_KEYS if you do mark them. Before every launch, search the built dist folder for the names of your server keys, and run the free scan on the live site, which checks for exposed keys the same way a stranger would. If the Bolt and Netlify variables in your project are still not arriving at build time, the Bolt Netlify environment variables undefined guide covers that side.

FAQ

Why did Netlify find my Supabase key?

Netlify scans for the values of environment variables you marked as secret. Vite copies every VITE_ variable into the browser bundle, so a marked variable like VITE_SUPABASE_ANON_KEY shows up in the built files and fails the scan, even though that key is public by design.

Is it safe to set SECRETS_SCAN_ENABLED to false?

It works, but it switches off every scan, including the one that would catch a real service role key. Use SECRETS_SCAN_OMIT_KEYS for specific public variables instead.

Can I just remove the secret flag from the variable?

Netlify's documentation says that once a variable is flagged as a secret, the flag cannot be removed. Use SECRETS_SCAN_OMIT_KEYS for public values, or delete the variable and create it again without the flag.

What is the difference between this and smart detection?

The standard scan looks for the values of variables you marked as secret. Smart detection, available on Personal, Pro and Enterprise plans, also looks for strings that match known secret formats. When it flags a harmless string, you can safelist the value with SECRETS_SCAN_SMART_DETECTION_OMIT_VALUES.

What if the flagged key really was a secret?

Treat it as leaked. Remove it from client code, rotate it at the provider, and check usage and billing for the period it was exposed. Fixing the build does not undo the exposure if an older deploy was already public.

Related guides