AISECURITYCODE REVIEW

Review AI-written code in 10 minutes: the checklist I use

AI tools write code that works on the first try and still lets a stranger read someone else's data. Here are the ten things I check before any AI-written change goes live, in the order I check them, and what each one looks like when it's wrong.

Ali Murtaza · 4 min read

I use AI to write code every day, and I'm not going back. But I've also watched it hand me a server action that worked perfectly in the demo and let any signed-in user delete anyone's invoice. The code wasn't broken. It did exactly what I asked. I just hadn't asked for the part that mattered.

AI is good at the happy path and quiet about everything around it. So before an AI-written change goes live, I spend ten minutes on the same checklist. It isn't a security audit. It's the handful of mistakes I've seen AI make again and again, and how to spot each one fast.

1. Secrets: where did the key go?

Search the change for anything that looks like a key, a token or a password. AI tools paste a key straight into the file when you give them one in the chat, and they'll happily put it in a variable starting with NEXT_PUBLIC_, which ships it to every visitor's browser.

  • Keys live in .env, and .env is in .gitignore.
  • Only values that are safe for anyone to see start with NEXT_PUBLIC_.
  • If a key was ever committed, rotate it. Deleting it from the file doesn't delete it from the history.

2. Who is asking?

Open every server action and API route in the change and find the line that checks the session. A server action is a public endpoint: anyone can call it, signed in or not, even if no button on your site points at it. AI often adds the button's check in the component and forgets the server.

3. What are they allowed to touch?

This is the one AI misses most. Signed in isn't the same as allowed. If the action takes an id, check that the query also matches the owner, like where: { id, userId: session.user.id }. Without it, changing the id in a request reaches someone else's row. Try it yourself: sign in as one user and send another user's id.

4. Is the input checked on the server?

Form fields with required and maxLength are for the person filling in the form, not for safety. Anything can be sent to your server. Look for a schema check (Zod or similar) at the top of the action, and for limits on length and type, not just presence.

5. Does any text end up as HTML?

Search for dangerouslySetInnerHTML, innerHTML and Markdown renderers. If text a user typed can reach one of them, it can run a script on someone else's page. React escapes what you render normally, so the risk is almost always one of these exits.

6. Does the server fetch a URL a user gave it?

Link previews, webhooks and "import from URL" features are favourites for AI to write in five lines. A server that fetches any URL can be pointed at your own internal network. Allow https only, and refuse private and local addresses, or don't fetch user URLs at all.

7. Did it add a package you don't know?

AI sometimes names a package that sounds right and doesn't exist, and people register those names to catch exactly this. Before you install anything new, run npm view <name> and check that it's the package you think it is: who publishes it, how many weekly downloads it has, and when it was last updated.

8. What happens when it fails?

Turn off your network or stop the database and try the feature. AI code often sends the raw error back to the browser, with a stack trace, a query or a file path in it. Log the details on the server, and show the person a sentence they can act on.

9. Do the tests test what you need?

When the same tool writes the code and the tests, the tests check what it built, not what you wanted. Write one test yourself from the requirement, especially the unhappy one: the wrong user, the empty list, the price of zero.

10. Can you explain every line?

Read the change top to bottom and say out loud what each part does. If there's a line you can't explain, you don't own that code yet, and you'll be the one fixing it at midnight. Ask the AI to explain it, check the explanation against the docs, or rewrite it in a way you understand.

The ten checks take me about ten minutes on a normal-sized change. The worst problems I've caught were all in checks 2 and 3.

If you want to see the server-side holes in a real app, with each one opened and closed step by step, read 8 security holes in a Next.js app, and how to close each one. It's the long version of checks 2 to 6.

THE NEWSLETTER

Get the next guide in your inbox

One email when a new guide or free build is out, every step tested before it’s published. No spam, unsubscribe anytime.

Found this useful? Give it a heart, or share it with someone who’d use it.
WRITTEN BY
Ali Murtaza

I build SaaS products with Next.js and teach them start to finish, in full courses here and free builds on YouTube.

KEEP READING
FREE BUILDS ON THIS STACK