Skip to content

Security Research

AI-generated code is vulnerable

A comprehensive benchmark of security vulnerabilities across five leading AI coding models, and how context-aware rules reduce exploitation risk by 95%.

Executive summary

AI coding assistants have fundamentally changed how software is built. Millions of developers now rely on models like Claude, Codex, Copilot, Gemini, and Cursor to generate code at unprecedented speed. But the same models writing your features are now writing the exploits used against them. In the past twelve months, attackers have used commercial AI coding tools to exfiltrate over 150GB of government data, automate up to 90% of multi-stage intrusion workflows, and weaponize leaked model source code into self-propagating malware. AI-driven offense is no longer a research demo. It is a production threat hitting banks, energy grids, and software supply chains today.

The defensive side of this equation is even more fragile. Our research reveals that between 68% and 76% of AI-generated code snippets across major models contain at least one exploitable security vulnerability, from SQL injection and cross-site scripting to secrets exposure and broken access control. These are not theoretical risks. They are production-ready vulnerabilities being shipped into applications every day, at machine speed, by the same systems an attacker can now point at your codebase.

To quantify the impact of context-aware security intervention, we conducted an extensive benchmark across five leading AI coding models, testing thousands of code generation tasks spanning seven major frameworks and the full OWASP Top 10 vulnerability taxonomy. The results are unambiguous: when Auditor’s stack-specific security rules are injected into the AI’s context at generation time, vulnerability rates drop to between 3% and 6%, an average reduction of approximately 95%. This is not incremental improvement. It is a categorical shift in the security posture of AI-generated code.

The implications are direct. You cannot out-review an attacker who generates exploits at machine speed using post-hoc tooling designed for human-written code. The only response that scales is defense applied at the same layer the offense operates on: at generation time, in context, before a single insecure line is shipped. This research demonstrates that context-aware, prevention-first security integration is both technically feasible and remarkably effective, and that it is now the minimum viable posture for any team building with AI.

95%

Average vulnerability reduction

71.6%

Baseline vulnerability rate

2,400+

Prompts tested

Exploitation risk benchmarking

Impact measured across thousands of AI-generated code reviews.

Risk of exploitation in AI-generated code, before and after Auditor

Without Auditor
With Auditor

The problem: AI-generated code and security

Large language models are trained to be helpful. When a developer asks an AI assistant to build an authentication endpoint, generate a database query, or set up a REST API, the model optimizes for the most direct path to functional code. It produces something that works: code that compiles, that runs, that returns the expected result. What it does not optimize for, in the absence of explicit instruction, is security. There is no intrinsic mechanism in standard LLM inference that weighs whether a generated SQL query is parameterized, whether an API key is hardcoded, or whether CORS headers are properly restricted. Functionality and security are distinct objectives, and the models consistently prioritize the former.

The problem is compounded by training data. These models learn from billions of lines of open-source code, Stack Overflow answers, tutorials, blog posts, and documentation. A significant fraction of that training corpus contains insecure patterns. Tutorials routinely demonstrate database queries with string concatenation for simplicity. Sample applications ship with wildcard CORS configurations. Example code hardcodes API keys for readability. The models internalize these patterns not as anti-patterns but as common solutions, and they reproduce them faithfully. The most statistically likely code completion is frequently the least secure one.

Perhaps most critically, AI coding assistants operate without security context. They do not know that the application they are generating code for handles medical records, processes payments, or is exposed to the public internet. They do not know whether the project uses an ORM that provides built-in parameterization or a raw database driver that requires manual escaping. They do not know the organization’s security policies, the regulatory requirements in play, or the threat model of the system. Without this context, the model cannot make informed security decisions, and so it makes none at all.

The result is a systemic and scalable vulnerability pipeline. Every developer using an AI coding assistant without security guardrails is, on average, introducing vulnerabilities into roughly seven out of every ten code generation interactions. Multiply this across engineering teams, across organizations, across the industry, and the aggregate security exposure is staggering. The velocity that makes AI code generation so valuable is the same velocity that makes its security failures so dangerous.

The threat is no longer theoretical

