Your first check is free. No card needed.Run a free security check

Is AI-generated code secure? What the studies found, and what to do about it

Not by default. In Veracode's 2025 test of over 100 models on 80 security-sensitive tasks, 45% of code samples failed its security checks, and a Stanford study found people using an AI assistant wrote less secure code while believing it was more secure. AI code is not uniquely bad; the risk is that nobody reads it, so the permissive defaults it picks ship unchanged.

Imagine you ask your AI coding tool for "an endpoint that updates the user's profile". It works on the first try. You click save, the name changes, you move on. Nobody reads the line that takes user_id from the request body instead of from the session, so any signed-in user can update anyone's profile by changing one number.

That is not an exotic bug. It is the most ordinary access mistake there is, and it is exactly what the research below keeps finding: code that works and has not been read.

What did the studies actually measure?

Each study below has a stated method. We opened and read each one; the numbers are theirs, not rounded into a headline.

StudyDateMethodMain result
Pearce et al., NYUIEEE S&P 202289 scenarios, 1,689 Copilot completionsAbout 40% vulnerable
Perry et al., StanfordACM CCS 202347 people, 5 tasks, with or without an assistantLess secure code, more confidence
BaxBenchFeb 2025392 backend tasks, tests run against the codeAbout half of correct programs failed a security test
VeracodeJul 2025, updated Nov 202580 tasks, 100+ models, static analysis45% of samples failed
EscapeOct 20255,600+ public apps, passive scan2,000+ vulnerabilities, 400+ exposed secrets

Asleep at the Keyboard? (NYU, 2021 preprint, IEEE S&P 2022). The authors wrote 89 scenarios around weaknesses on MITRE's 2021 CWE Top 25 and had GitHub Copilot complete them, producing 1,689 programs. They checked them with GitHub's CodeQL plus manual review and found "approximately 40%" vulnerable (arXiv). Limits: this was the Copilot of mid-2021, the prompts were built to invite a specific weakness, and one part tested hardware code in Verilog.

Do Users Write More Insecure Code with AI Assistants? (Stanford, ACM CCS 2023). 47 participants solved five security-related tasks in Python, JavaScript and C; 33 had an assistant built on OpenAI's codex-davinci-002 and 14 did not. The assistant group wrote less secure solutions on four of the five tasks. On signing a message, 3% of the assistant group wrote a secure answer against 21% of the control group. They were also more likely to believe their code was secure. Participants who trusted the AI less and reworded their prompts wrote fewer vulnerabilities (paper). Limits: a small sample, 66% undergraduates, short self-contained tasks, a 2022 model.

BaxBench (arXiv, February 2025, revised May 2025). 392 tasks asking models to write whole backend applications, each checked with functional tests and with real security tests run against the result. The best model, OpenAI o1, reached 62% on correctness, and around half of the correct programs from each model could be broken by one of the tests (arXiv). Limits: a benchmark of backends only, with early-2025 models.

Veracode GenAI Code Security Report (30 July 2025). 80 code-completion tasks with known potential for CWE weaknesses, given to over 100 models in Java, Python, C# and JavaScript and judged with Veracode Static Analysis (press release). 45% of samples failed. Java failed 72% of the time, Python 38%, JavaScript 43% and C# 45%. Security did not improve with model size, even though newer models wrote more correct code (Veracode blog). Cross-site scripting went unprotected in 86% of relevant cases and log injection in 88%. In the October 2025 rerun, GPT-5 Mini passed 72% of security tests and GPT-5 70%, while other providers' latest models stayed around 50 to 59% (November 2025 update). Limits: Veracode sells code scanning, static analysis only, and single tasks rather than whole apps.

Escape (29 October 2025, updated June 2026). Escape, which sells security testing, ran passive, non-destructive scans on over 5,600 publicly available apps built on vibe coding platforms, about 4,000 of them on Lovable, and kept only high-confidence findings: more than 2,000 vulnerabilities, over 400 exposed secrets and 175 instances of personal data (Escape methodology). Escape calls its results lower-bound estimates and lists sampling bias, a single snapshot in time and the skew toward one platform as limits. It measures deployed apps, so it cannot separate code the AI wrote from settings a person chose.

Is AI code worse than code people write?

Only one of these studies compares against people working without AI: Stanford's, and its sample is small. The others measure AI output on its own. SQL injection and cross-site scripting were common long before assistants; the CWE lists were built from human code.

