Explore our new Cybersecurity Terms Glossary, explained by our experts.

View the Glossary View the Glossary
PRIVIA

Cybersecurity Terms Glossary

Is AI-Generated Code Secure?

AI coding assistants produce working code quickly and insecure code just as quickly. The measured defect rates, and the controls that catch them, matter more than the debate.

Illustration for the cybersecurity term AI-Generated Code
Home / Blog / Cybersecurity Terms / Is AI-Generated Code Secure?
17 November 2026

Is AI-Generated Code Secure?

Is AI-Generated Code Secure?

AI coding assistants write code that compiles, passes the obvious tests, and looks like something a competent developer produced. Whether it is secure is a separate question, and the published evidence is consistent: a meaningful proportion of generated code contains recognised vulnerability patterns, and developers using assistants are more likely to believe their insecure code is safe.

What the Research Shows

The first systematic look came from New York University in 2021, where researchers generated code for scenarios drawn from the MITRE CWE list and found that around 40% of the completions in security-relevant contexts contained a vulnerability. A Stanford study published in 2023 ran a controlled experiment: participants with an AI assistant wrote less secure solutions to several tasks than those without one, and rated their own answers as more secure. Veracode’s analysis of generated code published in 2025 reported that roughly 45% of samples introduced a weakness from the OWASP Top 10, with cross-site scripting and log injection among the worst-performing categories. The numbers vary with model and prompt, but the direction does not.

Why the Output Looks Right and Is Not

Models are trained on public repositories, including a great deal of tutorial code written to be short rather than safe, and on examples that were secure against the threats of their era. Several failure patterns recur:

Hallucinated Dependencies

A distinct and more exploitable problem is the package that does not exist. Assistants regularly import libraries they have invented, and the same invented names recur across runs rather than appearing at random. Attackers register those names on public registries and wait, a technique the Python ecosystem named slopsquatting in 2025 after research measured how often recommended packages were non-existent. The defence is mechanical rather than educational: resolve all dependencies through an internal proxy or allow-list, fail the build on an unknown package, and review every new dependency in a pull request as a change of its own.

Reviewing Generated Code

Treat assistant output as a contribution from an unknown external developer who is fast, confident, and has not read your threat model. That framing produces the right controls. Require human review of every generated change, with the reviewer told which parts were generated, because people skim code that looks finished. Run static analysis and software composition analysis in the pipeline and block on high-severity findings rather than reporting them. Keep secret scanning on the pre-commit hook as well as the pipeline. Add security-relevant tests—authorisation checks, input boundaries, error paths—since generated unit tests tend to cover the happy path the generator had in mind. The OWASP Top 10 remains the working checklist, and our note on secure software development covers the surrounding process.

Governance, Licensing, and UK Obligations

Two non-security risks travel with the code. Licensing exposure arises when a model reproduces substantial portions of training data under a copyleft licence, which is why enterprise tiers offer filtering and indemnities worth reading carefully. Confidentiality exposure arises when proprietary code is sent to a hosted service; check whether the provider trains on submissions and whether an enterprise tenancy with retention controls is available. Where generated code processes personal data, UK GDPR’s security obligation under Article 32 applies exactly as it does to hand-written code, and the ICO will not distinguish between the two after a breach. Set the rules in your AI usage policy and verify them in the pipeline. At Privia Security, our Web Application Security Services test the applications that result, whoever or whatever wrote them.

FAQ – Frequently Asked Questions

Should We Stop Using AI Coding Assistants?

Most teams should not, because the productivity gain is real and the risks are manageable with pipeline controls. The failure mode is adopting the assistant without changing review practice. If every generated change goes through human review, static analysis, dependency allow-listing, and secret scanning, the residual risk is comparable to code written quickly by a junior developer.

Does AI-Generated Code Need a Different Kind of Testing?

The techniques are the same, but the emphasis shifts. Prioritise authorisation logic, input handling, and cryptographic choices, because those are where generated code fails most often. Pay particular attention to new dependencies, and avoid relying on tests the same assistant wrote, since they tend to encode the generator’s assumptions rather than challenge them.

Can an Assistant Introduce a Backdoor?

Deliberately, not on its own. The realistic routes are indirect: a hallucinated package name that an attacker has registered, or instructions hidden in a repository file or issue that a coding agent reads and acts on. Both are addressed by controlling dependency resolution and by treating repository content an agent processes as untrusted input.

Who Is Responsible for a Vulnerability in Generated Code?

The organisation that shipped it. No current provider accepts liability for security defects in output, and UK GDPR, PCI DSS, and contractual security obligations all attach to the deploying organisation. In practice this means your definition of done, your review gates, and your evidence of testing need to cover generated code explicitly.

Related Terms

You May Be Interested In These

What Is mTLS?

Cybersecurity Terms

17 November 2026

What Is mTLS?

Read More
What Is Defence Evasion?

Cybersecurity Terms

17 November 2026

What Is Defence Evasion?

Read More