AI-Generated Code Security: Risks and How to Mitigate Them
Copilot, Cursor, Claude Code, ChatGPT: your team today probably uses some generative AI to suggest lines of code, functions, and even entire system architectures of the systems you build in-house. Perceived productivity has likely gone up, but so has a risk that gets discussed far less.
The 2025 GenAI Code Security Report by Veracode tested more than 100 language models across 80 real coding tasks and arrived at a number that should be in every technology committee meeting: In 45% of the coding tasks tested, AI models introduced at least one known security flaw..
Sounds bad, right? But that rate varies a lot by language: in Java, the failure rate topped 70%, while in Python, C#, and JavaScript it sat between 38% and 45%. And the most uncomfortable finding in the research isn’t that gap, it’s that bigger, newer models haven’t been generating safer code than the older ones. The ability to write functional code has evolved; the ability to write secure code hasn’t.
This article isn’t a warning against using AI in development. We want to show that it’s possible to have a team that uses AI with speed and control, one that doesn’t pile up security debt in production.
Why AI-generated code often falls short on security
A language model doesn’t understand your business, your architecture, or your system’s threat context. It predicts the most probable sequence of code based on billions of lines from public repositories, and a good share of those repositories carry insecure patterns that have been copied, reproduced, and normalized over years.
Three factors explain most of the flaws found in the studies:
Inherited vulnerable patterns
The model learns from real code, and real code has SQL injection, weak authentication, and poorly implemented cryptography. The AI reproduces these patterns because they’re statistically common, not because they’re correct.
No architectural context
The AI doesn’t know whether that function will handle credit card data or an internal settings screen. It optimizes for “code that works,” not for “code appropriate to that component’s risk level.”
No built-in threat modeling
Security is an architecture decision, made before the first line of code. Generative AI comes in after that moment: it fills implementation gaps, it doesn’t replace system design.
Read also: Agility and Conversion: The Real Impact of AI Agents in Customer Interactions
The most common risks in AI-generated code
Some vulnerability patterns show up with a frequency that can now be considered structural in current models, not a one-off problem with a specific vendor.
Classic OWASP Top 10 flaws
The Veracode research identified especially high failure rates in categories like Cross-Site Scripting (CWE-80) and Log Injection (CWE-117), plus recurring SQL Injection (CWE-89) and flaws in cryptographic implementations (CWE-327). These are vulnerabilities the security market has known for decades; the problem isn’t that the flaw is new, it’s the scale at which it keeps reappearing.
Dependencies and packages that don’t exist
A phenomenon dubbed slopsquatting shows another face of the risk: AI models frequently recommend package names that simply don’t exist. A study of 16 models generated 576,000 code samples in Python and JavaScript and identified 205,474 unique hallucinated package names. The average rate of hallucinated packages ranged from 5.2% in commercial models to 21.7% in open-source models. Attackers register these names in public repositories like npm and PyPI before anyone notices. The developer doesn’t mistype a package, they trust a suggestion the AI itself invented.
Exposed credentials and secrets
In the sample analyzed by GitGuardian, commits assisted by Claude Code exposed secrets at a rate of 3.2%, against a baseline of 1.5% in public commits on GitHub.
Volume that outpaces review capacity
According to Snyk, more than half of tech professionals (56.4%) acknowledge that insecure AI suggestions are common day to day, but few teams have changed their review processes because of it, and a Snyk survey revealed the most critical point in this whole discussion: more than 75% of developers believe AI-generated code is more secure than human-written code, even in the face of evidence to the contrary. Fewer than 25% use software composition analysis (SCA) tools to validate these suggestions before accepting them.
That’s the core of the risk: it isn’t AI that creates the greatest exposure. It’s the disproportionate trust of teams that speed up delivery without adjusting the bar for review.
How to mitigate the risks without giving up speed
Banning generative AI from the development team isn’t a realistic strategy, and it’s probably no longer an option, given that a good share of developers already use these tools, whether the company has formalized it or not. The right question isn’t “use it or not"; it’s how to structure governance around that use.
1. Critical code review
AI-generated code needs to pass the same, or greater, level of scrutiny as hand-written code. Automated syntax review doesn’t replace human review of business logic and attack surface.
2. SAST and SCA integrated into the flow
Static analysis and software composition analysis tools need to run at the moment the code is generated, inside the IDE and in the pull request, not just in a CI pipeline no one stops to look at.
3. Threat modeling before the first AI suggestion
Secure architecture starts in the system design. Defining, before coding, which components handle sensitive data, authentication, and financial transactions directs where human oversight needs to be strictest.
4. Clear AI usage policies by criticality
Not all code carries the same risk. Boilerplate, tests, and low-impact refactoring can allow the AI more autonomy. Authentication, cryptography, and data-access components require mandatory review, no exceptions.
5. Dependency validation before the merge
With the risk of nonexistent packages recommended by AI, checking the existence, reputation, and history of each new dependency is no longer a best practice: it’s a prerequisite.
6. Traceability of what AI generated
Knowing which files, commits, and pull requests involved generative AI enables targeted audits and comparison of vulnerability rates between human and AI-assisted code.
None of these points requires abandoning AI. It requires treating it as part of an engineering process with defined technical responsibility, exactly the kind of structure that separates companies that scale securely from those that quietly accumulate risk until the incident shows up.
Mouts IT’s role in this scenario
Mouts IT has worked with automation and artificial intelligence applied to critical systems for years, and the company’s position in this debate is direct: AI in software development isn’t the problem, development without governance is. Digital transformation and process automation projects run by Mouts IT combine delivery speed with secure architecture from the ground up, structured code review, and technical responsibility across every layer of the system, from API design to data governance.
For IT leaders who have to decide where and how to accelerate AI use in development without turning that decision into technical liability, this is exactly the kind of diagnosis that keeps today’s speed gain from becoming next quarter’s security incident.
Talk to a Mouts IT specialist and find out how to apply AI in development with secure architecture from the ground up.
Related news
Innovation, the global market and divers topics about the technology universe are currently available on our blog.
Cases
Big Data in energy: how to manage data in the energy sector?
The energy sector is one of the fundamental pillars of the global economy, driving everything from large industries to domestic consumption. With the advancement of digital technologies, the amount of data generated in this sector has increased exponentially, making Big Data an essential tool for companies seeking to stand out in a competitive market. But how to collect and analyze this data effectively? And what are the benefits of a well-done analysis?
Cases
Energy and sustainability: 3 innovative technologies in the energy sector
Sustainability has become a central issue in the energy sector, driven by the need to reduce environmental impact, ensure energy security and meet growing demands for cleaner energy sources. With increasing pressure to reduce carbon emissions and adopt more sustainable practices, the sector faces significant challenges, but also promising opportunities.
Cases
Why is cloud migration important for the energy sector?
Energy market professionals can already see that the sector is undergoing a significant transformation, driven by the need for greater efficiency, sustainability, and innovation. Amid these changes, cloud migration has stood out as one of the main strategies for companies looking to optimize their operations, reduce costs and improve collaboration.
