← Vibe coding database safety

Is your app leaking API keys to the browser?

Updated · DBShifts · commands tested on real PostgreSQL, MySQL and MongoDB servers

Short answer

Any environment variable your build tool exposes to client code — VITE_ in Vite, NEXT_PUBLIC_ in Next.js, EXPO_PUBLIC_ in Expo, REACT_APP_ in Create React App — is copied into the JavaScript every visitor downloads. Publishable keys belong there; database URLs, service-role keys and secret API keys do not. Search your built files and your git history, rotate anything you find, and move the calls that need it to a server.

Key facts

  • Vite exposes only variables prefixed VITE_ to client code, and writes their values into the built files.
  • Next.js inlines NEXT_PUBLIC_ variables into the JavaScript sent to the browser at build time; other variables stay on the server.
  • Fine in the browser: Supabase anon or publishable keys (with RLS on), Stripe publishable keys (pk_), Firebase web config.
  • Never in the browser: Supabase service_role or secret keys, Stripe secret keys (sk_), OpenAI and Anthropic API keys, database connection strings.
  • Deleting a key from the code does not un-leak it. It is in git history and in every copy of the old bundle. Rotate it.

1. Search what you actually ship

Build the app and search the output: dist for Vite, .next/static for Next.js. Anything this prints is in every visitor's browser.

ShellSearch the built app for secrets
npm run build
grep -rnoE "(sk_live_[0-9A-Za-z]{8,}|sk-[A-Za-z0-9_-]{20,}|service_role|(postgres(ql)?|mysql|mongodb(\+srv)?)://[^\"' ]+)" dist .next/static 2>/dev/null

2. Search the history too

If the repository was ever public, or shared, or pushed to a platform that scans it, a key that was committed once is exposed. This lists every commit that added or removed one.

ShellFind commits that ever added or removed one
git log --all --oneline -G '(sk_live_|service_role|(postgres(ql)?|mysql|mongodb(\+srv)?)://)'

3. Rotate what you found

Create a new key or password, put it where only the server can read it, then revoke the old one. For a database URL, change the database password — the URL is just the password with a host attached.

4. Move the call to a server

The code that needs a secret key runs in an API route, a serverless function or a Supabase edge function, and the browser calls that. When an AI assistant 'fixes' an undefined variable by adding the VITE_ or NEXT_PUBLIC_ prefix back, that is the leak returning; say no.

Common mistakes

A .env file feels private

The prefix decides, not the file. VITE_DATABASE_URL in .env is public the moment you build.

Renaming the variable without moving the code

The value becomes undefined in the browser, and the quickest 'fix' an AI suggests is adding the prefix back.

Rotating the API key but not the database password

A leaked connection string carries the database password. Change it on the database.

Hiding the key with obfuscation

Anything the browser can use, a visitor can read. Base64 or string-splitting only slows down the first person.

How DBShifts helps

  • Agents and internal tools that need database data can use an Airlock token instead of a connection string: revocable, scoped to the tables you allow, masked and logged, and useless against the database directly.

Questions people ask

Are VITE_ environment variables public?

Yes. Vite exposes VITE_-prefixed variables to client code and writes their values into the built JavaScript. Put only values you would be happy to publish there.

Is NEXT_PUBLIC_ safe for API keys?

Only for publishable keys. NEXT_PUBLIC_ variables are inlined into the browser bundle at build time. Secret keys go in variables without the prefix and are used in server code.

Is it safe to put the Supabase anon key in my frontend?

Yes, as long as every table has row level security with real policies. The service_role key is the one that must never be there.

Stop handing out connection strings

Tokens agents can use and you can revoke, scoped and logged. Free to start.

7 guides in this series. See the checklist.