Skip to content
Tamandua
← All posts

Your API key is in the bundle: what to move server-side before obfuscating anything

Anything your front end ships, anyone can read. How secrets end up in JavaScript bundles, which keys are safe to expose, and the server-side patterns that remove the rest before obfuscation is even on the table.

Tamandua Team · · 6 min read

Every byte of JavaScript your site sends to a browser can be read by whoever is using that browser. Minification makes it harder to skim, and obfuscation makes it slower to understand, but neither makes it secret. If an API key is in the bundle, it is public. This post covers how keys end up there, which ones are fine to expose, and what to move server-side first.

How secrets end up in a bundle

It is rarely a developer pasting a key into a component. More often it happens through the build:

  • Public environment prefixes. Front-end build tools inline some environment variables into the code they emit. In Vite, variables prefixed with VITE_ are exposed to client code; in Next.js, the prefix is NEXT_PUBLIC_. Adding the prefix to make an error go away is one of the most common ways a server key reaches production.
  • Shared config modules. A config.ts imported by both server and client code pulls every value it holds into the client bundle unless the bundler can prove the import is server-only.
  • Source maps. Publicly served .map files reconstruct the original source, including comments and constants that minification seemed to hide.
  • Third-party snippets. Analytics, chat and payment widgets are configured with keys in the page. Some of those keys are designed to be public. Some are not, and the snippet does not tell you which.

Publishable keys and secret keys are different things

Many providers split credentials into two kinds. A publishable key identifies your account and can only do what is safe to do from an untrusted client: Stripe's publishable key creates payment tokens but cannot read charges or issue refunds (Stripe API keys). A secret key can do anything your account can do.

A publishable key in the bundle is expected. It still deserves the restrictions the provider offers, such as the HTTP referrer and API restrictions on a Google Maps Platform key, because anyone can copy it and spend your quota from their own page. A secret key in the bundle is an incident: rotate it, then find out how it got there.

What to move server-side

The rule is simple: if a value grants something you would not grant to an anonymous visitor, the browser must not hold it. In practice that means a small number of patterns.

A backend-for-frontend proxy

Instead of calling a third-party API from the browser with a secret key, the browser calls your own endpoint, which authenticates the user, checks what they are allowed to do, and makes the upstream call with a key that never leaves the server. The proxy is also where you add rate limits, input validation and logging.

Short-lived, scoped tokens

When the browser genuinely has to talk to a service directly (uploads to object storage, a real-time channel), have your server mint a token for that one user and that one operation, valid for minutes. A pre-signed upload URL or a scoped session token limits the damage if it is copied to roughly what the user could already do.

Server-side decisions

Prices, discounts, feature entitlements and "is this user allowed to do X" checks belong on the server. Client code can show or hide a button; it cannot be the thing that grants access. Anyone can edit the JavaScript running in their own browser, with or without obfuscation.

Where obfuscation fits after that

Once secrets and decisions are off the client, what is left in the bundle is code: UI, validation, maybe some algorithm you would rather competitors did not copy in an afternoon. That is where obfuscation earns its cost. It raises the effort needed to read and reuse that logic. It does not turn a client-side check into a security boundary, and it does not make an exposed key safe. The OWASP Secrets Management Cheat Sheet covers the server side of this in depth.

Next step: audit your public environment variables

  1. List every variable with your bundler's public prefix (VITE_, NEXT_PUBLIC_, REACT_APP_, PUBLIC_) in your CI and hosting settings.
  2. For each one, look up the provider's documentation and confirm it is a publishable key, not a secret one.
  3. For each publishable key, turn on every restriction the provider offers: allowed origins, allowed APIs, quotas.
  4. Check that your production site does not serve .map files to anonymous visitors.
  5. Anything that fails step 2: move the call behind your own endpoint, then rotate the key.

Where Tamandua Web Guard fits

Tamandua Web Guard is invite-only and default-off; a request records interest and does not grant access. The source contains a bounded path that renames local identifiers in one JavaScript ES module, but hosted alpha end-to-end remains on hold. Claim transformed output or a receipt only after an explicitly enabled job completes and its output is independently verified. Web Guard is not anti-tamper, anti-debugging or anti-reverse-engineering protection, and it leaves strings, exports and function names in place, so it would not hide a key even if hiding keys were a good idea. Do the steps above first. What JavaScript obfuscation does (and doesn't) protect covers the next layer.

Harden the JavaScript you ship

Web Guard hardens browser code in your build pipeline and returns a receipt for every protected bundle.