An AI-built app can look finished long before it is safe to put online. The screens render, the happy path works, and the agent summarized the diff as "added auth." To secure an AI-built app before you ship, you review the code the way you would review a fast contractor: every route that touches data, every key that left the server, every query built from user input, and every default copied from a tutorial. The model is good at producing a plausible app. It is inconsistent about the checks that only matter when a second user shows up.
This guide is for people building with Cursor, Claude Code, Copilot, or a similar assistant, who do not have a security engineer on the pull request. You will run one review pass, with two test accounts, before you share the URL. Pair it with the security checklist before launch when you are ready to flip the domain.
What to review before you trust the generated diff
Review four things before you ship an AI-built app: server-side access control, credentials in the repo and the client bundle, queries built from request data, and defaults the generator left in place. Read handlers and server actions, not only components. Spend the first pass on anything that accepts an id, a file, a webhook, or a role.
Four patterns that show up in generated apps
These four patterns account for most of the serious issues in a first ship of generated code. Fix them in this order. A leaked key gets rotated before you redesign a page. An open query gets parameterized before you tune caching.
| Pattern | What you see in the diff | What you want instead |
|---|---|---|
| Missing access control | Session check, then findById(req.params.id) |
Query includes the owner id from the session |
| Exposed credential | NEXT_PUBLIC_, hardcoded key, key in a screenshot in the README |
Server-only env var, key rotated if it was ever public |
| Unsafe query | String concatenation into SQL, shell, or a filter | Bound parameters, allowlisted fields |
| Insecure default | Admin/admin, CORS *, DEBUG=true, open storage bucket |
Production config reviewed line by line |
The OWASP Top 10 names the same families: broken access control, injection, and security misconfiguration. You do not need to memorize the list. You need to recognize the four rows in your own files.
Missing access controls
A generated handler often confirms that someone is logged in, then loads whatever id arrived in the path. That is enough for a demo with one user. It is an open door once you have two.
Create user A and user B. Copy B's object id into A's request. If the response contains B's email, file, or invoice, the access check is missing. Add the owner to the query and return 404 when the pair does not match. Do this for updates and deletes, not only for GET. Server actions and tRPC procedures need the same test, because they feel "internal" and still run on user input. The full distinction is in authentication versus authorization.
Exposed credentials
Agents like working code. A working example often includes a real-looking key, or it puts the key where the button's client component can read it. Search the repository for provider prefixes (sk_, rk_, AKIA, ghp_, xox), for BEGIN PRIVATE KEY, and for connection strings. Then search the production JavaScript bundle. A secret that was "only in dev" is still in git history.
Move the call to the server. Rotate the key in the provider dashboard. Store the new value in the host's secret store. The steps for leaks, scanning, and rotation are in how to store API keys and secrets.
Unsafe queries
Watch for SQL built with template strings, Mongo filters that pass the request body straight through, and shell commands that interpolate a filename. A user-controlled sort field or a GraphQL resolver that spreads req.body into an update can change columns you never put on the form, including role or is_admin.
Use bound parameters. Allowlist sortable columns. Pick the fields your handler may write, and ignore the rest of the payload. If the agent generated an ORM call, raw fragments and queryRaw still need parameters.
Insecure defaults
Read the config that deploys, not the config in the README. Common leftovers:
- A seed user
adminwith passwordadminorpassword. - CORS that reflects any origin, including credentialed requests.
- A storage bucket or blob container set to public read.
- Debug mode, verbose errors, and stack traces enabled in production.
- A database port published in
docker-compose.ymland left that way on the server.
Turn each one off, or restrict it to a local file that production does not load. Confirm by requesting the live host, not by rereading the chat.
A single-sitting review pass
Set a timer for one working session on a branch. You are looking for ship blockers: missing owner checks, secrets in the client, and tutorial defaults in the file that deploys. The numbered list is the pass. Read every patch the agent offers before you merge it.
- List every server entry point: HTTP routes, server actions, webhooks, cron handlers, and background jobs that trust a payload.
- Mark the ones that read or write per-user data. Those get the two-account test.
- For each, confirm the owner id comes from the session or a verified token, never from the body alone.
- Search source, history, and the built client for secrets. Rotate anything you find.
- Search for query concatenation,
eval,dangerouslySetInnerHTMLfed by users, and shell calls. - Diff production environment variables against the sample env file. Delete tutorial values.
- Create a fresh user on staging and click the real product. Watch the network tab for ids you can swap.
- Write down anything you defer, with the route name, and fix access-control failures before you merge.
When the agent offers a fix, read the patch. A common second mistake compares the user id to a value the client sent, which the attacker can change. The session is the source of truth. Verify webhook signatures over the raw body. A handler that trusts JSON because "it comes from Stripe" will accept a body you post yourself.
What to tell the agent on the next prompt
Keep using the assistant, and give it rules that stop the same hole on the next feature. Paste the list below into the project instructions, ask for file paths, and run the two-user test yourself. The agent's confidence is a claim. The two accounts are the evidence. You own the lines that decide who sees which row.
- Every query that loads a user-owned row must filter on the id from the server session.
- Secrets stay in server environment variables. Never prefix a secret for the client.
- Database calls use parameters. Request bodies may set only the fields listed in the handler.
- Do not add seed admins, public buckets, or
CORS *to the production config. - After you change an endpoint, state how to test it with two users.
Then ask the agent to review the current routes against those rules and to point at file paths.
FAQ
Are AI-built apps less secure than hand-written ones?
They ship the same classes of bugs, often faster, and with less review. The risk comes from volume and from tutorial defaults, not from a special kind of vulnerability. A careful pass over access checks, secrets, queries, and config closes the gap for a small app. Skip that pass and the speed works against you.
Should I ban AI coding tools until I understand every file?
Use the tools, and add a review gate before production. Ban nothing you can test. Keep generated migrations, auth callbacks, and payment webhooks on a short list you always read. For a marketing page with no user data, the gate can be lighter. For anything that stores accounts, the two-user test stays mandatory.
What is the first bug to look for?
Look for a route that loads or updates a row by an id from the URL or body, with no owner constraint. That bug leaks data as soon as you have two accounts, and it hides behind a working demo. Secrets in the client bundle are the second place to look, because they leak even with a single user.
Can a scanner replace this review?
A scanner helps you repeat the search for known risky patterns, dependency alerts, and committed secrets. It does not click your product as user A and user B. Run both. Use the tool to catch regressions, and use the two-account test whenever you add a route that takes an id.
What to do next
Check out a clean branch, list the server entry points, and run the two-account test on each one that touches user data. Rotate any key you find in the client or the history. Then read the production env file line by line against the table above.
StaySecure can scan the repository for risky patterns, dependency alerts, and exposed secrets, and explain the findings in plain language while you keep control of the patch. Use the scan as a backstop when you secure an AI-built app. Keep the two-user test as the launch gate.