AI-Generated Code Security: Faster Code, Same Old Flaws
Your developers are already shipping code written with an AI assistant. The question is not whether to allow it. The question is whether that code goes through the same security gates as everything else, or whether "the AI wrote it" has quietly become a reason to look less closely.
The research so far points one way: AI assistants make developers faster, but they do not make the code safer by default. This guide summarizes what the studies found, the two risks that are specific to AI-generated code, and a practical program to keep the speed without inheriting the flaws.
Adoption is ahead of trust
In the 2025 Stack Overflow Developer Survey, 84% of respondents said they use or plan to use AI tools in their development process, up from 76% the year before. Trust has not kept pace: only 33% said they trust the accuracy of AI output, while 46% said they distrust it. The most common frustration, cited by 66%, was "AI solutions that are almost right, but not quite."
"Almost right" is exactly the problem for security. Code that compiles, passes the happy-path test and looks idiomatic is the code reviewers are most likely to approve without a second look.
What the studies found
Nearly half of the samples failed security tests
Veracode's 2025 GenAI Code Security Report tested more than 100 large language models on 80 curated coding tasks. In 45% of cases, the generated code introduced a security vulnerability. Some results stand out:
- Java was the riskiest language, with a security failure rate above 70%.
- Models failed to defend against cross-site scripting (CWE-80) in 86% of cases and against log injection (CWE-117) in 88%.
- Newer models wrote better syntax, but security performance stayed flat over time, and larger models did not perform significantly better than smaller ones.
The takeaway is not that one model is safe and another is not. It is that "pick a better model" is not a security control.
Assistants recommend packages that do not exist
The second risk is new. In a study presented at USENIX Security 2025 ("We Have a Package for You!"), researchers generated 576,000 Python and JavaScript code samples with 16 popular models. On average, 5.2% of the packages recommended by commercial models and 21.7% of those recommended by open-source models did not exist. They counted 205,474 unique hallucinated package names.
Worse, the hallucinations repeat. When the same prompts were run ten times, 43% of hallucinated names came back in all ten runs, and 58% came back more than once. A name that an assistant keeps inventing is a name an attacker can register, fill with malicious code and wait for. This technique is often called slopsquatting, and it is a close relative of the typosquatting covered in our guide to malicious open source packages.
Why AI code slips past review
Three things make AI-generated code easy to under-review:
- Volume. Pull requests get bigger when code is cheap to write, and review attention does not scale with line count.
- Plausibility. The code looks like code a competent developer would write. The missing output encoding or the unvalidated parameter is not visually obvious.
- Authority by default. A dependency suggested in an import statement looks like a deliberate choice, even when nobody actually chose it.
None of this is unique to AI. A copied snippet from a forum has the same problems. The difference is scale: assistants produce these snippets all day, for every developer.
A practical program for AI-assisted development
1. Treat AI output as untrusted third-party code
NIST's Secure Software Development Framework (SP 800-218) groups its practices into preparing the organization, protecting the software, producing well-secured software and responding to vulnerabilities. None of those practices depends on who, or what, typed the code. Write that down as policy: AI-generated code goes through the same gates as human code, with no exceptions for "it's just boilerplate."
2. Scan in the pull request, not after release
The gates that matter most for AI code are the ones that run before merge:
- SAST for injection, XSS, unsafe deserialization and missing validation, the flaw classes where models fail most often.
- SCA for every new dependency, with known vulnerabilities and reachability.
- Secrets detection, because example code with hardcoded keys gets pasted and accepted.
If you need a refresher on what each scanner sees, start with SAST, SCA, secrets and IaC explained. Then make the results a merge condition, not a dashboard someone checks later.
3. Verify that every new dependency is real and reputable
Hallucinated packages are a supply chain problem, so the controls live in the supply chain:
- Fail the build when a new dependency appears that did not exist in the lockfile, until someone approves it.
- Check age, maintainers and download history for new packages; a package registered last week with a name an assistant suggested deserves a hard look.
- Install through an internal registry proxy or allowlist, so a brand-new public name cannot reach production silently.
Our guide to securing your CI/CD pipeline covers where these controls fit.
4. Move the first check into the editor
The cheapest moment to catch a flaw is before it is committed. IDE extensions that run the same rules as the pipeline let developers fix the suggestion while they still remember why they accepted it.
5. Focus human review where models fail
Reviewers cannot read everything twice. Point their attention at the areas the research flags: input handling and output encoding, authentication and authorization checks, logging of user-controlled data, cryptography and new dependencies. A short checklist in the pull request template does more than a general reminder to "be careful with AI code."
6. Hold AI-generated fixes to the same standard
AI is also being used to fix vulnerabilities, not just to write features. That is useful, but the same rule applies: an automated fix should be verified by re-scanning and by tests, and it should arrive as a pull request a human approves, not as a direct commit.
Frequently asked questions
Should we ban AI coding assistants? Bans are hard to enforce and push usage out of sight. A clear policy with the same gates for all code is easier to defend and to audit.
Is a commercial model safe enough? Commercial models hallucinated fewer packages in the USENIX study, but not zero, and in Veracode's tests larger models were not significantly safer. Model choice reduces risk; it does not replace scanning.
Do we need to label AI-generated code? It helps for measurement, but the controls should not depend on the label. Most teams cannot reliably tell which lines came from an assistant.
How priorIQ.ai helps
CodeIQ runs SAST with cross-file taint analysis in 11 languages, SCA with reachability and malicious-package detection, secrets scanning across the full git history with validation of live secrets, and container and IaC scanning. Its pipeline gate and IDE extensions for VS Code, JetBrains and more apply the same rules in the editor and in the pull request, and its AI auto-fix is verified before it is opened as a pull request. Code findings flow into PriorIQ Core, so they are prioritized in the same queue as endpoint and cloud risk. Request a demo.