Secrets in Frontend Code: How to Find and Fix Exposed API Keys
Vibe CodingHetz Team

Secrets in Frontend Code: How to Find and Fix Exposed API Keys

Why API keys and tokens end up in browser code, how to find them in a built app, which keys are safe to expose and how to move secrets to the server.

Many AI-generated apps work because a key is sitting in the browser. An assistant puts a service key into a frontend file so that a feature runs on the first try, the demo works, and nobody remembers the key is there. Once the app is live, that key is available to anyone who opens the developer tools. Secrets in frontend code are one of the most common and most avoidable problems we find in vibe-coded products.

This guide explains why keys end up in browser code, which ones are safe to expose, how to find the ones that are not, and how to move them to the server. For the broader risks in apps written with AI tools, read our guide to vibe-coded app security. For the platform-specific view, see AI app builder security.

Why Secrets End Up in the Browser

Frontend code is the part of your app that runs on the user's device. It is downloaded as a bundle of JavaScript, often minified, and it is readable by anyone who loads a page. Developers know this, but it is easy to forget when a coding assistant produces a working feature in one step.

Three patterns cause most leaks:

  • The quick connection. The assistant calls an API directly from the browser, with the key in the request, because that is the shortest path to a working result.
  • The environment variable with the wrong prefix. Many frontend build tools include variables with a public prefix in the bundle. If a private key is named with that prefix, it is copied into the output for everyone to read.
  • The copied example. Tutorials and documentation often show a key inside the frontend code. The example is correct for learning, and it becomes a leak when the same code reaches production.

None of these need a clever attacker. They need only a visitor with a browser and a few minutes.

Which Keys Are Safe to Expose

Not every key is a secret. Providers usually label them, and the label matters:

Key typeUsually safe in the browser?Why
Publishable or public client keyYes, with rulesDesigned to identify your app. Access must be enforced by the server or by database rules.
Analytics or tracking identifierYesMeant to be seen by every visitor.
Service role or admin keyNoBypasses your access rules and grants broad rights.
Payment provider secret keyNoCan create charges, refunds and payouts.
AI provider API keyNoAnyone with the key can run calls on your account and bill it.
Signing secret or webhook secretNoUsed to verify requests. Exposure lets an attacker forge them.

The safe keys are safe because the rules around them hold. A publishable key that can read every row of a table is not safe, even though the key itself is meant to be public. Check the rules, not only the key's label.

How to Find Secrets in a Built App

Search the output of a production build rather than the source code, because the build shows what visitors actually receive. Follow these steps:

  1. Build the app for production. Use the same command your host runs, then open the output folder.
  2. Search the files. Look for the words "secret", "private", "service_role", "admin" and the key prefixes of your payment and AI providers. Include minified files.
  3. Check the network tab. Load each page, open the developer tools and read every request. Look for keys in headers, query strings and request bodies, and for responses that contain values the browser should never see.
  4. Check environment variables. Read the variable names in your build configuration. Any name with a public prefix is copied into the bundle, so it must hold only public values.
  5. Check the repository history. A key removed from the latest commit still exists in earlier commits. GitHub's secret scanning documentation explains how scanning can flag known key formats in a repository.

Automated scanners speed this up, but they cannot see keys assembled at runtime, values loaded from a database or keys in a format they do not recognize. Treat them as one layer.

What to Do When You Find One

A key found in frontend code should be treated as compromised, even if you believe nobody has copied it. Follow this order:

  1. Rotate or revoke the key in the provider's dashboard. Create a new key for the server.
  2. Check the provider's logs for calls you did not make, unusual volume or new locations.
  3. Move the call to the server. The browser sends a request to your own endpoint, and the endpoint calls the provider with the key.
  4. Store the new key as a server secret, using your host's secret manager, not a committed file.
  5. Rebuild and redeploy, then search the new bundle again to confirm the key is gone.

Removing the key from the code is not enough on its own. The old bundle may still be cached, the key remains in history, and anyone who copied it can keep using it until it is revoked.

Moving a Private Call to the Server

A typical fix moves the private call into a server function that the browser calls. The browser sends only what the user is allowed to request, and the server adds the key, checks the user and returns a limited response:

  • The browser calls your endpoint, for example to generate a summary of the user's document.
  • The endpoint confirms that the user is signed in and owns the document.
  • The endpoint calls the AI provider with the private key, stored as a server secret.
  • The endpoint returns only the summary, not the provider's full response or the key.

This pattern also solves a second problem. The server can limit how often each user may call an expensive feature, which the browser cannot enforce.

Mistakes Made While Fixing a Leak

The fix itself is where many teams make a second mistake. The most common one is hiding the key instead of moving the call. Wrapping the key in base64, splitting it into two strings or loading it from a separate file still ships it to the browser, and anyone can reverse those steps in seconds.

A second mistake is moving the call to a server endpoint that accepts any request. If the endpoint forwards whatever the browser sends to the provider, it becomes an open proxy that anyone can use with your key. The endpoint must check that the user is signed in, limit what the request may ask for, and apply a rate limit.

A third mistake is returning too much. A server function that passes the provider's full response back to the browser can expose headers, internal identifiers or account details. Return only the fields the feature needs.

Finally, teams often forget the other places a key can live: preview deployments, logs, error messages and analytics events. An error handler that prints the full request can write the key into a log that many people can read. After a fix, check those places as well as the bundle.

Prompts That Prevent the Problem

Coding assistants follow clear rules. Put these in your project instructions, so each new feature starts with them:

  • Never place secret keys in frontend code, configuration files that the build exposes, or example snippets.
  • Use server functions for every call that needs a private key, and send only the minimum data back to the browser.
  • Use only the public prefix for variables that are safe to expose, and name private variables so they cannot be included in the bundle.
  • Never commit a file that contains a key. Read keys from the environment.

Rules reduce the mistakes the assistant makes, but they do not replace checking the output. The bundle, the network tab and the repository history are still the places where keys show up.

Where Testing Adds Value

A code review can miss a key that is only assembled at build time, and a secret scanner can miss a key in a format it does not know. A penetration test looks at what the live app actually delivers. It loads the pages, reads the bundle and the network calls, and tries to use any credential it finds, which is the most reliable proof that a secret is exposed.

Hetz tests the live app on your verified domain. A finding includes the exact request or file that exposed the value, a fix for your stack and a retest once the fix is live. The retest matters for keys, because it confirms that the old key no longer works and that the new bundle no longer carries it. For the wider picture, see vibe coding security testing and automated vs manual penetration testing.

Next steps

Build your app, search the output for secrets and rotate any key that was public. Then add the rules above to your project instructions, so the next feature starts safe. To see what a full test covers and what it costs, read our guide to penetration testing cost, or compare options on pricing. When you are ready, start a scan.

FAQ

Frequently asked questions

Keep reading