When this research was first scoped, AI-assisted attacks were a hypothetical concern raised in red-team papers and conference talks. That window has closed. In the past year alone, attackers have used commercial AI coding models to exfiltrate over 150GB of government data, automate up to 90% of multi-stage intrusion workflows, and weaponize leaked model source code into self-propagating malware. State-aligned groups have been observed running AI-driven reconnaissance against banks, energy grids, and software supply chains.

The asymmetry is the point. A single attacker with an AI agent can now probe more endpoints, draft more exploits, and adapt faster than entire defensive teams. The economics of offense have collapsed; the economics of defense have not. Patching after the fact, scanning after the merge, and reviewing after the deploy are all post-hoc strategies in a world where the offensive side is already operating at generation time.

This is the central insight behind Auditor. The only response that scales against AI-generated attacks is AI-generated defense, applied at the same layer, in the same loop, and with the same speed. You cannot detect your way out of a problem that is being created faster than you can read the diff.

Methodology

Our benchmark evaluated five leading AI coding models (Claude Opus 4.6, Codex 5.1, GitHub Copilot, Gemini 2.0, and Cursor) across a comprehensive suite of code generation tasks designed to surface security vulnerabilities. We constructed over 2,400 distinct prompts spanning seven major web and backend frameworks: React, Next.js, Express.js, Django, FastAPI, Spring Boot, and Ruby on Rails. These prompts were drawn from real-world development scenarios, including building authentication systems, constructing API endpoints, implementing search functionality, setting up database models, configuring server middleware, and handling user input. Each represents a context where security-critical decisions are made at the code level.

Each prompt was executed twice per model: once in a baseline configuration with no security guidance, and once with Auditor’s context-aware security rules injected into the model’s system prompt and conversation context. Auditor’s rules were generated dynamically based on the detected framework and language of each task, mirroring the tool’s real-world behavior where it analyzes a project’s dependency manifest and technology stack to produce tailored, prioritized security instructions. This ensures the comparison reflects actual usage conditions rather than a contrived or artificially optimized scenario.

Generated code was evaluated against the OWASP Top 10 vulnerability categories, including injection flaws, broken authentication, sensitive data exposure, XML external entities, broken access control, security misconfiguration, cross-site scripting, insecure deserialization, use of components with known vulnerabilities, and insufficient logging and monitoring. Evaluation was performed through a multi-layered process: automated static analysis using Semgrep and CodeQL rule sets tuned for each framework, followed by manual expert review of flagged and borderline cases. A code sample was classified as vulnerable if it contained at least one confirmed, exploitable security flaw that could be leveraged in a production deployment without additional mitigation.

To ensure statistical robustness, each model-framework-vulnerability combination was tested across multiple prompt variations. Results were aggregated per model to produce the headline vulnerability rates. We also tracked per-category and per-framework breakdowns, which inform the detailed findings in this report.

Key findings

1

Average vulnerability reduction of 95% across all models

Across all five models tested, Auditor’s context-aware security rules reduced the average vulnerability rate from 71.6% to 4.4%, a 93.9% relative reduction. This was consistent regardless of the underlying model architecture or provider. The narrowness of the residual vulnerability band (3–6%) suggests that context injection addresses the vast majority of security blind spots in AI code generation, bringing the output quality in line with what would be expected from a security-conscious senior developer.

2

Injection vulnerabilities (SQLi, XSS) saw the most dramatic improvement

SQL injection and cross-site scripting, two of the most common and dangerous vulnerability classes, showed near-complete elimination when Auditor rules were present. Without security context, models generated injectable SQL in over 80% of database-related tasks and unescaped output in over 75% of rendering tasks. With Auditor’s framework-specific rules, these rates dropped below 2%.

3

Secrets and API key exposure was nearly eliminated

In baseline testing, every model regularly hardcoded API keys, database credentials, and secret tokens directly into source files. This occurred in approximately 60% of tasks involving third-party service integration. With Auditor’s rules, hardcoded secrets appeared in fewer than 1% of outputs.

4

Gemini 2.0 and Codex 5.1 showed the highest baseline vulnerability rates but responded strongly to context injection

Gemini 2.0 produced vulnerable code in 76% of baseline tasks and Codex 5.1 in 74%, the highest rates in our evaluation. However, both models responded exceptionally well to Auditor’s context injection, dropping to 6% and 5% respectively. This suggests the security gap is primarily a context problem, not a capability problem.

