Get the free coreGet core

AI code generation security: how to check code an AI wrote

Updated 5 min readSection 03 / Is it safe?
SHORT ANSWER

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.

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

StepWhat to doWhat it catchesLimit
1. Read the diffOpen every changed file before you accept it; your editor shows the diff, and git diff prints itObvious shortcuts such as evalYou will miss subtle flaws
2. Static analysisRun CodeQL or SemgrepVulnerabilities found by queries and rules over the sourceCodeQL does not support PHP or Scala; Semgrep Community Edition sees one function or file at a time
3. DependenciesTurn on Dependabot alertsDependencies with known vulnerabilitiesCovers known flaws, not a package that does not exist
4. New package namesCheck that each new package exists and is the one you meantInvented namesManual work
5. SecretsRun GitHub secret scanning on the repoCredentials committed anywhere in the Git historySee the secret scanning page
6. Human approvalRequire a pull request approval before mergeChanges nobody else has readNeeds 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.

Sources

  1. Veracode: 2026 GenAI Code Security Report
  2. Cloud Security Alliance: security risks in AI-generated code
  3. AWS Security Blog: a control framework for AI coding agents
  4. GitHub docs: about code scanning with CodeQL
  5. Semgrep on GitHub
  6. GitHub docs: about Dependabot alerts
  7. GitHub docs: about secret scanning
  8. OWASP GenAI: LLM05 Improper Output Handling
  9. OWASP GenAI: LLM Top 10
  10. arXiv: We Have a Package for You (package hallucination)
  11. US Copyright Office: Copyright and AI, Part 2 Copyrightability
  12. Artificial Immunity
A review shows what an agent wrote, not what it ran. Artificial Immunity checks the lockfile dependencies on the Mac where your agent builds.Get the free core