Why AI-Generated Code Is Insecure: Four Root Causes
AI coding tools like Cursor, Lovable, Bolt, and Replit are changing how software gets built. Solo founders ship full-stack apps in days, and people who have never written a line of code ship SaaS products. The code works. Whether it is safe is a separate question, and the answer is often no — not at random, but for a small number of structural reasons that are worth understanding, because each one tells you where to look.
If you want the list of specific bugs and a grep for each, that is our practical checklist. This post is about why those bugs appear in the first place.
What the research says
Research from Stanford found that developers using AI assistants produce significantly less secure code than those who don't. A 2025 Veracode study found that 45% of AI-generated code samples failed its security tests (80 coding tasks across more than 100 models). These aren't edge cases — they're common patterns that show up again and again in AI-generated code.
1. The prompt names an outcome, not a boundary
“Add a Stripe webhook.” “Let admins delete users.” “Send people back where they were after login.” Each prompt describes something that should happen. None of them says who must not be able to make it happen, and that is the half security lives in. The model writes the shortest code that produces the outcome, the demo works, and the boundary was never part of the request.
That one gap accounts for most of what scanners find in generated code: a webhook that never checks it came from Stripe (VC005), a route that never asks who is calling (VC003), a redirect that follows any URL it is handed (VC016). The fix is equally structural: put the boundary in the prompt. “…and reject requests without a valid signature” produces the safe version; leaving it out produces the other one.
2. The model sees the file, not the system
Access control in a real app is rarely in the file being edited. It is in middleware mounted somewhere else, in database policies, in a hosting platform's settings. An assistant working on a component or a route handler sees that file and maybe a few neighbours, so it does what is locally sensible: hide the admin button from non-admins, or add one more handler shaped like the last one.
Hiding a button is presentation, not authorization (VC010): the request behind it still works for anyone who sends it. And the new handler does not inherit the auth check its neighbours have, because the check lives in a file the model never opened. In apps from AI app builders the gap is wider still, because the real access control is configuration rather than code — Supabase row-level security for a typical Lovable app, entity permissions in the hosted backend for Base44.
3. The goal becomes “make the error go away”
A large share of generated code is written in response to an error message, and the fastest way to remove an error is often to remove the check that raised it:
- A CORS error in the console becomes
origin: truewith credentials on (VC009). - An API key that reads as
undefinedin the browser gets aNEXT_PUBLIC_orVITE_prefix, which ships it to every visitor (the full story). - React escaping the HTML in a bio becomes
dangerouslySetInnerHTML(VC007).
In each case the error was the security control doing its job. The assistant was asked to make the app work, and it did.
4. The codebase teaches its own bad habits
Models complete code in the style of the code around it. Once a project has one key written inline in a client config, the next integration is most easily written the same way. Once one catch block returns error.message to the caller, every handler generated after it does too. A mistake made once in a generated project tends to be repeated across the codebase, which is why a single scan finding is worth reading as “where else does this pattern live?” rather than as one line to patch.
Builders and assistants fail in different places
The four causes apply everywhere, but where they bite depends on the tool. App builders such as Lovable, Bolt, v0 and Base44 generate whole projects, often client-first on a hosted backend, so the risk concentrates in what ships to the browser and in backend settings no one prompted for. Our per-builder pages break down what repeats in each, from the Vibe Security Index.
Editor assistants such as Cursor work inside an existing codebase, so the risk is in the seams: new handlers that skip the conventions the rest of the app follows, and quick fixes to errors. That is also where a check is easiest to add, because the assistant can run one itself through an MCP server.
Why “make it secure” is not a prompt
Asking for “secure code” rarely helps, because it names no boundary. You may get a comment like // TODO: add authentication above a route that still has none. Specific requirements work far better: “return 401 before doing any work if there is no signed-in user,” “verify the webhook signature against the raw body,” “redirect only to paths on this site.” A model that will not volunteer a check will usually write it when the check is the thing you asked for.
What to do with this
- Name the boundary in every prompt that creates an endpoint, a webhook, a redirect or a data query.
- Review the boundaries, not every line. Who can call this, what can they read, where can they send a user? Those three questions cover most of the four causes above.
- Treat a finding as a pattern. Search for its siblings before you fix the one in front of you.
- Check automatically. Run
npx xploitscan@latest scan .locally (30 rules free, 223 on a paid plan), or on every pull request with the GitHub Action.
See which of these are in your code
Paste a file or point the CLI at your repo. Free to start.
Scan Now — Free