Skip to content

Kompile Learn

Secret keys exposed in your JavaScript

Keep API keys that can spend money or read data out of the browser, where every visitor can see them.

What it is

Everything your site sends to the browser, including its JavaScript bundles, can be read by anyone. AI app builders often wire a service up by pasting its secret key straight into front-end code.

Why it matters

A secret key in the browser is a key handed to the whole internet. Bots scrape new sites for them and use them within minutes: running up AI bills, issuing refunds, or reading your database.

How to fix it

1. Rotate the key now

Open the provider's dashboard (Stripe, OpenAI, Supabase, AWS...) and roll or revoke the exposed key. Removing it from your code is not enough: it is already public.

2. Call the service from your server instead

// Next.js route handler (server only). The browser calls /api/chat, // never OpenAI directly, and never sees the key. export async function POST(req: Request) { const body = await req.json(); 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: "gpt-5", input: body.input }), }); return new Response(r.body, { status: r.status }); }

3. Keep only public keys in the browser

OK in the browser: Stripe publishable key (pk_...), Supabase anon key (only with Row Level Security on every table) Never in the browser: sk_..., service_role, AWS secret keys, OpenAI keys

Tell your AI coding tool explicitly: "never put secret keys in client code; read them from server-side environment variables."

Does your site have this problem?

Kompile Scan checks for this and the other problems in these guides in under a minute. Free for one site.

Check my site