Abbi Labs

Guides

dangerouslyAllowBrowser: true in OpenAI or Anthropic code? What it exposes and the safe fix

By Abbi Labs · Last updated 6 December 2026

Scan your app free — see if an OpenAI or Anthropic key is sitting in your live JavaScript right now.

Abbi Labs checks your live site from the outside: secret keys in the JavaScript (OpenAI, Anthropic, Stripe, Supabase service_role and more, shown masked), Supabase tables readable with your public key, exposed .env and .git files, security headers, CORS, cookie flags and TLS certificate expiry. Read-only. No sign-up for the quick check.

No app handy?

Example result · our deliberately broken demo app

6 issues found: 3 critical, 1 high, 1 medium, 1 low

  • Critical: Stripe live secret key in public JavaScript
  • Critical: /.env file readable by anyone
  • High: Git repository exposed (/.git/HEAD)

The demo's problems are planted on purpose. Paste your own URL above to see yours.

No sign-up. Read-only public-resource and protocol metadata checks. No model prompts or tool execution. Scan only sites you own or are allowed to test.

Run on every deploy or use from Cursor / Claude Code via MCP →

Weekly AI-app security checklist

One short email a week: a practical check for apps built with Supabase, Next.js and AI tools (RLS, leaked keys, headers and more). Unsubscribe anytime.

You add a chat box to your app, call OpenAI or Claude straight from a React component, and the page throws this:

It looks like you're running in a browser-like environment.

This is disabled by default, as it risks exposing your secret API credentials to attackers.
If you understand the risks and have appropriate mitigations in place,
you can set the `dangerouslyAllowBrowser` option to `true`, e.g.,

new OpenAI({ apiKey, dangerouslyAllowBrowser: true });

That's the exact text in the openai and @anthropic-ai/sdk source (the Anthropic one says new Anthropic(...)). The fix is in the error, so you, or your AI coding tool, adds the flag and the chat works.

What changed: nothing about the key. It's still in your JavaScript, and every visitor's browser now sends it to the provider. OpenAI's README puts it this way: enabling the option "exposes your secret API credentials in the client-side code", and "any user with access to the browser can potentially inspect, extract, and misuse these credentials." Someone who copies it can run requests on your account until you notice or hit your spend limit.

1. Check your code (1 minute)

grep -rn "dangerouslyAllowBrowser" --include=*.{js,jsx,ts,tsx,vue,svelte} . | grep -v node_modules

