Matte black banner with a cyan outline of an open empty parcel box, representing a software package that does not exist

Is AI-Generated Code Safe? What the Research Shows

In 2022, Stanford researchers ran a study that should have ended the argument about AI coding assistants. Developers given an AI assistant wrote less secure code than developers without one. That part got the headlines. The finding underneath it is the one that still matters, and it has nothing to do with how good the model was: the developers using the AI were more confident their code was secure. They were more wrong and more sure at the same time.

That’s an uncomfortable combination, because confidence is what decides whether anybody checks. This guide walks through what the research actually found, what has genuinely changed since those models, and a newer problem that doesn’t depend on code quality at all — where an AI invents a software package that doesn’t exist, an attacker registers the name, and your install command does the rest.

1. What the research actually found

The study is “Do Users Write More Insecure Code with AI Assistants?” by Perry, Srivastava, Kumar and Boneh at Stanford. They gave 47 participants five security-relevant programming tasks across Python, JavaScript and C. Thirty-three had access to an AI assistant; fourteen did not.

The group with the assistant produced less secure solutions on four of the five tasks. On encryption, 36% of the AI group’s answers were insecure, against 7% of the control group. On the task of adding an entry to a SQL table, SQL injection vulnerabilities appeared in 36% of AI-assisted answers versus 7% without. On message signing, only 3% of the AI group got a secure solution, compared with 21% of the control.

Those are not small gaps. But before you take them as the verdict on AI coding tools in 2026, note the date: the study ran in late 2022, on the models of that moment. Anyone telling you this settles the question today is overreaching, and we’ll come back to that.

Insecure solutions, by task Stanford, 47 participants, 2022 models Encryption 36% with AI 7% without SQL injection 36% with AI 7% without Source: Perry, Srivastava, Kumar & Boneh, arXiv:2211.03622

2. The confidence problem

Here is the result that doesn’t age the way the vulnerability numbers do. Participants with the AI assistant were more likely to believe they had written secure code — while writing more insecure code. The paper also found an inverse relationship worth sitting with: participants who trusted the AI less produced more secure solutions, and those who engaged more carefully with their prompts did better than those who accepted what came back.

The risk was never that the machine writes bad code. It’s that confident-looking code stops getting read.

This is a familiar shape if you’ve spent time around security incidents. The control that fails is rarely the technical one; it’s the human step that quietly stopped happening because everything looked fine. Code you wrote yourself at 2am carries a felt sense of doubt — you know which bit you rushed. Code that arrives instantly, correctly indented, with a plausible comment above it, carries no such signal. It looks reviewed before anyone has reviewed it.

And notice what that does to the economics of review. If an assistant makes you four times faster at producing code but your review capacity stays the same, review is now the bottleneck — and bottlenecks are where corners get cut.

3. What’s changed since then (and what hasn’t)

Honesty matters more than a clean narrative here, so: the models in that study are obsolete. Today’s coding models are substantially better at exactly the things the study measured. If someone reran it now, the raw vulnerability rates would almost certainly be lower. Anyone citing those 2022 percentages as current fact — including people who agree with the conclusion — is misusing them.

What hasn’t obviously changed is the human half. The overconfidence finding isn’t a statement about model quality; it’s a statement about what fluent output does to a reader’s judgment. A better model arguably makes that worse, not better, because the output is more plausible and the errors that remain are subtler. A snippet that’s wrong in an obvious way gets caught. A snippet that’s wrong in one line out of thirty, surrounded by twenty-nine good ones, is the harder problem.

So treat the specific numbers as a historical measurement and the behavioural finding as the thing to carry forward. Which is also why the next section matters: it’s from 2025, and it doesn’t depend on code quality at all.

4. The package that doesn’t exist

Modern code barely gets written from scratch. You import libraries, and an AI assistant will happily tell you which ones to import. Sometimes it names a library that has never existed.

Researchers presenting at the USENIX Security Symposium in 2025 tested 16 code-generating models, produced 576,000 Python and JavaScript code samples, and counted how often the generated code referenced a package that isn’t real. They found 205,474 unique non-existent package names. The published paper reports hallucinated packages in at least 5.2% of cases for commercial models and 21.7% for open-source ones.

On its own that’s just an annoyance — you run the install, it fails, you move on. The problem is what a determined attacker can do with it. Because the invented names are not random noise. When the researchers re-ran the same prompt ten times, a hallucinated package name came back more than once in 58% of cases, and 43% of them appeared in all ten runs. The paper notes an attacker could find the persistent ones simply by analysing a large volume of model output over time.

