CCyberwiki

Security of AI-generated code

A coding assistant writes code from a plain-language request or from the code around the cursor, using a large language model trained largely on public code. Public code contains exploitable, buggy examples, and the model learns from them as well as from the safe ones.1 A request such as "add a search box" names a feature and no security requirement, which leaves the choice between a safe and an unsafe way of writing it to the model.2 The result often runs correctly and still contains a vulnerability. Committing such code with almost no human review is called vibe coding.3

The OWASP Top 10 2025 edition puts Inappropriate Trust in AI Generated Code among the risks that narrowly missed the ten, and notes that no CVE or CWE entry describes AI-generated code as such.3 The flaws measured in such code fall under existing Top 10 categories.2

Why the flaws appear

Secure code samples are a minority in public sources, and a model may weigh them less for that reason.3 In a 2025 test of more than 100 models on 80 tasks, each of which could be completed in a safe or an unsafe form, the models picked the unsafe form 45 percent of the time. Over successive model releases the share of syntactically correct code rose while the security result stayed flat, and larger models did not do significantly better than smaller ones.2

In a 2023 user study, 47 participants solved five security-related tasks in Python, JavaScript and C. Those given an assistant wrote less secure code in four of the five tasks and were more likely to believe their code was secure. Participants who trusted the assistant less and reworded their prompts produced fewer vulnerabilities, while feeding the assistant's earlier answer back in as a new prompt tended to carry its flaws forward.4

Table 1. Measurements of security in AI-generated code
StudySet-upResult
Pearce et al., 2021GitHub Copilot, 89 scenarios built on high-risk weakness types, 1,689 programsAbout 40% vulnerable1
Perry et al., 202347 participants, five tasks, model codex-davinci-002Less secure code with the assistant in four of five tasks4
Veracode, 202580 tasks, over 100 models, results checked by static analysisInsecure in 45%; Java above 70%; Python, C# and JavaScript 38 to 45%2
Spracklen et al., 202516 models, 576,000 code samples in two languagesPackages that do not exist: at least 5.2% (commercial models), 21.7% (open-source models)5
GitGuardian, 2025Public GitHub repositories scanned in 2024Secret leak rate 6.4% where Copilot is used, at least 4.61% across all6

Common flaws

Generated code is weakest where safety needs a step the request did not mention. In the 2025 test, models failed to guard against cross-site scripting in 86 percent of the relevant tasks and against Log injection in 88 percent.2 Escaping user content before it is placed in HTML prevents the first.7

Other reported problems are outdated cryptography, outdated dependencies, missing error handling and secrets written into the code.7 Public repositories that use Copilot leaked secrets at a rate of 6.4 percent, about 40 percent above the rate for public repositories overall.6

Models also name packages that have never been published. The 2025 package study collected 205,474 distinct invented names.5 Anyone who registers one of those names on a public package registry receives the installs of every project that follows the suggestion, an attack called Slopsquatting and a variant of Dependency confusion and typosquatting.

Access control fails in a different way in apps made with AI app builders, where the browser queries the database directly and per-row rules decide what each caller may read. When those rules are missing, or do not match what the interface allows, any visitor can query the data around the interface.8

Lovable projects, 2025

On 20 March 2025 a researcher changed one query on Linkable, a site built with the Lovable app builder, and received every row of its users table. The public key in the page was there by design; the table had no row level security. A script then visited the homepages of 1,645 projects listed on Lovable Launched, a gallery of published projects, and found 303 endpoints with missing or weak rules in 170 of them, about 10.3 percent. A later check of Linkable, in May, found rules that applied to signed-in requests only: with the authorization header removed, a request could read and write rows, and insert a record with payment_status set to paid, bypassing the Stripe payment step.8

The flaw was published as CVE-2025-48757 with a CVSS score of 9.3. Lovable disputes it, on the grounds that each customer is responsible for protecting the data of their own app.9 The security scan that Lovable added in April 2025 checked that a policy existed, not whether it was correct.8 Securing an application made with an AI app builder covers the settings in each builder.

Review and instructions

The person who commits generated code is responsible for it and should be able to read and explain each line.3 The code goes through the same code review, tests and static analysis as code written by hand.7 Vibe coding is advised against for complex functions, business-critical programs and programs that stay in use for a long time.3

Assistants read project instruction files such as CLAUDE.md, a Copilot instructions file or Cursor rules. Short, specific lines there change the default the model picks. Lines copied in that do not apply to the project work against this: the model becomes more likely to add extraneous or wrong code for attacks that cannot happen there.7 Example lines:

Use parameterized queries for all database access.
Encode user content before it is placed in HTML.
Read keys from environment variables and never write them into code.
Add a package only after confirming it exists on the registry and is the intended one.

In a second pass the assistant is asked to list the problems in its previous answer and then to fix them, once or several times. The method is called recursive criticism and improvement.7

Prompt for a coding agent

Review the code changed on this branch that was written with an AI assistant. Look for SQL built by joining strings, user input written into HTML or log lines without encoding, keys or passwords in the code, MD5, SHA-1 or ECB used for passwords or encryption, routes or database tables with no check of who is asking, and packages that do not exist on the registry or have a name close to a popular one. For each finding give the file, the line and a proposed fix. Change nothing until I confirm.

In SecFlash

SecFlash reports each of these flaws under its own code. On the Security page, SF-SEC-702 marks a key in the code or its history, SF-SEC-704 SQL built from strings, SF-SEC-646 functions such as eval and innerHTML that lead to cross-site scripting, and SF-SEC-707 broken hashing or encryption. On the Cloud page, SF-CLD-501 marks a public table without row level security and SF-CLD-508 a policy that lets every caller through. SF-EXP-213, on the Exposure page, shows a Supabase table that answers a read with no login.

See also

References

  1. a b Pearce, H., Ahmad, B., Tan, B., Dolan-Gavitt, B. and Karri, R. (2021, revised 16 December 2021). "Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributions". arXiv 2108.09293; IEEE Symposium on Security and Privacy 2022.
  2. a b c d e Veracode (30 July 2025). "AI-Generated Code Poses Major Security Risks in Nearly Half of All Development Tasks, Veracode Research Reveals". Veracode press release.
  3. a b c d e OWASP Foundation (2025). "Next Steps". OWASP Top 10:2025.
  4. a b Perry, N., Srivastava, M., Kumar, D. and Boneh, D. (2023). "Do Users Write More Insecure Code with AI Assistants?". arXiv 2211.03622; ACM CCS 2023.
  5. a b Spracklen, J., Wijewickrama, R., Sakib, A. H. M. N., Maiti, A., Viswanath, B., Jadliwala, M. (2025). "We Have a Package for You", an analysis of package hallucinations by code-generating LLMs. 34th USENIX Security Symposium.
  6. a b GitGuardian (2025). "The State of Secrets Sprawl 2025". GitGuardian.
  7. a b c d e OpenSSF Best Practices and AI/ML Working Groups (1 August 2025). "Security-Focused Guide for AI Code Assistant Instructions". OpenSSF Best Practices Working Group.
  8. a b c Palmer, M. (2025). "Statement on CVE-2025-48757". mattpalmer.io.
  9. back CVE Program (30 May 2025, updated 21 August 2025). "CVE-2025-48757". cve.org.