So we would not say AI code is uniquely bad. The Stanford finding that matters most is the confidence gap: people with the assistant felt safer while writing weaker code. Add the speed at which a vibe-coded app grows, and you get large amounts of plausible code that no one has read. The Veracode and BaxBench results say a working answer is not a safe answer, and that being newer does not fix that by itself.

Which defaults tend to leak?

In apps built with AI tools, the mistakes that expose data are rarely clever. They are defaults that make the app work fastest:

  • Policies that allow everyone, so the first query succeeds.
  • Checks in the browser only: a hidden admin button with no check on the server.
  • Secrets in front-end code, because the call worked from the client.
  • Trusting ids sent by the client, like the profile endpoint in the opening.

Here is the first one in Supabase, next to the fix:

-- Works on the first try, and lets every visitor read every row
create policy "read profiles" on profiles for select using (true);

-- Each signed-in user reads only their own row
create policy "read own profile" on profiles for select
  to authenticated using ((select auth.uid()) = user_id);

Supabase's Security Advisor flags policies whose condition is always true (Supabase advisors). It cannot tell you that user_id is the right column; you can.

What should I ask the AI for?

Put your security requirements where the agent reads them on every request, so you are not relying on remembering to ask:

  • Claude Code reads a project ./CLAUDE.md or ./.claude/CLAUDE.md, and can read AGENTS.md (Claude Code docs).
  • Cursor reads project rules from .cursor/rules as .mdc files; a plain .md file there is ignored. It also supports AGENTS.md (Cursor docs).
  • Lovable has Instructions under Project settings, up to 10,000 characters, always included in context (Lovable docs).

Write rules you can check afterwards:

## Security rules
- Every route and server action gets the user from the session, never from the request body.
- Every query on user data filters by the signed-in user's id or checks ownership first.
- Every new table gets row level security and an owner-only policy.
- No secret key in client code or in a VITE_ or NEXT_PUBLIC_ variable.
- Validate input on the server; never build SQL by joining strings.

Claude Code's documentation says it treats CLAUDE.md as context, not enforced configuration, and that specific, concise instructions are followed more consistently. Rules lower the odds; they do not replace a check. Our Claude Code and Cursor checklist covers the rest of the repository side.

What should I review by hand first?

You do not need to read every line. Read the few places where a mistake exposes other people's data, in this order:

  1. Access checks. In every route that reads or changes a record, where does the user id come from, and is ownership checked? Our access control test shows what a failure looks like.
  2. Database policies. Read each policy as a sentence: who can do what to which rows. Anything with true or only "is signed in" needs a reason. See row-level security testing.
  3. Secrets. Search the client code and public variables for secret keys, and check git history. The exposed key guide has the order of steps if you find one.
  4. Input handling. Look for SQL built from strings, HTML rendered from user text, and file uploads that accept any type.

Then test the running app, because a reviewed line can still sit behind a wrong setting. The vibe-coded app checks take an afternoon with two test accounts.

Start today with one thing: open your three most-used routes and find the line where each gets the user's id.

Where does Saqr fit?

Saqr tests the running app rather than the code: on a Full task it signs in with test logins you provide and checks, among other things, whether one user can reach another's records or change a field such as role or price. Each finding comes with the request and response as proof, and a fix prompt to paste into your AI coding tool. Your first check is free, no card needed.

Saqr (usesaqr.com) is AI security for web apps. Its AI agents attack your live app from the outside, the way a real attacker would, and show you exactly what to fix. From $19 a month, cancel any time. Tests only apps you have shown are yours.

Questions

Do newer AI models write more secure code?

Not automatically. Veracode found security flat across model size in July 2025, though in its October 2025 rerun two GPT-5 models passed 70 to 72 percent of security tests against 50 to 59 percent for other providers' latest models.

Is it safe to ship code I did not write myself?

People ship libraries they did not write every day. The difference is that AI code was written for your app minutes ago and nobody has reviewed it, so read the access checks, policies and secret handling before launch.

Can I ask the AI to review its own code for security?

Yes, and it is worth doing, especially for a specific area such as sign-in or data access. Treat it as one more review of the code, then test the deployed app, which no code review can see.

Where do statistics like 40 to 62 percent of AI code being vulnerable come from?

Usually from several studies rounded together, with different models, tasks and methods. Check the original study, its date and what it measured before repeating a number.

Which programming language is safest with AI?

Veracode's 2025 test found Java failed its security checks most often, at 72 percent, with Python at 38 percent. That reflects its 80 tasks, not your app, so the checks above matter more than the language.