Exposed API Keys in JavaScript: How Hackers Find Your Secrets
Published December 9, 2025 · By DomainOptic · 8 min read
Exposed API Keys in JavaScript: How Hackers Find Your Secrets
Public JavaScript bundles can contain credential-like strings, including values that were accidentally exposed and values intentionally designed for clients. Automated scanners can discover these patterns quickly, so every confirmed secret should be treated as exposed without assuming a universal exploitation timeline.
What Actually Gets Exposed
Your JavaScript bundle is public. Anyone can view it. When you accidentally include secrets, here's what they find:
The expensive ones:
- AWS access keys (AKIA followed by 16 characters) - These get used for crypto mining. People wake up to $45,000 AWS bills.
- Stripe secret keys (sk_live_) - Full access to your payment system. Refunds, customer data, everything.
- OpenAI keys (sk-) - Get sold on Telegram. Someone runs a wrapper service on your dime.
The dangerous ones:
- Firebase configs with write access - Your entire database, open to the world
- GitHub tokens - Access to your private repos
- Database connection strings - Direct access to your data
The annoying ones:
- Google Maps API keys without restrictions - Get used for other people's apps, you get the bill
- Twilio credentials - Someone sends 10,000 SMS messages
- Slack webhooks - Random messages in your channels
How They Find Them
It's not sophisticated. The tooling is trivially available.
Pattern matching. Simple regex against your JavaScript:
\\\
AKIA[0-9A-Z]{16} # AWS
sk_live_[a-zA-Z0-9]{24} # Stripe
sk-[a-zA-Z0-9]{48} # OpenAI
ghp_[a-zA-Z0-9]{36} # GitHub
\\\
Source map hunting. If you accidentally ship .map files, attackers get your original source code. Makes finding secrets trivial.
Variable name scanning. Search for assignments to variables named apiKey, secret, token, password, credential. You'd be surprised how often this works.
Real Damage I've Seen
An exposed cloud credential can be abused to create resources, access data, or generate charges. An exposed payment-provider secret can expose customer or transaction operations allowed by that key. An exposed model-provider key can consume quota or reveal data available to the associated account. The actual impact depends on the credential's permissions, which is why confirmed secrets should be revoked or rotated immediately.
How to Check Your Site
The manual way:
- Open your production site
- View Page Source (or DevTools > Sources)
- Ctrl+F for: AKIA, sk_live, sk_test, sk-, ghp_, apiKey, secret, token
- In Network tab, look for any .map files loading
The fast way:
Our Secret Scanner downloads publicly accessible JavaScript, checks hundreds of credential-like patterns, looks for exposed source maps, and flags leads for manual review.
How to Fix It
Never hardcode secrets. Use environment variables:
\\\javascript
// NO. Just no.
const stripe = require('stripe')('sk_live_abc123');
// Yes.
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
\\\
Keep secrets on the backend. If your frontend needs to call Stripe or OpenAI, make it go through your server:
\\\
Browser → Your API → Stripe
(key lives here, never sent to browser)
\\\
Disable source maps in production. In webpack:
\\\javascript
devtool: process.env.NODE_ENV === 'production' ? false : 'source-map'
\\\
Use restricted keys where possible. Stripe publishable keys are meant for the frontend. AWS IAM policies can limit what a key can do. Google Maps keys can be restricted to specific domains.
If You've Already Leaked a Key
Don't just generate a new one. The old one is still out there and still valid.
- Rotate immediately - Create new credentials
- Revoke the old key - This is the step people forget
- Check access logs - See if it was already used
- Check your bills - Look for unexpected charges
- Scan your git history - Secrets in old commits are still findable
And then figure out how it happened and fix your build process so it doesn't happen again.
Scan your site for exposed secrets
Website Security
Continue with public-site security