Home/Journal/Opinion
OpinionAI SecurityCode SecurityVulnerability Research

Vulnerabilities in AI-Generated Code: What CTOs Need to Know

AI-generated code reproduces known vulnerability classes at scale. Here is what types appear most often, why LLMs produce them, and what to do about it.

Greywatch Security Team··8 min read

AI-generated code introduces real, exploitable security vulnerabilities at scale. Large language models are trained on public code repositories where security was rarely the priority - Stack Overflow answers from 2012, open-source scaffolding with hardcoded credentials, API handlers that assume authorization at the route level. When an LLM generates code from those patterns, it reproduces the same mistakes. The result is not hypothetical risk. It is known vulnerability classes - IDOR, SQL injection, exposed keys, broken authentication - appearing in production code at a rate that scales with how fast teams are shipping.

What kinds of vulnerabilities appear most often in AI-generated code?

The same classes that have dominated breach reports for a decade, now reproduced faster and at greater volume:

Insecure direct object references (IDOR)
The most common finding by Greywatch's AI agents on AI-heavy codebases. An LLM generating an API endpoint assumes authorization lives at the route level. It does not validate that the authenticated user owns the object being requested. The result: /api/invoices/12345 returns any invoice for any logged-in user who knows the ID.
SQL injection
Stack Overflow answers from 2010-2018 are full of string-concatenated queries. LLMs trained on that corpus reproduce them. Modern ORMs make injection less common in greenfield code - but LLMs generating raw query strings, especially in edge-case data access patterns, still produce injection-vulnerable code.
Hardcoded secrets and API keys
LLMs generate placeholder code that looks like real configuration. Development teams copy it, edit the values they remember to edit, and ship. Keys for third-party services, internal APIs, and database connections end up in git history. An exposed key is a working exploit with no further steps required.
Broken authentication
JWT validation logic is conceptually simple but implementationally fragile. LLMs generate authentication middleware that validates the token structure but skips critical checks: expiry validation, algorithm enforcement, audience verification. The code passes unit tests. It fails against a targeted attack.
Server-side request forgery (SSRF)
LLMs generate URL-fetching code without validating the target. An endpoint that accepts a URL parameter and fetches it internally can be used to probe internal infrastructure, reach metadata services in cloud environments, and exfiltrate data. LLMs have no model of the network topology the code will run in.
Path traversal & exposed admin endpoints
File handling code generated without input sanitization. A route that accepts a filename parameter and reads it from disk is a one-line vulnerability. LLMs also generate admin routes as a matter of course - often with weak authentication or none at all on /admin and /api/admin endpoints.

Why does AI-generated code reproduce security mistakes?

Three reasons, none of which are bugs that will be patched away:

How fast are these vulnerabilities being exploited?

Fast enough that a quarterly security review is not a meaningful control.

Zero Day Clock - median time to exploitation
771 days
2018 — you had two years to patch
6 days
2023 — a quarterly pentest was already too slow
<4 hours
2024 — most exploited vulnerabilities were weaponized before public disclosure

When exploitation is faster than most security review cycles, the only credible response is security that runs as continuously as the pipeline it is protecting. AI-generated code shipped on Monday is attack surface by Monday afternoon.

Is this different from vulnerabilities in human-written code?

Yes, in two important ways:

Does a SAST scanner catch these vulnerabilities?

Some of them, sometimes.

SAST scanners are trained on the same vulnerability classes and have the same problem as AI code generators: they flag patterns, not behaviors. A scanner catches the textbook SQL injection - string concatenation in a query builder. It does not catch the IDOR in an API endpoint that has perfectly valid code structure but missing authorization logic. Authorization flaws, business logic vulnerabilities, and context-dependent access control failures require an adversarial perspective - actually attempting to access objects you should not be able to access.

The output of a scanner on an AI-heavy codebase is typically 500-1,000 findings, most of which are false positives or low-severity informational items. The 3 critical findings that represent real breach risk are somewhere in that list. Finding them requires someone who can read the application the way an attacker would.

What should a startup do about vulnerabilities in AI-generated code?

First: acknowledge the baseline has changed. If your team is using AI code generation at scale (Cursor, GitHub Copilot, Claude in the IDE), your code is materially different from what a traditional security review was calibrated to evaluate. The review process needs to catch up.

Second: treat authorization as the highest-priority class. IDOR and broken access control are the most common and most damaging findings in AI-heavy codebases. Every API endpoint should have explicit authorization logic that is independently reviewed - not generated.

Third: run continuous offensive testing. The exploitation window is 4 hours. Point-in-time security (quarterly VAPT, annual pentest) was calibrated to a world where that window was 771 days. It is not calibrated to now. The only security posture that makes sense given the Zero Day Clock data is one that runs continuously - the same way the CI/CD pipeline does.

Fourth: demand proof, not flags. A scanner tells you what might be wrong. A finding worth fixing comes with the HTTP request that worked, the data it exposed, and reproduction steps. If your security process cannot show you an actual exploit, it cannot tell you what your real risk is.

Frequently asked

What percentage of AI-generated code contains security vulnerabilities?
Published research puts it between 40-60% of AI-generated code snippets containing at least one security vulnerability, depending on the model and prompt type. The more meaningful question is which vulnerabilities are exploitable in your specific application, against your specific data. That requires an adversarial test, not a percentage.
Are newer LLMs (GPT-4o, Claude, Gemini) more secure than older ones?
Newer models produce fewer obvious vulnerabilities in standard benchmarks. They still reproduce authorization flaws, SSRF, and business logic errors that require application context to identify. Improvement in the model does not change the fundamental problem: LLMs cannot reason about your threat model.
Does a SAST scanner catch vulnerabilities in AI-generated code?
Some of them, sometimes. SAST scanners flag patterns, not behaviors. They catch textbook SQL injection but miss IDOR and authorization flaws that require an adversarial perspective. The output on an AI-heavy codebase is typically 500-1,000 findings, most of which are false positives. The 3 critical findings that represent real breach risk are somewhere in that list.
Is AI-generated code safe to use in production?
Yes - with the right review process. The risk is not that AI code generation is inherently unsafe. The risk is that teams are shipping AI-generated code without adjusting their security review to account for the vulnerability classes it introduces. The gap is process, not technology.
How is reviewing AI-generated code different from reviewing human-written code?
The vulnerability classes are the same. The distribution is different. AI-generated code over-indexes on authorization flaws and under-indexes on memory safety issues. Review processes calibrated for human-written code may not weight authorization checks heavily enough for AI-generated codebases.
Does Greywatch specifically test for vulnerabilities in AI-generated code?
Yes. Greywatch's offensive harness runs continuous adversarial testing against live applications - including API endpoints, authentication flows, and data access patterns that are the most common output of AI code generation. The agents are designed to find exploitable vulnerabilities, not flag patterns. Every finding comes with the exact request that worked and reproduction steps.

Greywatch finds the vulnerabilities that ship with AI-generated code. The harness runs continuously. A security engineer validates every finding. You get confirmed critical vulnerabilities - with exploit proof - not a thousand-item noise dump.

Start with a free scan →