OpenAI or Anthropic API Key Exposed
Catches leaked OpenAI project/legacy secret keys (sk-proj-, sk-) and Anthropic Claude API keys (sk-ant-api03-) before they cause runaway AI billing or unauthorized model usage.
- Rule Slug
- openai-anthropic-api-key
- Affected Files
- *
- Detection Method
- Regex + Entropy (>= 3.5)
Vulnerable Pattern vs. Hardened Fix
// lib/ai.ts
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'sk-proj-9aB3cD5eF7gH9iJ1kL3mN5oP7qR9sT1uV3wX5yZ7aB9cD1eF3gH5',
});// lib/ai.ts (server-only module)
import OpenAI from 'openai';
// SDK automatically reads process.env.OPENAI_API_KEY on the server
export const client = new OpenAI();Why This Fails in Production
Large language model API keys from OpenAI and Anthropic are prime targets for automated credential harvesters because they convert directly into high-value compute without requiring complex infrastructure setup. An exposed sk-proj- or sk-ant-api03- key can be plugged immediately into proxy resellers, scraping farms, or bulk inference scripts that burn through monthly spending limits and prepaid credit balances in minutes.
Beyond direct token billing abuse, leaked AI provider keys expose sensitive organizational assets tied to the workspace. Depending on the key scope, an attacker may be able to list uploaded fine-tuning datasets, download batch output files containing proprietary customer prompts, read stored Assistants or vector store files, or poison shared project resources. In regulated industries, unauthorized access to uploaded files or prompt logs can escalate from a billing incident into a reportable data breach.
A particularly common root cause in modern full-stack frameworks is prototyping an AI feature quickly in a local script, Jupyter notebook, or browser-side fetch call and forgetting to strip the literal key before pushing. Even if you delete the key in a follow-up commit, OpenAI and Anthropic partner with secret-scanning programs that automatically revoke keys spotted in public repositories, causing immediate 401 Unauthorized failures across your staging or production services.
All LLM calls must route through a server-side proxy or edge function that reads OPENAI_API_KEY or ANTHROPIC_API_KEY from encrypted environment variables, enforces per-user rate limits, and configures strict workspace budget alerts.
Remediation Steps
- 1Revoke the exposed API key immediately in the OpenAI Platform dashboard or Anthropic Console and generate a fresh project-scoped key with minimal permissions.
- 2Review recent API usage logs, token consumption spikes, and uploaded files/batches to verify whether unauthorized requests occurred.
- 3Move all LLM SDK instantiations to server-side routes reading process.env.OPENAI_API_KEY or process.env.ANTHROPIC_API_KEY. Never embed keys in client-side code.
- 4Set hard monthly spend limits and email threshold alerts on your AI provider workspace to cap blast radius.
Frequently Asked Questions
- Can I safely call OpenAI or Anthropic directly from a React or Vue frontend?
- No. Any API key embedded in browser JavaScript or prefixed with NEXT_PUBLIC_ / VITE_ can be extracted trivially from DevTools Network tabs. Always proxy AI requests through a backend API route or serverless function.
- Does PushCheck detect both legacy sk- keys and newer sk-proj- keys?
- Yes. The pattern matches OpenAI project keys (sk-proj-), legacy 48-character OpenAI secret keys (sk-), and Anthropic keys (sk-ant-api03-), combined with Shannon entropy filtering to eliminate placeholder values.
- What if I am using an OpenAI-compatible gateway with a different key format?
- Store all gateway tokens in .env files verified by PushCheck’s unignored .env check and avoid hardcoding literal token strings in source files.