Skip to main content
interlace
Plugin: anthropic-securityRules

no-browser-api-key-exposure

Forbid dangerouslyAllowBrowser, which exposes the Anthropic API key to the client

Forbid dangerouslyAllowBrowser, which exposes the Anthropic API key to the client.

Why

The Anthropic SDK refuses to run in a browser by default. dangerouslyAllowBrowser: true is the opt-in that lifts that refusal, and the SDK's own JSDoc explains what it costs โ€” client-side use "risks exposing your secret API credentials to attackers" (client.d.ts, v0.115.0). A key that reaches the browser is readable by anyone who opens devtools, and it is billable by whoever finds it.

This rule is that warning, enforced.

The rule is gated on the SDK being imported, so it stays silent in files that do not construct the client. An explicit dangerouslyAllowBrowser: false is the safe choice already made and reports nothing. A spread ({ ...base }) or a runtime-decided value ({ dangerouslyAllowBrowser: isBrowser }) is treated as unreadable rather than guessed at.

Incorrect

import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({ apiKey: KEY, dangerouslyAllowBrowser: true });

Correct

// Server route โ€” the key never leaves the server.
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });

The browser calls your route; your route calls Anthropic and forwards the result.

Did this rule catch something? Star the repo to get new CWE coverage as we ship it โ€” or follow the AI-code-security benchmarks behind these rules.