I want to report what I believe is an unsafe default in the AI Studio "Build apps" export, with a

reproduction and a real-world outcome. Posting publicly because I suspect others are hitting it

without realizing.



**## Summary**



The exported project's \`vite.config.ts\` loads **\*\*all\*\*** operating-system environment variables and

inlines `GEMINI_API_KEY` into the browser bundle. If your machine has `GEMINI_API_KEY` set as a

user-level environment variable — which is what the Gemini CLI setup tells you to do — any

production build you ship contains a live, billable key in plain text, with no warning.



**## The mechanism**



The template's build config does, in substance:



```ts

const env = loadEnv(mode, '.', '')        // empty prefix → loads EVERY OS env var

// ...

define: {

  'process.env.API_KEY': JSON.stringify(env.GEMINI_API_KEY),

}

Vite’s default prefix is VITE_, which exists precisely so that non-prefixed variables are not

exposed to client code. The empty third argument disables that protection. `define` then performs

a literal text substitution at build time, so the key ends up as a string in the output JS.

Two things make this worse than it first looks:

  1. No .env.local is required. The developer never put the key in the project. It was picked up

    from the OS environment.

2. No warning is emitted. The build succeeds silently. There is nothing in the output to suggest

a secret was just published.

## Reproduction

Environment: Windows, Vite 6.4.1, verified 2026-09-15.

  1. Set GEMINI_API_KEY as a user-level OS environment variable (as per Gemini CLI setup).

2. Create a project from an AI Studio “Build apps” export. Do not create .env.local.

3. Run the production build.

4. Grep the output bundle for the key prefix — the key is present in plain text.

5. Change loadEnv(mode, '.', '') to Vite’s default prefix and rebuild — the key is absent.

## What happened to us

  • The exported app was an internal ERP tool. It had no AI feature whatsoever. The browser-side

    Gemini client was generated by the template and never used.

  • Three bundles built 2026-03-04 to 03-06 each contained the key and were publicly served until

    approximately 2026-06-23. Access logs show 224 external IPs, mostly scanners.

  • First unauthorized use: 2026-06-12. The key was evidently resold into a third-party API relay —

    sampled request content included Simplified-Chinese role-play with jailbreak system instructions,

    Korean novel translation, quantitative-trading code review, and a QQ group chat bot. None of it

    ours.

  • Traffic ran 24/7 including weekends, 37% HTTP 400, on models our application has never called.

  • Volume grew from ~1,600 to ~95,600 successful requests per half-month.

  • Total unauthorized charges: approximately USD 4,010 over 94 days, on a project whose normal

    spend was USD 69–183/month.

  • We discovered it ourselves. We received no abnormal-usage notification.

We have since deleted the key, removed the bundles, restricted the remaining key by IP and API,

set 85 quota overrides, and added a post-build secret scan that fails the build on any API-key

pattern. That part is handled. I am posting about the template, not about our cleanup.

## Why I think this is a product issue, not just user error

Google’s own documentation states that API keys must not be embedded in client-side code. The

template’s default does exactly that, automatically, for an app that did not ask for AI

functionality, reading from an environment variable that Google’s own CLI tooling tells users to

set. A developer following Google’s instructions end to end can ship a live key without ever

making a decision to do so.

## Suggested fixes

  1. Use a non-empty prefix in loadEnv, so OS-level variables are not swept in.

2. Do not include a browser-side Gemini client in exports with no AI feature.

3. Fail the build, or at minimum warn loudly, when an API-key-shaped value would be inlined into

client output.

4. Default the template to a server-side proxy pattern rather than direct browser calls.

Items 1 and 3 seem cheap and would have prevented this entirely.

## Questions

  • Is this a known issue? Has the export template been changed since March 2026?

  • For anyone else who built from this template between roughly late 2025 and mid-2026: it is worth

    grepping your deployed bundles. We only found ours because the bill got large enough to notice.

Happy to provide the full root-cause report, reproduction output, or the nginx access logs to

anyone from the product team.

Thank you for reporting this. We are actively working on it.

Thanks for the detailed repro. Until the template changes, a quick fix is to drop the define for process.env.GEMINI_API_KEY from vite.config.ts, call Gemini from a server route (Cloud Run, a Vercel/Netlify function), and grep dist/ for AIza before every deploy. It’s also worth setting a monthly spend cap on the project (AI Studio, Spend tab) so a missed key can’t run for days.

If you’d rather not run that server piece, KeyVault Edge (I build this) gives the browser a host-locked token with a monthly cost cap and a revoke button, and adds the real key at the edge: keyvaultedge .com/?utm_source=google-ai-forum&utm_medium=reply&utm_campaign=warm-prospects-2026-11