caelo

Caelo › Guides › VITE_ variables

Are VITE_ environment variables safe for API keys?

By Arthi Arumugam, Irth Labs · Published · Updated

Short answerNo. Vite exposes every VITE_-prefixed variable to your client code through import.meta.env and writes the value into the built JavaScript. Vite's own docs say VITE_ variables should not contain sensitive information such as API keys. A Vite app runs entirely in the browser, so calls that need a secret key belong in a backend or an edge function.

What does Vite expose?

Vite's docs say: "Variables prefixed with VITE_ will be exposed in client-side source code after Vite bundling", and "VITE_* variables should _not_ contain sensitive information such as API keys." Unprefixed variables in your .env files are not exposed to client code.

The file name does not help. .env.local is ignored by git if you follow Vite's advice to add *.local to .gitignore, but a VITE_ value inside it is still written into the bundle you deploy. Git-ignored and secret are different things.

The vulnerable pattern and the fix

Vulnerable: a server-only key read in browser code

// src/lib/email.ts
const res = await fetch("https://api.resend.com/emails", {
  method: "POST",
  headers: { Authorization: "Bearer " + import.meta.env.VITE_RESEND_API_KEY },
  body: JSON.stringify(email),
});

Fixed: a Supabase Edge Function holds the key

// supabase/functions/send-email/index.ts
import { withSupabase } from "npm:@supabase/server@1";

export default {
  fetch: withSupabase({ auth: "user" }, async (req, ctx) => {
    const { subject, html } = await req.json();
    const res = await fetch("https://api.resend.com/emails", {
      method: "POST",
      headers: {
        Authorization: "Bearer " + Deno.env.get("RESEND_API_KEY"),
        "Content-Type": "application/json",
      },
      body: JSON.stringify({ from: "app@example.com", to: ctx.userClaims?.email, subject, html }),
    });
    return Response.json({ ok: res.ok });
  }),
};

The browser calls the function with the signed-in user's token

const { data, error } = await supabase.functions.invoke("send-email", {
  body: { subject: "Your report", html: "<p>Ready.</p>" },
});

Set the key with supabase secrets set RESEND_API_KEY=.... It never appears in your repository or your bundle. Any backend works the same way: a Vercel function, a Cloudflare Worker or an Express server.

The loadEnv trap: a key with no VITE_ prefix can still ship

Vite lets a config file load every variable with loadEnv(mode, process.cwd(), ""), where the empty third argument means "regardless of the VITE_ prefix". That is fine for configuring the dev server. It becomes a leak when the config then copies a value into client code with define:

Vulnerable: vite.config.ts inlines an unprefixed secret

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), "");
  return {
    define: {
      "process.env.GEMINI_API_KEY": JSON.stringify(env.GEMINI_API_KEY), // now in the bundle
    },
  };
});

Every define entry is replaced in your source at build time, so the key ends up in the JavaScript even though its name has no prefix. Remove the define line and move the call to a server. Separately, Vite refuses envPrefix: "" in the config: its docs say an empty prefix would expose all your variables and that Vite throws an error when it detects one.

Fix it, step by step

  1. List the public variables: grep -rhoE "VITE_[A-Z0-9_]+" src | sort -u, and read vite.config for define and loadEnv.
  2. Keep only publishable values behind VITE_: a Supabase URL and anon or publishable key, a Stripe publishable key, Firebase web config, analytics IDs.
  3. Move every other call into an edge function or backend that reads the key from its own environment.
  4. Rotate every key that was ever behind VITE_ or in a define. Old deployments and anyone who loaded them still have it.
  5. Build again and search the output: npm run build && grep -rl "sk-\|re_\|AIza" dist should return nothing that is a secret.

Check it automatically

Caelo flags a VITE_ name that looks like a secret or belongs to a server-only provider (browser.public-secret-name), and any provider key it recognises written directly in a file, such as an OpenAI or Google key. It does not evaluate your vite.config, so it will not catch the define pattern above when the value comes from your environment at build time. Read that file yourself.

Part of the real report from the demo app in the Caelo README

- Server-only key exposed to the browser [needs review]
    src/lib/ai.ts:1  (browser.public-secret-name)
    value: VITE_OPENAI_API_KEY
    why: VITE_OPENAI_API_KEY has a browser-public prefix, so its value is bundled into the JavaScript every visitor downloads. This provider's keys are server-only; there is no browser-safe version.

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.

Book the App Health Check See repair services

Questions

Is VITE_SUPABASE_ANON_KEY safe in my bundle?

Yes. The anon key and the publishable key are meant to be public, and row level security decides what they can reach. Check your policies with our RLS guide.

Does putting the key in .env.local keep it private?

It keeps the file out of git. If the variable starts with VITE_, the value is still written into the built JavaScript, so the key is public as soon as you deploy.

Can I obfuscate or encode the key in the frontend?

No. The browser needs the real value to send it, so anyone can read it from the network tab. The only fix is to keep the key on a server.

Sources