API keys and secrets in the frontend: how AI-generated code leaks them and how to find them
Contents
A typical prompt reads "add an AI chat to my landing page" or "connect Stripe". The fastest working answer calls the external API straight from the browser, with the key in the code. The page works, the demo impresses, and from the first deployment the key is public. Exposed keys are one of the most common problems in vibe-coded projects, and one of the easiest to avoid.
Why does AI-generated code put secrets in the frontend?
Language models produce what makes the requested feature run in the context they see. If that context is a React component, the API call ends up in the component. Three mechanisms make the leak permanent:
- Build-time variables. Vite exposes every variable starting with VITE_ in the client bundle and states that such variables should not contain sensitive information such as API keys (Vite docs). Next.js inlines every NEXT_PUBLIC_ variable into the JavaScript sent to the browser at build time (Next.js docs). When an AI renames OPENAI_API_KEY to VITE_OPENAI_API_KEY "so it works", the key is published.
- Hard-coded values. Keys pasted into the chat during debugging end up as string literals in the source.
- Repositories. A committed .env file or a config file stays in the Git history even after it has been deleted from the latest version.
Which keys may be public and which must stay secret?
Not every key in the browser is a mistake. The difference matters, because a scanner that reports every "key" creates noise.
| Key | Public by design? | Note |
|---|---|---|
| Supabase publishable key (formerly anon) | yes | only reaches what Row Level Security allows (Supabase API keys) |
| Supabase secret key (formerly service_role) | no | bypasses Row Level Security, never in a browser or in source control |
| Firebase API key | yes | may be in code; protection comes from security rules (Firebase API keys) |
| Gemini Developer API key | no | Firebase explicitly says it must not be in code or config files |
| Stripe publishable key (pk_...) | yes | intended for the browser |
| Stripe secret key (sk_...) | no | full access to the account |
| LLM provider keys, mail and SMS API keys, cloud access keys | no | anyone can spend your money or send in your name |
The rule of thumb: a key is only safe in the browser if the provider explicitly designed it for that and if a second layer (rules, domain restrictions, quotas) limits what it can do.
How do you find exposed keys in your own app?
Search the delivered bundle
Open your live site, then the browser's developer tools, and search all loaded scripts (in Chrome: Sources, then "Search in all files") for typical prefixes such as sk_, sk-, secret, service_role, api_key or Bearer. Even better, search the build output before deploying:
npm run build
grep -rnE "sk_(live|test)_|sk-[A-Za-z0-9]|service_role|BEGIN (RSA|EC|OPENSSH) PRIVATE KEY" dist/ .next/static/ 2>/dev/null
Adjust the folder to your framework. A hit is not always a leak, but every hit deserves a look.
Scan the repository including its history
Use a secret scanner such as Gitleaks, which can scan both the Git history and a plain folder:
gitleaks git -v .
gitleaks dir -v dist/
On GitHub, secret scanning runs automatically and free of charge for public repositories, and for supported partner secrets GitHub notifies the provider so the key can be revoked (GitHub docs). Do not rely on that alone: private repositories, other platforms and keys that no partner recognises are not covered.
Check source maps and exposed files
Source maps make the original source readable, including comments and file names. Vite and Next.js do not ship browser source maps in production by default, so check that nobody switched them on. Also make sure your server does not deliver /.env or the /.git/ folder; open both URLs on your live domain and expect a 404.
Ask the AI, but verify
Asking your coding assistant "list every place where a secret or API key is used in the client" is a good start. Verify the answer with the search above, because the same tool created the problem.
What should you do when you find a leaked key?
- Rotate immediately. Create a new key with the provider and revoke the old one. Removing it from the code does not help, because copies exist in browser caches, archives and scraper databases. GitHub also points out that cleaning the history is time-consuming and often unnecessary once the credential is revoked.
- Check usage. Look at the provider's logs and billing for calls you did not make.
- Move the call to the server. A serverless function, an edge function or your backend calls the external API with the key from a server-side environment variable; the browser only calls your own endpoint.
- Protect the new endpoint. Require a login where possible, check permissions and add a rate limit, otherwise your proxy becomes the open door instead of the key.
- Restrict what remains public. Domain or app restrictions, quotas and minimal scopes for every key that stays in the browser.
- Prevent recurrence. Add .env to .gitignore, enable push protection or a pre-commit hook with a secret scanner, and add a short rule to your AI assistant's project instructions: "never put secrets in client code or VITE_/NEXT_PUBLIC_ variables".
Frequently asked questions
Is it enough to obfuscate or minify the key?
No. Minification only shortens names; the key stays a readable string, and anyone can watch the request in the network tab. A secret in the browser cannot be hidden.
My Supabase key is visible in the browser. Is that a leak?
Not if it is the publishable (anon) key and Row Level Security protects every table. If it is the secret or service_role key, it is a serious leak: rotate it and read our article on Supabase and Firebase database rules.
Does athorion find keys in my JavaScript?
No. The free athorion check is a passive, non-intrusive external check: it measures open ports and services, TLS certificates, security headers, cookie flags, disclosed software versions with known CVEs, subdomains and mail settings. It does not read or analyse your JavaScript bundle. For secrets, use the bundle search and a secret scanner as described above.
More on securing vibe-coded apps
Exposed keys are point one of ten in our guide to vibe coding security, and part of the pre-launch security checklist. What your domain shows from the outside, from open ports to missing headers, is measured by the free check; the Nightly Audit + Seal repeats it every night.
Sources
Researched and structured with AI assistance, reviewed for accuracy by Robin Schwarzfeld.