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.
- CWE: CWE-522 โ Insufficiently Protected Credentials
- OWASP: A07:2021 โ Identification and Authentication Failures
- CVSS: 8.6 (High) ยท Recommended:
error
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.