Any hit in a file that runs in the browser (a React/Vue/Svelte component, anything with "use client", a Vite app's src/) is the problem. Also search for NEXT_PUBLIC_OPENAI, VITE_OPENAI, NEXT_PUBLIC_ANTHROPIC and VITE_ANTHROPIC: those prefixes copy the value into the browser bundle at build time (more on that).

2. Check your live site (2 minutes)

On your own app only:

  1. Open the site, DevTools > Sources, then search all files (Ctrl/Cmd + Shift + F) for sk-proj-, sk-svcacct-, sk-ant- and dangerouslyAllowBrowser.
  2. DevTools > Network, use the AI feature, and look for requests going straight to api.openai.com or api.anthropic.com. Click one and look at the request headers: an Authorization: Bearer sk-... or x-api-key: sk-ant-... header means the key is public.
  3. For Anthropic, the SDK also adds anthropic-dangerous-direct-browser-access: true to every request when the flag is on, which makes it easy to spot.

If you find a key, assume it has been copied. Bots scan public JavaScript for these prefixes.

3. Fix it in this order

  1. Check usage. Look at usage in the OpenAI or Anthropic console for traffic you don't recognise. If there is some, revoke the key now, even before the fix ships (the AI feature will break until it does).
  2. Create a new key and put it in a server-only env var: OPENAI_API_KEY or ANTHROPIC_API_KEY, no NEXT_PUBLIC_/VITE_ prefix.
  3. Move the call to the server (below), remove the flag and the key from the frontend, and deploy.
  4. Revoke the old key once the new version is live. Deleting it from your code doesn't help on its own: it's in old builds, caches and anyone's copy.
  5. Set a spend limit or budget alert in the provider console, so a future leak has a ceiling.

4. The server route (Next.js example)

// app/api/chat/route.ts  (runs on the server, key never reaches the browser)
import OpenAI from "openai";
import { createClient } from "@/lib/supabase/server"; // or your auth

const openai = new OpenAI(); // reads OPENAI_API_KEY from the server env

export async function POST(req: Request) {
  const supabase = await createClient();
  const { data: { user } } = await supabase.auth.getUser();
  if (!user) return new Response("Unauthorized", { status: 401 });

  const { message } = await req.json();
  if (typeof message !== "string" || message.length > 4000)
    return new Response("Bad request", { status: 400 });

  // add a per-user rate limit here (e.g. Upstash, a Supabase counter table)

  const r = await openai.responses.create({
    model: "gpt-6.2-sol",         // pick the model on the server, not from the request
    instructions: "You are a helpful assistant for ...",
    input: message,
    max_output_tokens: 800,
  });
  return Response.json({ text: r.output_text });
}

The browser now calls fetch("/api/chat", ...) with no key. On Vite, Lovable or Bolt apps without a server, the same code goes in a Supabase Edge Function (keep JWT verification on and check the user; see Edge Function auth). Anthropic is the same shape with new Anthropic() and messages.create.

A server route without a sign-in check just moves the problem: anyone can call /api/chat and spend your credits through it. Keep three things on the server: who's allowed (sign-in, and a per-user rate limit), what model and system prompt are used, and how big a request can be.

5. When browser use is fine

A prompt for Lovable, Bolt or Cursor

Search my project for dangerouslyAllowBrowser and for any OpenAI or Anthropic API key used in browser code (including NEXT_PUBLIC_ or VITE_ env vars). Move every AI call to a server-side route (or a Supabase Edge Function if there is no server) that reads the key from a server-only env var, rejects requests without a signed-in user, limits each user's requests per minute, caps input length and max output tokens, and sets the model and system prompt on the server. Update the frontend to call that route without any key, remove dangerouslyAllowBrowser and the public env vars, and list every file you changed. Remind me to create a new key and revoke the old one.

What an outside scan can and can't see

Abbi Labs reads your live site from the outside, signed out. It downloads the home page and its same-origin JavaScript and flags OpenAI-style and Anthropic keys there as critical (masked, never stored in full), along with other secret key patterns. It lists AI-style endpoints it finds in your JavaScript, such as /api/chat, but never calls them, so it can't tell you whether they require sign-in; test that yourself with a signed-out curl -i -X POST. It doesn't look for the dangerouslyAllowBrowser flag itself, and won't see keys that only load on signed-in pages. If you just want to check a single key you found, the is this key safe? tool tells you, in your browser, whether it's public by design or a secret to rotate now.

Scan your app free

FAQ

What does dangerouslyAllowBrowser actually do?

It turns off one safety check. The official openai and @anthropic-ai/sdk JavaScript libraries detect when they're running in a browser and throw an error instead of sending requests. The flag skips that check. It doesn't hide, encrypt or proxy your key: whatever you pass as apiKey is still in your JavaScript and in the Authorization or x-api-key header of every request.

I keep the key in a .env file. Isn't that hidden?

Not if the code runs in the browser. Variables with NEXT_PUBLIC_ or VITE_ prefixes (or anything a bundler inlines) are copied into the JavaScript at build time. If the browser can use the key, anyone can read it in DevTools.

Is it ever OK to use?

OpenAI's README lists a few cases where the risk is lower: internal tools used only by trusted people, credentials with very limited scope, and short-lived development keys that aren't used in production. A public app with a long-lived production key isn't one of them.

What about a user-supplied key (bring your own key)?

That's the main legitimate browser use: the user pastes their own key and it never leaves their browser except to the provider. Then your own key isn't involved. Don't store their key on your server or in localStorage unless they understand that, and never fall back to your key when theirs is missing.

How do realtime voice apps connect from the browser then?

OpenAI's Realtime API docs describe browser connections that keep the standard key on your server: either your server sits in the middle of the session setup, or your server calls /v1/realtime/client_secrets with the real key and hands the browser a short-lived ephemeral key. Your token route still needs to check the user is signed in, or anyone can mint tokens on your account.

Does Abbi Labs check this?

Partly. The free outside scan downloads your home page and its same-origin JavaScript, signed out, and flags OpenAI-style (sk-, sk-proj-, sk-svcacct-) and Anthropic (sk-ant-) keys it finds there as critical, showing only a masked value. It doesn't search for the dangerouslyAllowBrowser flag itself, doesn't crawl other pages or sign in, and won't see a key that's fetched at runtime or only loaded on a signed-in page. It also lists AI-style endpoints in your JavaScript (like /api/chat) without calling them, so you can check each one requires sign-in.

Related

Weekly AI-app security checklist

One short email a week: a practical check for apps built with Supabase, Next.js and AI tools (RLS, leaked keys, headers and more). Unsubscribe anytime.