Lesson 6 of 10 · Own the code AI writes
Vibe coding security keep your keys and data safe
Vibe coding security in plain English: where API keys leak from an app built with AI, Supabase Row Level Security, and what to do if a key gets out.
Most of the problems in this course cost you time. This one can cost you money, your customers' trust and, if personal data gets out, a conversation with the regulator. It's also the one I'd check first in any app built with AI, because the tools make it easy to get wrong and the app works perfectly while it's wrong.
It comes down to two questions. Where are your secrets? And what stops a stranger reading or changing your data?
Two kinds of key
Almost every service your app uses gives you keys, and they come in two kinds.
- Public keys are meant to be seen. They let the browser talk to a service in a limited way, such as showing a payment form.
- Secret keys can do anything your account can do. They belong on a server and nowhere else.
Stripe's are a good example, because the names tell you which is which. Stripe's docs say "Only publishable keys are safe to expose outside your application's back end." Publishable keys start pk_. Secret keys start sk_ and are described as having "unrestricted permissions on all Stripe APIs". Anyone with your live secret key can do anything your Stripe account can.
Other services name theirs differently, but there's always a pair. Before you put a key anywhere, find out which kind it is.
Leak 1: API keys exposed in the browser
Anything sent to the browser can be read by anyone who visits. Not by hacking: by opening the developer tools, or looking at the page's source.
Frameworks have a rule for which settings go to the browser, and it's in the name:
- Next.js: its docs say "By default, environment variables are only available on the server." To send one to the browser, it must start with
NEXT_PUBLIC_, and its value is then written into the JavaScript every visitor downloads. - Vite: its docs say variables starting
VITE_"will be exposed in client-side source code", and warn that they "should not contain sensitive information such as API keys."
So a variable called NEXT_PUBLIC_STRIPE_SECRET_KEY is a contradiction, and it's exactly the kind of thing an AI produces when a secret doesn't work in the browser and it's asked to "fix it". It's fixed. It's also public.
Watch for AI keys too. An app that calls an AI model straight from the browser has to send the key with it. Anyone can copy that key and spend your money with it.
How to check: search the project for NEXT_PUBLIC_ and VITE_, and for every match ask whether that value is safe for the whole world to see. Then open your live site, open the developer tools, and search the loaded files for sk_live_, sk_test_ and the start of any other secret you know.
Leak 2: secrets in the repo
The file that holds your secrets, .env or .env.local, must never be committed to Git. If it is, everyone with access to the repo has your keys, and if the repo is public, so does everyone else.
Two things people don't expect:
- Deleting the file later doesn't remove it. Git keeps every version of every file, so the key is still in the history. Once a secret has been committed, treat it as leaked.
- A key in the code is the same as a key in
.env. AI sometimes writes a key straight into a file "for testing". Search for it.
GitHub helps a bit. Its push protection is "designed to prevent hardcoded credentials, such as secrets or tokens, from ever being pushed to your repository", and the version for your own account is on by default and stops you pushing secrets to public repositories. It's a safety net, not a plan. It covers the secrets it recognises and the repositories it's switched on for.
Leak 3: secrets pasted into chats
Pasting a key into an AI chat to "just get it working" sends it to a service you don't control, and it sits in your chat history afterwards. On my own projects passwords and keys are never pasted into AI chats. Give the AI the name of the setting (STRIPE_SECRET_KEY) and keep the value in .env.
The same goes for your customers' data. Don't paste real orders, real email addresses or a copy of your database into an AI tool to show it a problem. Make up a test record instead.
Leak 4: secrets in logs and errors
An AI debugging a problem often adds a line that prints everything:
console.log("Config:", process.env);
That prints every secret your server has, into logs that your host keeps and that other people on your team can read. The same goes for error messages that show the visitor the full details of what went wrong. Search for process.env inside console.log, and take out any debugging lines once the bug's fixed.
Supabase Row Level Security: who can read your data
Plenty of apps built with Lovable, Bolt and similar tools use Supabase, where the browser talks straight to the database. That's fine, and it's how Supabase is meant to be used, but it means something has to stop one visitor reading another visitor's rows. That something is Row Level Security, or RLS.
Supabase's docs are blunt about it: "Enable RLS on every table in an exposed schema." And: "A table in an exposed schema without RLS is readable and writable by any role with a grant on it." In plain English, a table without RLS can be open to anyone who has your app's public key (older projects call it the anon key), and your app's public key is in the browser.
With RLS switched on, nothing is readable until you write policies that say who can see what, such as "a customer can read their own orders". The docs also say: "Never use a secret key in the browser or expose it to customers." Supabase's secret key (older projects call it the service_role key) gets past RLS altogether, so it belongs on the server only.
Firebase has the same idea under a different name, security rules. Whatever your database, the question is the same: if a stranger had your public key, what could they read and change?
How to check: in the Supabase dashboard, look at every table and check RLS is on. Then read each policy and say in plain English who it lets in. A policy that lets everyone read everything (using (true)) on a table with personal data is the same as no lock at all.
What to do if an API key is exposed
Do this in the first hour, in this order:
- Make a new key and delete the old one in the service's dashboard. Most call this rotating or rolling a key. Removing it from the code isn't enough: the old key still works until you delete it.
- Put the new key where it belongs, in
.envlocally and in your host's settings, never in the code. - Look at the service's logs for anything you didn't do: payments, emails sent, data read, AI usage.
- If personal data may have been seen, take advice. In the UK, a business that holds personal data has duties under data protection law, whoever wrote the code.
If you built something with AI
What to check
- Search your project for
NEXT_PUBLIC_andVITE_. Every match is public. Is each one safe to be? - Search the project, and your live site's files in the browser, for
sk_live_,sk_test_and any other secret you know the start of. You should find nothing. - Check
.gitignorecovers.envand.env.local(a line.env*covers both). Then check the Git history:git log --all --oneline -- .env .env.local .env.productionshould print nothing. If it prints anything, rotate every key that file held. - If you use Supabase, check RLS is on for every table, and that every policy says what you think it says.
- Check every AI, email and payment service you use has a spending limit or an alert.
- If you'd like it checked before you launch, that's what my launch-readiness review is for.
If you're learning the trade
Try it
Make a small Next.js app with one secret in it: put SECRET_MESSAGE=hello in .env.local. You'll also need this Client Component, a button that shows the same message when it's pressed. Save it as app/show-secret.tsx:
"use client";
import { useState } from "react";
export default function ShowSecret() {
const [message, setMessage] = useState("");
function show() {
const secret = process.env.SECRET_MESSAGE;
setMessage(secret ?? "nothing");
}
return (
<div>
<button onClick={show}>Show the secret</button>
<p>{message}</p>
</div>
);
}
- Show the message on the home page,
app/page.tsx, with{process.env.SECRET_MESSAGE}. It's a Server Component, so it works. - Import
ShowSecretinto the same page, put it under the message and press the button. It says "nothing". Write down why. - Rename the setting
NEXT_PUBLIC_SECRET_MESSAGE, in.env.local, inpage.tsxand inshow(), and restartnpm run dev. Press the button: it says "hello". Now build the app (npm run build, thennpm start), open it, and find "hello" in the browser's developer tools: in the Sources tab, Cmd+Option+F on a Mac (Ctrl+Shift+F on Windows) searches every file the page loaded.
You've just done, on purpose, the exact thing AI tools do by accident. You'll spot it next time.