AI code generation security: how to check code an AI wrote
AI-written code compiles and runs, and yet it often fails security tests. Veracode found that roughly 44% of AI code generation tasks introduced a risky vulnerability, even though the syntax is almost always right. What helps is review. Treat the output like a stranger's pull request, a proposed change that someone must read before it is merged. Read the diff, which is the list of changed lines. Run a static analyser, check new dependencies and secrets, and keep a human approval step.
AI code generation security starts with an uncomfortable fact: what the model just wrote may work perfectly and still be unsafe. Hoping the next model fixes it is not a plan, and the checks below do not depend on which model wrote the code. If you vibe code, the broader view is in vibe coding security. The studies behind the numbers are on AI-generated code vulnerabilities.
01Is AI-generated code secure?
Not on its own. Veracode's 2026 report, a vendor study, found that roughly 44% of AI code generation tasks introduced a risky security vulnerability in tests. The average security pass rate across models is 56%. Models now write syntactically correct code nearly 100% of the time, which means it looks fine at a glance. Working code is not trustworthy code by default, and GenAI code security depends on verification, not appearances.
02Why generated code fails
The root problem for AI-generated code security is that assistants don't inherently understand your application's risk model, internal standards or threats. Three patterns follow from it.
- Repeating bad habits. If an unsafe pattern, such as SQL built by joining strings, is common in the training data, the assistant readily produces it.
- Shortcuts. A model may answer with
eval(expression). It solves the task and also opens the door to remote code execution, meaning someone else's commands running in your app. - Missing protections. Validation steps, access checks and output encoding, which cleans text before it is shown or run, are simply left out.
Cloud settings are a typical case: generated code can default to wildcard access policies, open security groups and unencrypted storage, or put secrets straight into the code instead of a secrets manager. A generated project can also pull in deprecated packages, library versions with known CVEs, or package names that don't exist. A CVE is a public ID for a known security flaw.
What the model writes matters after it is written, too. If its output is used downstream without enough validation, the results can include XSS and SSRF, which are attacks on web pages and servers, and remote code execution.
03Six checks for AI code generation security
| Step | What to do | What it catches | Limit |
|---|---|---|---|
| 1. Read the diff | Open every changed file before you accept it; your editor shows the diff, and git diff prints it | Obvious shortcuts such as eval | You will miss subtle flaws |
| 2. Static analysis | Run CodeQL or Semgrep | Vulnerabilities found by queries and rules over the source | CodeQL does not support PHP or Scala; Semgrep Community Edition sees one function or file at a time |
| 3. Dependencies | Turn on Dependabot alerts | Dependencies with known vulnerabilities | Covers known flaws, not a package that does not exist |
| 4. New package names | Check that each new package exists and is the one you meant | Invented names | Manual work |
| 5. Secrets | Run GitHub secret scanning on the repo | Credentials committed anywhere in the Git history | See the secret scanning page |
| 6. Human approval | Require a pull request approval before merge | Changes nobody else has read | Needs a second person; alone, re-read the diff and run steps 2 to 5 before you push |
Static analysis. A static analyser reads code without running it. CodeQL is GitHub's analysis engine. It treats code like data, builds a database of it and runs queries to find vulnerabilities. The results appear as code scanning alerts. There are three ways to run it, including a default setup you can switch on in a repository. Semgrep is an open-source tool that supports 30+ languages. It runs in your editor, as a pre-commit check that fires just before git saves a change, or in CI, which means automated checks on every push. Its rules look like the code you already write. No GitHub repository? Semgrep also runs in your editor, so step 2 works on your own machine, and steps 1 and 4 are manual anyway.
Dependencies. Dependabot scans the default branch, the main line of your repository. It sends an alert when a new vulnerability lands in the GitHub Advisory Database, GitHub's list of known flaws, or when your dependency list changes. An agent can also invent a package name. In one study the average share of invented packages was at least 5.2% for commercial models and 21.7% for open-source models. Attackers can register such names, which is called slopsquatting; see slopsquatting.
Human approval. Branch protection is a repository setting that requires pull request approval before changes reach the main line. It goes together with pre-commit hooks for security checks and sandboxed agent runs that cannot push straight to protected branches. A step-by-step list for solo builders is in the vibe coding security checklist.
04Who owns AI-generated code in the US?
The US Copyright Office's report from January 2025 says copyright does not extend to purely AI-generated material. The same goes for material where a human has too little control over the expressive elements. Writing prompts alone does not give enough control with current technology. Copyright still protects the original expression of a human author, even if the work includes AI-generated material. Whether a person's contribution is enough is decided case by case. These facts are about the US only and are not legal advice.
05Where agents change the picture
A coding agent does more than suggest. It runs commands and installs packages while it writes, and a diff shows none of that, so AI code generation security continues on the machine after the review. That side is covered on AI coding agent security.
A scan of the code does not cover the dependencies it pulled in: the free, open-source core of Artificial Immunity matches lockfile dependencies against OSV and CISA KEV and reports what it finds when you ask.
06The short version
The habit that matters for AI code generation security is simple: treat generated code as a pull request from a stranger. Read it, scan it, check what it pulls in, and make a person approve it before it ships.
FAQ
Is AI-generated code secure?
Not by default. In Veracode's 2026 tests, roughly 44% of AI code generation tasks introduced a risky security vulnerability. The same report says working code is not trustworthy code by default.
How secure is the AI code?
In Veracode's 2026 report, a vendor study, the average security pass rate across the tested models is 56%, barely changed from 55% in the first report. Models write syntactically correct code nearly 100% of the time, but security does not follow the same curve.
What are the top 3 AI security risks?
The first three entries of the OWASP LLM Top 10 for 2025 are prompt injection, sensitive information disclosure and supply chain. For generated code, add improper output handling, which is LLM05.
Is it legal to use AI-generated code?
The open question in the US is who owns it. Copyright does not extend to purely AI-generated material. This page is not legal advice.
What is the best AI for code generation?
No model passes security tests reliably. In Veracode's current results the leader scores 68%, while more than half of the tested models sit at 50-53%.
What is GenAI security?
It is the security of applications built on generative AI. OWASP tracks their risks in its Top 10 for LLM applications, which includes prompt injection, supply chain and improper output handling. For code, the part that matters is what the model writes.
Is GenAI used in coding?
Widely. In organizations that have adopted AI coding tools, AI now authors roughly half of all committed code, according to Veracode.
Related guides
Sources
- Veracode: 2026 GenAI Code Security Report
- Cloud Security Alliance: security risks in AI-generated code
- AWS Security Blog: a control framework for AI coding agents
- GitHub docs: about code scanning with CodeQL
- Semgrep on GitHub
- GitHub docs: about Dependabot alerts
- GitHub docs: about secret scanning
- OWASP GenAI: LLM05 Improper Output Handling
- OWASP GenAI: LLM Top 10
- arXiv: We Have a Package for You (package hallucination)
- US Copyright Office: Copyright and AI, Part 2 Copyrightability
- Artificial Immunity