t0ul

Vibe Coding Securely

Vibe Coding Securely, Ch. 2: Secrets Never Ship

Part of Vibe Coding Securely, a short, hands-on book about shipping the app an AI built without getting owned.

The most common way an AI-built app leaks is also the simplest, which is why it is first. Your app needs a secret: a key to some service it talks to. The tool wires the key up for you, and, trying to be helpful, it often puts the key somewhere the browser can reach. The moment it does that, the key is not a secret any more. Everyone who visits your site has a copy.

Let me show you exactly how, in Vibe Notes, and then fix it, because this is also the chapter where we walk the loop from chapter 1 for real. You will see the weak spot, run the check that catches it, make the change, and run the same check again to watch it pass.

To follow along, put the app at the start of this chapter:

git checkout ch2-start

The feature, and where the key went

Vibe Notes shows a small “Synced” badge near the top. To fill it in, it asks a cloud sync service whether things are up to date, and that service wants an API key. Here is the code the AI wrote for it, in app/sync-status.tsx:

"use client";

import { useEffect, useState } from "react";
import { SYNC_API_KEY } from "@/lib/secrets";

export default function SyncStatus() {
  const [ok, setOk] = useState<boolean | null>(null);

  useEffect(() => {
    fetch("/api/sync/ping", { headers: { "x-sync-key": SYNC_API_KEY } })
      .then((r) => setOk(r.ok))
      .catch(() => setOk(false));
  }, []);

  return <p className="sync">{ok === null ? "Checking sync..." : ok ? "✓ Synced" : "Offline"}</p>;
}

The first line, "use client", is the important one. It marks this as a client component: code that runs in the visitor’s browser, not on your server. That is what makes the badge interactive. But look at what it does. It imports SYNC_API_KEY and sends it along with the request.

And SYNC_API_KEY is a real secret, sitting in a file, lib/secrets.ts:

export const SYNC_API_KEY = "vibenotes_sk_live_9f2c7a41d8b30e65";

(It is a fake key that talks to a local stub, so Vibe Notes runs offline. Pretend it is the real key to your email service or your database.)

Here is the problem in one sentence. For the browser to use the key, the key has to be in the browser. So when your app is built, that key is baked into the JavaScript the browser downloads. You did not put it on a public page on purpose. The tool put it there for you, by reaching for it from code that runs in the browser.

Seeing it

We do not have to take my word for it. Run this chapter’s check:

npm run check:ch2

It builds the app the way it would ship, then searches the browser’s JavaScript for the key:

Building, then scanning the browser bundle for a secret...

FOUND: the sync key is in 1 browser file(s), e.g.
  .next/static/chunks/3phdisnd8n76k.js
Anyone who opens the site can read it in DevTools. See chapter 2.

That red “FOUND” is the weak spot, caught. The check built the app and found the key in a file called a bundle, one of the JavaScript files your site hands to every visitor. If you are curious, you can see it yourself. That file is plain text, served to anyone, and the key is right there in it:

grep -ro "vibenotes_sk_live_[A-Za-z0-9]*" .next/static/chunks
.next/static/chunks/3phdisnd8n76k.js:vibenotes_sk_live_9f2c7a41d8b30e65

No break-in, no cleverness. Anyone who opens your site, opens the browser’s developer tools, and searches the downloaded files finds the key. On a real app, that key might let them read your database, send email as you, or run up a bill on your account. A secret in the bundle is a secret you have published.

The fix: keep the secret on the server

The rule is short: a secret belongs on your server, never in code the browser downloads. The browser should not hold the key at all. Instead, it asks your own server to do the thing that needs the key, and your server, which the visitor never gets to read, holds it.

So the badge changes to ask our server a plain question, with no key in sight:

"use client";

import { useEffect, useState } from "react";

export default function SyncStatus() {
  const [ok, setOk] = useState<boolean | null>(null);

  useEffect(() => {
    fetch("/api/sync/ping")
      .then((r) => setOk(r.ok))
      .catch(() => setOk(false));
  }, []);

  return <p className="sync">{ok === null ? "Checking sync..." : ok ? "✓ Synced" : "Offline"}</p>;
}

And the thing it calls, app/api/sync/ping/route.ts, runs on the server. A route handler like this is server code: it is never sent to the browser, so the key in it stays put.

import { NextResponse } from "next/server";

const SYNC_API_KEY = process.env.SYNC_API_KEY ?? "vibenotes_sk_live_9f2c7a41d8b30e65";

export async function GET() {
  const reachable = SYNC_API_KEY.startsWith("vibenotes_sk_live_");
  return NextResponse.json({ ok: reachable });
}

Two things to notice. First, the key never crosses to the browser now. The browser calls /api/sync/ping, the server uses the key on its behalf, and the browser only ever learns “synced” or “offline.” Second, in a real app the key would come from process.env.SYNC_API_KEY, an environment variable: a value you set on the server and never write into your code or commit to git. The hardcoded fallback here is only so Vibe Notes runs for you offline. Keeping secrets out of your code and out of git is the whole of chapter 4 of Git and GitHub the Safe Way, and it is the same lesson from the other side.

If you want the app to jump straight to the fixed version:

git checkout ch2-done

Proving it

Now run the exact same check again:

npm run check:ch2
Building, then scanning the browser bundle for a secret...

BLOCKED: the sync key is not in the browser bundle.

That is the receipt. The command that found the key a minute ago now finds nothing, because there is nothing to find. The badge still works, the sync still happens, and the secret stays on the server where it belongs. You did not just move some code around and hope; you proved the leak is closed with the same tool that proved it was open.

If it already leaked, move the key and change it

One honest warning, because it is the part people skip. If a key was ever in your bundle, or ever committed to git, moving it is not enough. Anyone who already copied it still has a working key. Your git checkout does not reach into their notes.

So when a real secret has been exposed, you do two things, in this order. You move it to the server, as above, so it stops leaking. Then you rotate it: go to the service, generate a brand new key, put the new one in your server’s environment, and disable the old one. Moving the key stops the next leak. Rotating closes the one that already happened. If you are not sure whether a key was exposed, assume it was and rotate it anyway; it costs a few minutes and a new string.

Where this leaves us

You just walked the whole method on a real weakness: you saw the secret sitting in the bundle, a check caught it, you moved it to the server, and the same check went quiet. Every chapter from here has that shape.

Next we stop trusting the browser about secrets and start trusting it about something more tempting: who you are. The app has a login, and as the tool wrote it, claiming to be someone else is as easy as editing one value in your browser. That is chapter 3.

← Vibe Coding Securely