5. Why a model’s mistake becomes an attack

Put those two facts together and the attack writes itself. An attacker asks a model for code thousands of times, collects the package names it invents, keeps the ones it invents reliably, and registers those names on the public package registry with malicious code inside. Then they wait. The next developer who asks a similar question gets the same invented name — except now it resolves. The install succeeds. The dependency is real, and it is hostile.

How an invented name becomes a real dependency Prompt the model thousands of times Keep the names it repeats Register them with malware inside Wait for the install The name the model invents for the attacker is the same name it invents for you. Source: Spracklen et al., USENIX Security 2025

The security world has started calling this slopsquatting, a name coined by a security developer at the Python Software Foundation. The academic term is a package confusion attack, and it is a close cousin of typosquatting — registering a near-miss spelling of a popular library and waiting for someone to fat-finger it. The difference is who makes the mistake. Typosquatting needs a human to mistype. This needs a machine to misremember, and machines misremember far more consistently than humans mistype.

That consistency is the whole problem. A human typo is unpredictable. A model’s invented package name is reproducible, which makes it targetable. It’s the same structural pattern behind prompt injection: a property of how these systems work, turned into an attack surface by someone who thought about it for longer than the people deploying them.

6. Why reviewing AI code is harder than writing it

There’s an asymmetry nobody warns you about. When you write code, you build a mental model as you go — you know which assumptions you made, which edge case you deferred, which line you’re unsure about. When you accept generated code, you get the code without the mental model. Reviewing it properly means reconstructing reasoning that was never yours.

That’s genuinely harder work than writing it, and it’s the step most likely to be skipped, because the whole appeal of the tool was speed. Nobody adopts an assistant to go slower.

It matters more as these tools gain the ability to act rather than suggest — installing dependencies, running commands, editing files across a repository. At that point an invented package name isn’t a suggestion you can ignore; it’s an install that already happened. We covered that broader shift in why AI agents are a new security risk, and dependency installation is one of the clearest examples of why “the human reviews it” stops being a real control the moment the human is no longer in the loop.

7. What to actually look for in review

“Review it carefully” is useless advice without a list of what tends to go wrong. The Stanford tasks are a reasonable guide here, because the places the assistant performed worst are the places it still has the least to go on.

Cryptography with plausible defaults. This was the worst-performing task in the study, and the reason is structural rather than incidental. Encryption code has a property that makes it uniquely dangerous to generate: wrong implementations run perfectly. There’s no error, no failed test, no crash — the data encrypts, it decrypts, everything looks right, and the mode of operation or the key handling is quietly unsuitable. Working code and correct code are the same thing in most of programming. In cryptography they aren’t, and that gap is invisible to the only check most people actually run, which is “did it work?”

Queries built by joining strings. SQL injection was the second big gap, and it’s easy to spot once you’re looking: if a query is assembled by concatenating or interpolating a variable rather than passed as a parameter, that’s the bug. It’s one of the oldest vulnerability classes in existence, which is exactly why it appears — there’s an enormous amount of old, vulnerable example code in the world for a model to have learned the shape of.

Permissions and paths that are wider than they need to be. Generated code tends toward whatever makes the example work, which often means broader file access, looser validation or more permissive settings than your actual case needs. It isn’t wrong so much as unconstrained, and nothing in the output signals that a narrower version was available.

Error handling that explains too much. Helpful messages that return stack traces or internal details are good for debugging and useful to an attacker mapping your system. Generated code is written to be demonstrative, and demonstrative code explains itself.

Any dependency you didn’t go and look up. Covered above, but it belongs on the review list rather than in a separate mental category, because it’s the one item here that can compromise you without any flaw in the surrounding code at all.

What these share is that none of them break anything. Every one produces software that runs, passes a casual test and looks finished — which is precisely why the confidence effect from section 2 is the thing doing the damage. The failures that get caught are the ones that announce themselves, and none of these do.

8. How to use these tools without the risk

None of this is an argument for not using AI coding assistants. The productivity gain is real, and “don’t use the useful thing” has never been advice anyone follows. The useful version is narrower.

Verify every package before you install it. This is the highest-value habit in the whole list, and it takes seconds. Does it exist on the registry? How old is it? How many downloads, how many maintainers, does it have a repository with history? A package that appeared three weeks ago with no history and a name that sounds exactly right deserves suspicion precisely because it sounds exactly right.

