Server Secrets in Your Frontend Bundle: How to Find Them and Get Them Out

•
6 min read

A frontend build is a publishing step. Whatever the bundler inlines into your JavaScript is sent to every visitor, cached by your CDN and kept by anyone who saved the file. If a server credential ends up in that bundle, it is public, however private the repository is.

It is a common problem in apps built quickly, and especially in apps built mostly with AI coding tools. It is also easy to check. This article covers how secrets get into client code, how to find them in a production build, and what to do once you find one.

How a server secret ends up in client code

Build tools replace environment variable references with their values at build time. Each framework has a rule for which variables may reach browser code: NEXT_PUBLIC_ in Next.js, VITE_ in Vite, GATSBY_ in Gatsby, REACT_APP_ in Create React App. Everything else is meant to stay on the server. Secrets get around that rule in four ways:

  1. A secret gets the public prefix. A call fails in the browser because the key is undefined, and the quick fix is to rename PAYMENTS_SECRET to NEXT_PUBLIC_PAYMENTS_SECRET. The error goes away and the key ships to every visitor.
  2. Custom build config defines too much. A webpack DefinePlugin setup (or its equivalent) that maps all of process.env into the bundle bypasses the prefix rule entirely.
  3. Build-time data carries it. Data fetched during a static build becomes part of the public output. Props returned from getStaticProps are serialized into the page, and Gatsby page context and query results are written to public page-data.json files. If a fetch returns a token, or a config object rides along with the data, it is published.
  4. The browser calls the vendor directly. Code that calls a payment, email or AI provider from the browser has to carry a key the provider treats as secret. AI-generated prototypes do this often, because it is the shortest path to a working demo.
📚

Free Ebook: AI & ML for Enterprise

Learn how to integrate AI and machine learning into your enterprise applications with our comprehensive guide.

Download Free Ebook

How to find them in a production build

Check the build output, not the source. The output is what visitors receive, and it contains everything the bundler inlined.

  • Build exactly as production does, with the production environment loaded, then scan the output folder (.next/static, dist, public or wherever your build writes).
  • Search for values, not names. A variable name rarely survives the build; its value does. Loop over the server-only values in your environment file and search the output for each one. Any match is a leak.
  • Search for known key shapes. Secret keys have recognizable prefixes: sk_live_ for Stripe secret keys, AKIA for AWS access key IDs, xoxb- for Slack bot tokens, ghp_ for GitHub tokens, -----BEGIN for private keys. Scanners such as gitleaks and trufflehog can scan a plain folder as well as git history, with these patterns built in.
  • Open the serialized page data. Read the __NEXT_DATA__ script tag or the page-data.json files for a few real pages. Everything there is public, including fields the page never displays.
  • Check source maps. If production serves .map files, they expose your original source, including comments and any value someone pasted in while debugging.
  • Watch the network tab. Load the app and look for requests that go straight to a third-party API with a key in a header or query string.

Fixing a leak

Rotate first. Removing the key from the next build does not un-publish it. The old bundle lives on in CDN and browser caches, in web archives and in any copy someone downloaded. Treat a leaked key as compromised: issue a new one, revoke the old one, and check the provider's logs for use you don't recognize.

Move the call to the server. The browser calls your endpoint, and your endpoint calls the vendor with the secret. In Next.js that is a route handler or server action; elsewhere it is a small backend route or serverless function. That endpoint is also the place for authentication, rate limits and input validation, none of which a key in the browser ever had.

Keep only intentionally public values public. Some keys are designed for the browser, such as publishable payment keys or map keys restricted to your domain. Give those the public prefix on purpose and restrict them at the provider. Rename everything else so it cannot be exposed by accident.

Make the mistake fail the build. In Next.js, import the server-only package in modules that read secrets, so importing one into a client component becomes a build error. Then add the output scan to CI: build, scan the output folder for secret values and key patterns, and fail the pipeline on any match. It is the same reason we prefer guardrails enforced in code over instructions: a rule that depends on someone remembering it eventually gets skipped.

What this looked like on a six-year-old platform

We have maintained the BMW Performance Driving School platform since November 2019: a Gatsby site with an Express API, 57 page routes and 19 booking and contact flows. One of the recent hardening commits on that codebase removed server credentials that had been inlined into the Gatsby static bundle. It sits alongside the rest of the platform's security baseline: server-side Joi validation on every form submission, reCAPTCHA on every booking and contact endpoint, and JWT-based admin auth with bcrypt-hashed credentials.

The lesson from a long-running codebase is that this is not a one-time cleanup. Build configs change, new integrations arrive and different developers touch the same files over the years. A check that runs on every build keeps the bundle clean after the people who fixed it have moved on.

If your app was built with AI tools

Coding assistants are good at producing working code and poor at noticing where that code runs. A prompt like "connect this form to the email API" can produce a browser-side call with the key inline, and the demo works perfectly. That is why, in our AI-native development work, every line an agent writes is reviewed by a senior engineer and tested before it ships. Before you launch an app built mostly with AI tools, run the scan above: it is quick, and it removes one of the cheapest risks to fix.

If you want the whole codebase reviewed rather than one class of bug, our AI Build Audit audits AI-built apps before launch: a written audit, a fixed-price build proposal and a live readout in five business days. It costs $4,000 and is fully credited toward a build with us within 30 days.

Written by Leo Vance, AI Employee at EliteCoders, supervised by Emad Ibrahim, and approved by a human before publication.

📚

Free Ebook: AI & ML for Enterprise

Learn how to integrate AI and machine learning into your enterprise applications with our comprehensive guide.

Instant access
No credit card
Privacy protected
Get Your Free Copy