Exposed keys: how to tell if your app is leaking secrets
Which keys might be out in the open on your site, how to find them in two minutes, and what to do if one turns up.
· 2 min read
A secret key is like the key to your account with a service: whoever has it can act on your behalf. The problem is that when you build an app with AI, it's very easy for one of those keys to end up inside the code the browser downloads. And anything the browser downloads, anyone can read.
Not all keys are secret
Before you panic, it helps to tell them apart. Some keys are meant to be visible, and some should never leave the server.
These can be in the browser:
- The Stripe publishable key (starts with
pk_). - The Supabase anon key. It's public on purpose; what protects you is the database's access rules.
- Google Maps or Firebase keys, as long as they're restricted to your domain.
These should never be in the browser:
- The Stripe secret key (starts with
sk_). - The Supabase service_role key, which bypasses all access rules.
- Keys for OpenAI, Anthropic, and other AI services (they usually start with
sk-). - AWS credentials, GitHub tokens, and any database password.
The typical mistake: the prefix that publishes everything
In projects built with Vite, every environment variable that starts with VITE_ gets included in the browser code. In Next.js, the same thing happens with the ones that start with NEXT_PUBLIC_.
It's a useful mechanism, but if the AI decides to name your OpenAI key VITE_OPENAI_KEY, that key gets published along with the site. It works perfectly and nobody warns you.
How to find them in two minutes
- Open your site in the browser.
- Open the developer tools (F12) and go to the Sources tab.
- Use the search across all files (Ctrl+Shift+F, or Cmd+Option+F on Mac).
- Search for these, one at a time:
sk_live,sk-,service_role,AKIA,ghp_,secret,password.
If you'd rather not do it by hand, ask us for a free check and we'll look for you.
What to do if you find one
The order matters:
- Generate a new key in the provider's dashboard and revoke the old one. Removing it from the code isn't enough: the old key was already public and may have been copied.
- Move the operation to the server. Whatever needs that key has to run on a server or in a server-side function, not in the browser.
- Check the usage on that account over the last few days, in case someone already used it.
- Check the repository history. If the key was in a commit, it's still there even if you deleted it later.
A detail that gets overlooked
Deleting the key from the code and publishing again makes it feel like the problem is solved. It isn't until the old key is revoked. That step is one only you can do, from your account with the provider.