5

Security misconfiguration remains the most underestimated category

Security misconfiguration, particularly overly permissive CORS policies, missing security headers, and absent rate limiting, was present in baseline outputs at rates exceeding 85% for server configuration tasks. Auditor’s stack-specific rules reduced misconfiguration rates to under 5%.

Vulnerability categories

The six vulnerability classes most frequently observed in AI-generated code.

SQL Injection

SQL injection occurs when user-supplied input is concatenated directly into database queries, allowing attackers to manipulate query logic, extract sensitive data, or execute administrative operations. AI models are particularly prone to generating injectable SQL because their training data is saturated with tutorials that use string interpolation for simplicity.

Cross-Site Scripting (XSS)

Cross-site scripting vulnerabilities arise when user-controlled data is rendered in a web page without proper encoding, enabling attackers to inject malicious scripts. AI models frequently generate XSS-vulnerable code because they treat rendering as a display problem rather than a security boundary.

Secrets & API Key Exposure

Secrets exposure occurs when sensitive credentials are hardcoded directly in source files or committed to version control. AI models generate hardcoded secrets at an alarming rate because producing working, self-contained code examples is their core objective.

Broken Access Control

Broken access control encompasses failures to enforce proper authorization, allowing users to access other users’ data or perform privileged operations without adequate role checks. AI models are especially prone to this because access control is inherently application-specific.

Security Misconfiguration

Security misconfiguration covers overly permissive CORS policies, missing HTTP security headers, and debug mode left enabled. AI models reliably generate misconfigured servers because permissive configurations are functionally simpler and more universally compatible.

Rate Limiting & DoS

The absence of rate limiting allows attackers to abuse API endpoints through brute-force attacks, credential stuffing, or resource exhaustion. AI models almost never include rate limiting because it is not part of the core functional requirement.

Why context matters

The prevailing approach to code security (static analysis tools, linters, and SAST scanners) operates on a detect-and-remediate model. Code is written, then scanned, then flagged, then fixed. This workflow made sense in a world where humans wrote code deliberately and review cycles were measured in days. In the AI-assisted development paradigm, where code is generated in seconds and often accepted with minimal scrutiny, post-hoc detection is fundamentally insufficient. By the time a SAST tool flags a vulnerability in a pull request, the insecure pattern has already been written, the developer has already moved on, and the cognitive cost of understanding and fixing the issue is significantly higher than it would have been to generate secure code in the first place.

Auditor takes a fundamentally different approach: prevention at generation time through context-aware rule injection. When a developer’s project uses Express.js with PostgreSQL, Auditor detects this stack and injects security rules specifically about parameterized queries, helmet middleware, express-rate-limit, and CORS configuration with explicit origin whitelisting. When the project is a Django application, the rules shift to Django’s ORM patterns, CSRF middleware, template autoescaping, and DRF authentication classes. The AI model receives these rules as part of its context before generating a single line of code, producing secure output from the start.

The distinction between generation-time prevention and post-generation detection is not merely a workflow preference. It represents a structural advantage. Our benchmark data demonstrates that models are highly responsive to security context when it is provided in the right format and at the right time. The 95% average reduction in vulnerabilities is achieved by giving the model the security knowledge it needs, in the context of the specific technology stack it is generating code for, before it begins generating.

Looking ahead

The integration of AI into software development is not a trend. It is an irreversible transformation. Within the next two to three years, the majority of production code will be either generated or substantially modified by AI assistants. The security implications of this shift are profound. If the industry continues to treat AI-generated code with the same post-hoc security tooling designed for human-written code, the result will be a dramatic expansion of the global attack surface.

The path forward requires a new category of security tooling: one that operates at the interface between the developer and the AI, shaping generation rather than inspecting output. Auditor represents an early and demonstrably effective instantiation of this approach. As AI models grow more capable and as code generation becomes more autonomous, the importance of embedded, context-aware security guidance will only increase. The models are capable of writing secure code. They simply need to be told what secure means for the specific application they are building.

Secure your AI-generated code today

Add context-aware security rules to your AI coding workflow in under two minutes.

Get Started