Home › Security notes › A secret API key shipped in frontend code
Weekly security note
A secret API key shipped in frontend code
What it is
Some API keys are meant to stay on a server: OpenAI keys, Stripe secret keys (sk_live_…), and similar. When one is pasted into frontend code or a public environment variable, it gets bundled into the JavaScript every visitor downloads.
Why it's risky
Anyone can open developer tools, search your JavaScript for the key, and copy it. With it they can make API calls billed to your account, and for payment providers, act on your account's data.
Prefixes like VITE_, NEXT_PUBLIC_ and REACT_APP_ are built into the browser bundle by design, so they aren't safe places for secrets.
How to check
- Open your live site, then open developer tools and go to Sources (or Debugger).
- Search all files for key prefixes such as sk-, sk_live_ and secret.
- Check your env files for secrets behind public prefixes like VITE_ or NEXT_PUBLIC_.
Example
# Example: search a local build for likely secret prefixes grep -rnE "sk-|sk_live_" dist/ build/ .next/static/ 2>/dev/null
Or run a free scan. It checks your app's public surface for this and other common exposures.
How to fix it
- Rotate the exposed key in the provider's dashboard first. Assume it has already been copied.
- Move the API call into a server function (an edge function, API route or serverless function) that reads the key from a server-only environment variable.
Example
// Example: server-only function (Node / edge) export default async function handler(req) { const r = await fetch("https://api.openai.com/v1/responses", { method: "POST", headers: { Authorization: `Bearer ${process.env.OPENAI_API_KEY}`, "Content-Type": "application/json" }, body: JSON.stringify({ model: "YOUR_MODEL", input: "..." }) }); return new Response(r.body, { status: r.status }); } - Have the frontend call your function instead of the provider. Add auth and rate limits to the function so it can't be abused as an open proxy.
- Remove the old key from your code, env files and git history, then redeploy.
After you fix it
Check the provider's usage and logs for activity you don't recognize.
Re-run a scan to confirm no secret appears in your public JavaScript.
To catch it if this comes back, Warditor Monitor re-scans your app every 6 days for $19/month.
FAQ
Is a Stripe publishable key (pk_live_) a problem?
No. Publishable keys are meant for the browser. Only secret keys (sk_live_) and restricted keys must stay server-side.
I deleted the key from my code. Am I safe?
Not yet. Old builds, caches and git history may still hold it. Rotate the key so the old one stops working.
Does minifying or obfuscating the code hide the key?
No. The key still has to be in the bundle for the browser to use it, so anyone can find it.
Does Warditor use keys it finds?
No. The scan reports the key type with the value redacted and never uses or tests it.
Scan your app free
Look-only check of what any visitor can already see. No login, no changes to your app.
Scan your app freeNeed help fixing it? See Fix Pack, Monitor and Shield.
More security notes
- Row-level security turned off on a Supabase table
- Firebase security rules left in test mode
- A public .env file or .git folder on a live site
This page describes a generic mistake pattern. It is not about any specific app, site or person.