Treat generated code as a draft from a stranger. Not as a mistake, not as finished work — as a confident first attempt by someone whose reasoning you can’t inspect. That framing produces roughly the right amount of scepticism without making the tool useless.

Be most careful where the model is most convincing. Security-sensitive code — authentication, cryptography, input handling, permissions — is where fluent-sounding wrong answers do the most damage, and where the Stanford study found the biggest gaps. If you wouldn’t be able to explain why the generated approach is correct, you aren’t yet in a position to accept it.

Keep the automated checks, and don’t let speed outrun them. Dependency scanning, lockfiles, static analysis and code review all still work on AI-generated code — they don’t care who wrote it. The failure mode isn’t that these controls stop working; it’s that the volume of code going through them quietly triples.

Decide what your tools are allowed to do on their own. If an assistant can install packages or run commands without a human approving each one, the review step you were relying on doesn’t exist. That’s a configuration decision, and it’s worth making deliberately rather than by default. The same judgment applies to what you paste into these tools, which we covered in using AI chatbots at work.

9. Frequently asked questions

Is AI-generated code less secure than human-written code?

The best-known study, from Stanford in 2022, found that developers with an AI assistant wrote insecure solutions more often on four of five security tasks. But those were 2022 models and today’s are considerably better, so the specific percentages shouldn’t be quoted as current. The durable finding is about people rather than models: the same participants were more confident their code was secure while being more likely to be wrong.

What is slopsquatting?

It’s when an attacker registers a software package under a name that AI models invent. Research presented at USENIX Security 2025 found 205,474 unique non-existent package names across 576,000 generated code samples, and that the same invented names recur reliably — 43% reappeared in all ten re-runs of a prompt. An attacker publishes malware under a name the model keeps suggesting, and waits for a developer to install it.

How do I check whether a package the AI suggested is real?

Look it up on the registry before installing. Check the publication date, download counts, maintainers and whether there’s a source repository with genuine history. Be especially careful with anything newly published, thinly documented, or with a name that sounds like an obvious fit for your problem — that last one is exactly what a slopsquatted package is designed to look like.

Does this mean I shouldn’t use AI coding assistants?

No. It means the review step matters more, not less, and that you should be most sceptical in security-sensitive code. The Stanford study found that participants who trusted the AI less, and who worked harder at their prompts, produced more secure results — which suggests the tool isn’t the variable so much as how it’s used.

Do automated security tools catch AI-generated vulnerabilities?

Static analysis, dependency scanning and code review work the same way regardless of who or what wrote the code. The practical risk is volume: if generated code arrives several times faster than before, the review and scanning capacity has to keep up, or things start going through unchecked.

Security, Decoded.

If this is the kind of thinking you want more of, the newsletter does one security idea a week, decoded clearly. Free.

Subscribe to SKB Decoded →
References Neil Perry, Megha Srivastava, Deepak Kumar and Dan Boneh, “Do Users Write More Insecure Code with AI Assistants?” (arXiv:2211.03622; study conducted 2022) — 47 analysed participants, 33 with an AI assistant and 14 control, across five security-relevant tasks in Python, JavaScript and C; insecure encryption solutions in 36% of AI-assisted answers versus 7% of control; SQL injection in 36% versus 7%; secure message-signing solutions in 3% versus 21%; secure sandboxed-directory solutions in 12% versus 29%; and the finding that participants with access to an AI assistant “were more likely to believe that they wrote secure code than those without access,” alongside an inverse relationship between trust in the AI and security of the result. · Joseph Spracklen, Raveen Wijewickrama, A H M Nazmus Sakib, Anindya Maiti, Bimal Viswanath and Murtuza Jadliwala, “We Have a Package for You! A Comprehensive Analysis of Package Hallucinations by Code Generating LLMs,” 34th USENIX Security Symposium, August 2025 — 16 code-generating models, 576,000 Python and JavaScript code samples, 205,474 unique hallucinated package names, hallucinated packages in at least 5.2% of cases for commercial models and 21.7% for open-source models, a hallucinated name repeating more than once across ten re-runs in 58% of cases with 43% repeating in all ten, 81% of hallucinated names generated by only one model, and the description of a package confusion attack in which an adversary publishes malicious code under a hallucinated name after identifying persistent ones by analysing model output over time. The term “slopsquatting” was coined separately by Seth Michael Larson, security developer-in-residence at the Python Software Foundation.

Leave a Reply

Your email address will not be published. Required fields are marked *