Vibe Coding Securely, Ch. 7: Shipping It
Part of Vibe Coding Securely, a short, hands-on book about shipping the app an AI built without getting owned.
Everything so far happened on localhost, where you are the only visitor and the app is talking to itself. Shipping changes the audience. The moment your app is on the public internet, anyone can send anything to any endpoint, from any site, as many times as they like, and none of them are being polite. A few things that were harmless on your laptop become doors the day you deploy, and they tend to be leftovers: code you added to help yourself build, and settings you loosened to make development work. This chapter finds two of them in Vibe Notes, deletes them, and then names two more that a quick check will not catch but that you have to think about before you go live.
Put the app at the start of this chapter:
git checkout ch7-start
The debug route you forgot to delete
While building an app, it is genuinely useful to have a page that just shows you everything in the database. So the tool made one, app/api/debug/route.ts:
export async function GET() {
const users = getDb().prepare("SELECT * FROM users").all();
const notes = getDb().prepare("SELECT * FROM notes").all();
return NextResponse.json({ users, notes, node: process.version });
}
No login check, no owner check, nothing. It dumps every user and every note to anyone who asks. On your laptop that is a handy shortcut. On the public internet it is the whole database available at a URL. You do not even have to be logged in:
curl -s http://localhost:3000/api/debug
{"users":[{"id":1,"username":"alice","role":"admin"},{"id":2,"username":"bob","role":"user"}],
"notes":[{"id":1,"user_id":1,"title":"Alice's diary","body":"Private. The spare key is under the third flowerpot."},
{"id":2,"user_id":1,"title":"Welcome","body":"..."},
{"id":3,"user_id":2,"title":"Bob's todo","body":"..."}],
"node":"v24.22.1"}
Everything we spent four chapters protecting, the users and their roles, alice’s private diary, is handed over in one unauthenticated request. All the careful auth and ownership checks on the real routes do not matter, because this route does not use them. It is the single most common way a vibe-coded app leaks: not a clever exploit, just a convenience endpoint that shipped. Notice it even reports the Node version, which is a small gift to an attacker deciding which known bugs to try.
The CORS line that invites every website in
The second leftover comes from a frustration you will probably hit yourself. In development, a request from one origin to another gets blocked by the browser, an error about CORS appears in the console, and when you ask an AI tool to fix it, it often reaches for the biggest hammer. Here is what landed in middleware.ts:
export function middleware(req: NextRequest) {
const res = NextResponse.next();
const origin = req.headers.get("origin");
if (origin) {
res.headers.set("Access-Control-Allow-Origin", origin);
res.headers.set("Access-Control-Allow-Credentials", "true");
}
return res;
}
export const config = { matcher: "/api/:path*" };
A word on what CORS is, because the error message makes it sound like an obstacle rather than a protection. CORS, cross-origin resource sharing, is the browser’s rule that a script running on one website cannot read responses from another website’s API by default. That rule is what stops a random site you visit from quietly reading your email provider’s API using your logged-in session. The headers above switch that protection off in the most dangerous way possible. They reflect back whatever origin asked, and they set Allow-Credentials: true, which together tell the browser: any site at all may call this API using the visitor’s cookies, and may read the answer.
See it by pretending to be a malicious site calling the API:
curl -s -D - -o /dev/null -H "origin: https://evil.example" http://localhost:3000/api/me | grep -i '^access-control-'
access-control-allow-credentials: true
access-control-allow-origin: https://evil.example
The API just told evil.example that it is welcome. In practice that means if one of your logged-in users wanders onto a malicious page, that page can make requests to your API as them and read their data. It is chapter 3’s lesson again, from the outside: you are trusting something the visitor’s browser is in a position to abuse.
There is a telling detail in how the code got this wrong. The first thing people try is Access-Control-Allow-Origin: *, a wildcard meaning “any site.” Browsers refuse to combine that wildcard with credentials, so the cookies do not flow and the development error often persists. The usual next step, and the one the tool took here, is to stop using the wildcard and instead echo back the exact origin that asked, which browsers do accept alongside Allow-Credentials: true. So the “fix” that finally silences the console error is the more dangerous one, because it is the version the browser is willing to honor with the user’s cookies attached. A setting that looks stricter than *, a specific origin rather than a star, is in fact the one that opens the door.
The fix is the delete key
Both of these are fixed the same way, and it is the most satisfying fix in the book: you remove them.
git rm app/api/debug/route.ts middleware.ts
The debug route existed only to help you build, so it goes. The CORS middleware existed only to silence a development error, and Vibe Notes does not need it at all, because the app and its API are the same origin: the browser is already allowed to call them, no headers required. Thirty lines deleted, zero added. If you are ever unsure whether a piece of code earns its risk, code that only ever helped you during development almost never does.
Jump to the fixed app:
git checkout ch7-done
Proving it
The debug route is gone:
curl -s -o /dev/null -w "http %{http_code}\n" http://localhost:3000/api/debug
http 404
And the API no longer greets other sites:
curl -s -D - -o /dev/null -H "origin: https://evil.example" http://localhost:3000/api/me | grep -i '^access-control-'
(none: no access-control-* headers)
The check confirms both:
npm run check:ch7
Debug route /api/debug: not found.
CORS: the API does not open itself to other sites.
BLOCKED: no debug route, and the API does not share itself with other origins.
Two more that a quick check will not catch
The check above found the two leftovers because they are easy to test from outside. Two other shipping risks are real but do not show up as a tidy FOUND, so you have to carry them in your head.
The first is no rate limit. Nothing in Vibe Notes stops the same caller from hitting an endpoint as fast as they can. You can watch it on the login route:
for i in $(seq 1 10); do
curl -s -o /dev/null -w "%{http_code} " -X POST http://localhost:3000/api/login \
-H 'Content-Type: application/json' -d '{"username":"bob"}'
done; echo
200 200 200 200 200 200 200 200 200 200
(That is a bounded ten requests on purpose, just to show the shape.) Ten for ten, none refused, and nothing would stop ten thousand. On a login that accepts passwords, that is how people brute-force their way in. On the AI assistant from chapter 6, if it were backed by a paid model, that is someone running up your bill while you sleep, a problem with the honest name “denial of wallet.” A rate limit is the fix: cap how often one caller can hit sensitive routes and return a 429 Too Many Requests past the cap. You can write one, but this is a place where the platform usually does it better. Managed hosts and edge networks like Vercel and Cloudflare offer rate limiting you turn on rather than build, which is less code and fewer mistakes.
The second is what your errors say. When something throws, the friendliest thing a framework can do is show you the full error, the stack trace, the file paths, the library versions. That is wonderful on your laptop and a map of your internals in production. Vibe Notes keeps its error responses deliberately boring, a plain {"error":"..."}, and that is the habit to keep: log the real details on the server where only you can read them, and send the visitor a generic message. The same instinct covers the values your app needs to run. In production, your database location and any API keys from chapter 2 and chapter 6 live as environment variables set on the host, never committed to git, and never, ever behind a NEXT_PUBLIC_ name, which as chapter 2 showed ships them to the browser.
The part that generalizes
Shipping is not a step at the end. It is a change of threat model. On localhost you are the only person who exists; in production, every endpoint you have will be called by strangers, from other sites, at machine speed, including the ones you only meant for yourself. So before you deploy, walk your own app with that in mind. Delete the things you built for your own convenience: debug routes, test endpoints, seed buttons, verbose logging. Close the things you loosened to make development work, the CORS being the classic. Assume each route will be hit a million times by someone who is not logged in, and make sure it still behaves. And keep the real secrets on the host, out of the code and out of the browser.
Straight talk. A good managed platform gives you a lot of this for free, and you should take it: HTTPS, a safe place to store environment variables, and rate limiting you can switch on. Leaning on the platform is almost always less code and fewer mistakes than rolling your own. But the platform will just as happily deploy your debug route and your reflect-any-origin CORS, because it cannot tell those apart from code you meant. The leftovers are yours to find and remove. A deploy host makes the good path easy; it does not make the bad path impossible.
Try it. Before you ship anything, search your own project for the two leftovers in this chapter: look for any route with “debug” or “test” in its name, and for a
middlewarefile that setsAccess-Control-Allow-Origin. Then open each of your API routes while logged out and see what comes back. Anything that answers with real data to a logged-out stranger is a route to fix or delete before it goes live.
Where this leaves us
You deleted the leftovers, closed the API to other sites, and know the two risks that do not announce themselves. That is most of the gap between “works on my machine” and “safe on the internet.”
It is still not all of it, because security is never finished, and one day something will go wrong anyway: a key gets committed, a dependency turns out to be bad, someone gets in. What decides whether that is a scare or a disaster is what you do in the first hour. The last hands-on chapter is a calm drill for exactly that moment: find out what happened, rotate what leaked, contain the damage, and read the logs that tell you how far it went. That is chapter 8.