How to fix: API Key or Secret Hardcoded in Client JavaScript
An API key, secret, or credential pattern was found embedded directly in client-side JavaScript. Any secret in client JavaScript is fully visible to anyone who visits the page.
Why it matters
API keys in client JavaScript are trivially extractable. Press F12 in any browser and view the network tab or page source. Exposed keys enable attackers to abuse third-party services at your cost: OpenAI API, AWS, Stripe, Twilio, etc.
Client-side JavaScript is delivered to every visitor's browser. There is no effective way to 'hide' a secret in frontend code. Any key embedded in client JS should be treated as compromised.
How to fix it
- 1Move all API calls to your server side. The browser calls your API, your server calls the third party.
- 2Use environment variables on the server. Never use NEXT_PUBLIC_ prefix for secrets.
- 3For browser-SDK APIs (Firebase, Stripe publishable key): these are designed to be public but should be origin-restricted in the provider's dashboard.
- 4Rotate any key that has been in client JavaScript immediately.
Code examples
Server-side API proxy
// Bad: secret in client code
const openai = new OpenAI({ apiKey: 'sk-...', dangerouslyAllowBrowser: true });
// Good: proxy through your server
// Client:
const response = await fetch('/api/ai/chat', { method: 'POST', body: JSON.stringify({ message }) });
// Server (API route):
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });References
Detected automatically by VulnRadar. The check id is api-key-hardcoded-in-js, stable across every scan so you can filter or diff on it.
More client-side checks
- HighCSP Allows 'unsafe-inline' Scripts
- MediumCSP Allows 'unsafe-eval'
- HighpostMessage Without Origin Validation
- HighSensitive Data Stored in localStorage
- HighDOM XSS via location.hash Assignment
- Highdocument.write() in Page Scripts
- Higheval() in Client-Side JavaScript
- MediumExternal Script Without Subresource Integrity
Questions
- How severe is API Key or Secret Hardcoded in Client JavaScript?
- VulnRadar rates this high severity in the client-side category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
- Does VulnRadar check for this automatically?
- Yes. It runs as part of every scan under the check id api-key-hardcoded-in-js, which stays the same between runs so you can filter, diff, or gate CI on it.
- Which standards does API Key or Secret Hardcoded in Client JavaScript map to?
- It maps to CWE-798. In the OWASP Top 10 it falls under A07:2021.
Missing a check?
The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.