OpenAI API key leaked in a frontend bundle: what to do
Revoke the exposed OpenAI key, review usage and move model requests to an authorized server endpoint. A browser environment variable cannot keep a private model-provider key secret.
Check your app free, first fix freeTreat a genuine provider key in a downloadable JavaScript asset as exposed. Replace its authority at the provider, fix the frontend-to-server boundary and check the replacement build. Protect the new server route against unauthorized spending, and keep usage investigation separate from the question of whether the current bundle still contains the old value.
Contain spending before polishing the build
Identify the affected OpenAI project and key privately, then revoke the exposed credential through its supported account controls. Review usage and billing for unexpected requests. Do not paste the full key into a public issue or use a third-party key checker to decide whether it works.
A scanner may recognize a key-shaped value without knowing whether it is active. If it is a synthetic fixture, document that distinction. If it was a real private key, deleting the JavaScript file does not invalidate copies already downloaded by visitors.
Move the model call, not just the variable
The repair is architectural: the browser asks your server for an allowed feature; your server validates the caller and performs the model request using protected configuration. Returning the secret to the browser from a configuration endpoint defeats that boundary.
POST /api/generate
verify the user and allowed feature
validate input size and requested operation
enforce per-user limits appropriate to the product
call the model provider using server-only configuration
return the intended result, never the provider credentialThis is a request-flow checklist, not a working authentication or quota implementation. A public unrestricted endpoint can still spend your money even when the provider key is hidden. Test both an allowed request and a denied request on your actual server.
Check how the key entered the browser
Search source literals, public environment prefixes and serialized error responses. A VITE_OPENAI_API_KEY or NEXT_PUBLIC_OPENAI_API_KEY can be inserted into client assets during a build. Renaming it to OPENAI_API_KEY helps only if the consumer also runs exclusively on the server.
Inspect the browser's actual JavaScript requests after redeploying. A source search alone can miss a stale deployment, a cached HTML document pointing at an old asset or a separate public configuration endpoint. Keep the production URL and release identifier with the repair notes.
Verify behavior without creating a new exposure
Use a synthetic prompt and controlled account to check that the server feature still works. Verify an unauthenticated or unauthorized caller is denied where the feature requires an account. Avoid printing the replacement key or entire environment during debugging.
Configure usage controls appropriate to your account, but do not claim that a dashboard budget necessarily provides an immediate hard cap. Check the provider's current behavior and your own enforcement requirements. An application-level quota can be important even when the provider offers spending alerts.
Finally, keep incident investigation and code repair as separate checklist items. A clean bundle is evidence about the current deployment, not proof that nobody used the earlier credential.
Turn the check into a release decision
Keep a small record of the production URL, the time you checked it, the exact finding and the action you took. A local preview and a deployed application can have different environment variables, response headers and database projects. After a repair, deploy the intended version and check the public address again. Do not assume that changing a repository file changed the live system.
Read coverage as carefully as the score. A timed-out asset, an ambiguous backend configuration or a request that could not safely establish access should remain unknown. It should not disappear from your release notes merely because there is no red finding. If a check was not completed, decide who will review it and what additional evidence is needed.
ProofForge's free report includes the first fix. The $9 Fix Report unlocks all findings' available repair guidance and builder prompts, with a PDF and private report link. Fix + Guard costs $29 once and adds builder rules, on-demand deploy-hook scans for 30 days and four weekly rechecks you start yourself. These are different deliverables, not different promises that an application is completely secure.
Keep private report URLs out of public issues and commits. If you need shareable evidence, use the owner-verification and publication flow for an eligible receipt. A signature protects the recorded assessment against undetected changes; it does not make the assessment exhaustive, prove a business outcome, or show that a later deployment still matches an earlier result.
Questions builders ask
Can I hide an OpenAI key with a public environment variable?
Is a server proxy enough?
Does a key pattern prove the key works?
Related checks and fixes
Continue with a related guide
Sources
Scan your app free
See what your live app ships to every visitor, with the first fix free. A clean scan is not a full audit. Run it only on an app you own or have permission to test.
Scan your app free