85 agentic tools audited for security incidents · Talk to the lab →
MotionticBook a call
Guides

· 6 min read

Is AI-generated code secure? What the research shows

What published studies say about AI generated code security, where those studies fall short, and a PR checklist for shipping AI-written code safely.

A dotted lens ring over a white code window highlights one tangerine line, linked to a lilac card and green check

AI-generated code is not secure by default. Peer-reviewed studies of AI generated code security found that assistants like GitHub Copilot regularly suggest code with known weakness patterns, and that developers using them trust that code more than they should. AI-written code becomes safe to ship only when it passes the same review and CI checks as human code.

What the research on AI generated code security shows

Four bodies of research shape what is known about AI code security, and they agree on the direction: assistants produce insecure code at a meaningful rate, and people miss it.

The NYU Copilot study

The NYU team behind Asleep at the Keyboard built prompts around MITRE's list of the most dangerous software weaknesses and asked GitHub Copilot to complete them. A large share of the completed programs were exploitable. The failures clustered in familiar places:

  • SQL injection from string-built queries.
  • Path traversal when handling file names.
  • Use of weak hashing for passwords.
  • Out-of-bounds reads and writes in C code.

The study showed that the model mirrors its training data. Public code is full of insecure examples, so the model suggests them with the same confidence as secure ones.

The Stanford user study

The Stanford study Do Users Write More Insecure Code with AI Assistants? looked at people, not models. Participants with access to an AI assistant wrote less secure code on most tasks than the control group. They were also more likely to rate their own insecure answers as secure.

That second finding matters more for a CTO. It means the reviewer, not only the model, is part of the failure.

Package hallucination research

A study of package hallucinations tested code-generating models on how often they import libraries that do not exist. Every model tested invented package names, and many of those names came back again and again across prompts. An attacker who registers a recurring name on npm or PyPI can ship malware to anyone who runs the install without checking.

The CSET policy report

Georgetown's Center for Security and Emerging Technology published Cybersecurity Risks of AI-Generated Code, which reviewed model output across several systems. It found that models frequently produced code with bugs that could lead to exploitation, and it framed the risk in three parts: insecure code from models, attacks on the models themselves, and downstream effects as AI code trains the next generation of models.

What the studies do not tell you

The published research has limits, and you should know them before quoting it in a board deck.

  1. Most studies tested older models. Newer models write fewer obvious injection bugs on simple prompts, so headline rates from 2021 or 2022 do not transfer cleanly.
  2. Tasks were short and synthetic. Real startup code involves multi-file changes, auth middleware and tenant boundaries that a benchmark prompt never touches.
  3. Logic flaws are under-measured. Scanners used to score the studies catch pattern bugs. A missing authorization check on a new endpoint looks fine to a scanner and is the flaw AI introduces most often in SaaS codebases.
  4. None of the early studies covered agents. Tools like Claude Code and Cursor's agent mode run commands, install packages and edit config, which adds a class of risk the completion-era studies never tested.

The direction of the findings still holds. Newer models fail less on the easy cases and keep failing on context the prompt did not include.

Why AI flaws slip past code review

AI flaws slip past review because the code reads as finished. It is formatted, named well and commented, which lowers the reviewer's guard exactly as the Stanford study predicts.

Three other forces stack on top:

  • Volume. AI lets one engineer open many more pull requests, and review capacity does not grow with it.
  • Missing context. The model never saw your permission model, so it writes a correct-looking route that skips the tenant check.
  • Tool blind spots. SAST finds known patterns but not missing logic. Dependency scanners flag known CVEs but not a package registered last week to match a hallucinated name.

Teams comparing assistants, for example in GitHub Copilot vs Claude Code, often focus on speed and model quality. The security outcome depends more on what happens after the code is written.

How to make AI-generated code secure: a PR checklist

Securing AI-generated code means running a fixed set of checks on every pull request before merge. Use this checklist as a starting point and make each item a blocking CI check where you can.

  1. Require at least one human approval, with senior review on auth, billing, data access and infrastructure paths.
  2. Label AI-authored PRs so reviewers read them as untrusted input.
  3. Run SAST on the diff and fail the build on high-severity findings.
  4. Verify every new dependency exists, has release history and has no known critical CVE. Block packages published in the last few days unless someone approves them.
  5. Turn on secret scanning with push protection so keys never reach Git history.
  6. Check every new route or handler for an authorization call and a tenant filter.
  7. Flag new outbound network calls, shell execution and dynamic code evaluation for manual review.
  8. Check how the code handles LLM output. Treat model output as untrusted input, the way the OWASP Top 10 for LLM Applications describes.
  9. Cap PR size, and ask the agent to split large changes.
  10. Keep a log of findings and fixes. Buyers will ask for it.

Write the rules your agents should follow into a shared file. Motiontic's glossary entry on golden principles for coding agents covers how to phrase those rules so agents follow them.

Running this checklist by hand on every PR does not scale past a few engineers. The Motiontic Security Layer (MSL) automates it inside the team's own CI: it runs on every pull request, scores risk against the ARI-42 risk index, and posts a PR comment listing each risk with its fix. Teams also get a dashboard and a buyer-ready PDF report for security reviews, with setup on a 15-minute call and the first report within 48 hours.

What this means for your security review

Enterprise buyers have read the same research and now ask how you control AI in your development process. A good answer has three parts: a written policy, proof that checks run on every change, and a record of what those checks found.

If you sell into regulated industries, map those controls to the frameworks your buyers use. Motiontic's page on SOC 2 and ISO compliant AI systems covers how that mapping works for AI-heavy teams.

FAQ

Is AI-generated code secure enough for production?

It can be, if it passes the same checks as human code. The research shows AI output carries known weakness patterns and that developers overtrust it, so unreviewed AI code is a risk. Blocking CI checks plus human review on sensitive paths close most of the gap.

Does SAST catch flaws in AI-generated code?

SAST catches pattern bugs like injection and weak crypto, which are part of the problem. It misses logic flaws such as missing authorization or tenant checks, and it does not detect hallucinated packages. Most teams pair SAST with dependency verification and AI-specific logic checks.

Are newer models safer than the ones in the studies?

Newer models avoid many of the simple bugs the 2021 and 2022 studies measured. They still lack your codebase's security context, so they keep producing correct-looking code that skips your permission rules. Treat model upgrades as an improvement, not a replacement for review.

Can you tell if code was written by AI?

Not reliably from the code alone. The practical approach is to label AI-assisted PRs in your workflow and review them with the checklist above, rather than trying to detect AI after the fact.

Who is responsible when AI-written code causes a breach?

Your company is. Buyers, auditors and regulators treat AI-written code as your code, which is why they ask for evidence of review and testing on every change.

Check your AI-written pull requests

Motiontic helps startups ship AI-written code that holds up in security review. Book a 20-minute call to see an ARI-42 report on your own pull requests.

Motiontic · Published

Running agents that have to hold up?

Describe the agent, the systems it touches and what it must never get wrong. The first call is with a lab engineer, who will say plainly whether it is ready for production, and whether Motiontic is the right team.