The 7 most common security flaws in AI-generated code

Security flaws in AI-generated code are hard to spot. Code from Claude, Cursor or Lovable usually works right away, and code that works looks secure, even when it is not. According to the 2026 GenAI Code Security Report by security vendor Veracode, models now write syntactically correct code nearly every time. Yet the same report found that “roughly 44% of AI code generation tasks introduced a risky security vulnerability in tests.”

Below are the seven security flaws in AI-generated code that show up most often. They are secrets in the code, missing permission checks, injection, vulnerable or made-up packages, too much data in logs and API responses, insecure file uploads, and CORS settings that are too open. For each one you will read what it is, why an AI tool misses it, how to recognize it yourself and how to fix it. You do not need to be a developer to follow along. You will get the most out of it if you go through the list with whoever built your app.

Seven gray, blue and cream blocks in a curved row, connected by a thin glowing blue line
An AI tool builds what you ask for. What you do not ask for, it rarely checks.

Why do the same flaws keep showing up in AI-generated code?

The flaws are not random. They come from a few fixed habits of AI tools.

Models learn from example code. Tutorials often leave out security to keep the example short. The key sits right in the code, and the code glues the query together from strings. The model picks up that habit.

The tool also does not know your business rules. Nobody has written down which customer may see which invoice. Ask it to show the invoice with a given number, and that is exactly what you get. For everyone.

And an AI tool often fixes an error message by making it go away. If the browser throws a CORS error, the tool opens everything up. The message is gone, the app works, and nobody notices there is now a hole.

Most of the flaws below appear in the OWASP Top 10:2025, the best-known list of risks for web applications. Each flaw lists its OWASP category, so your developer can look it up.

The seven security flaws in AI-generated code, one by one

The flaws at the top are the ones that let someone get straight to your data or your accounts.

  1. 01

    Secrets in the code

    API keys, database passwords or tokens sit in the code. Sometimes they are in a .env file that is committed to the repository, or in the JavaScript that ships to the browser. An AI tool puts a key wherever the code works right away. Whether that key is then visible to everyone is not something it weighs. A variable with a prefix like NEXT_PUBLIC_ or VITE_, for example, ends up in the browser, readable by every visitor. OWASP counts hard-coded passwords and keys under A07 Authentication Failures.

    How to spot it: search the code and the full repository history for keys, for example with GitHub secret scanning. Open your app in the browser, look at the JavaScript it loads and search for words like key, secret and token.

    How to fix it: replace every leaked key with the provider. Deleting the line is not enough, because the repository history still remembers it. Keep secrets in environment variables on the server or in a secrets vault. Add .env to .gitignore, and never let the browser talk directly to a paid API. More in the OWASP Secrets Management Cheat Sheet.

  2. 02

    Missing permission checks (IDOR)

    The app checks whether you are logged in, but not whether the data belongs to you. A link like /invoices/1042 shows invoice 1042 to any logged-in user who changes the number. This is called IDOR (insecure direct object reference), and it falls under number one on the OWASP list: A01 Broken Access Control. An AI tool misses it because the code looks correct. The query neatly fetches the right invoice. The condition “and this invoice belongs to this customer” was never in the prompt. With databases like Supabase or Firebase you see the same flaw in another form: row-level access rules that are turned off or too loose.

    How to spot it: create two test accounts. Log in with the second one and change the numbers in links and API calls to point at data from the first. If you see something that is not yours, the check is missing. In the code, look for queries that find a record only by the ID from the request. They do not filter on the logged-in user or company.

    How to fix it: filter every query on the server by the logged-in user or organization, even if the interface already hides the button. A check that only runs in the browser is not a check: anyone can call your API directly. Deny access by default and open only what is needed. Write a test that tries to get in with a second account. Examples are in the IDOR Prevention Cheat Sheet.

  3. 03

    Injection: SQL, HTML and prompts

    User input goes unfiltered into a database query (SQL injection), into the HTML of the page (cross-site scripting) or into a shell command. That is A05 Injection. AI tools still often glue text together into a query, or render markup with innerHTML or dangerouslySetInnerHTML. A lot of example code does it that way. If your app has an AI feature, there is a new form: prompt injection. Text in a document or email then gives the language model hidden instructions. The OWASP Top 10 for LLM Applications puts it in first place.

    How to spot it: search the code for queries built with plus signs or template strings, for innerHTML, and for exec or system calls that include input. Type an apostrophe or a bit of HTML such as <b>test</b> into every input field. If you get a database error or bold text, the app is not handling input properly.

    How to fix it: always use parameterized queries or your ORM’s functions, let the framework escape HTML, and avoid shell commands that include user input. Treat a language model’s answer as untrusted input too. Never let it become a query, a command or part of a page as is (LLM05: Improper Output Handling). See also the SQL Injection Prevention Cheat Sheet.

  4. 04

    Vulnerable and made-up packages

    An AI tool picks packages from what it saw during training. Those are often older versions, sometimes with a known vulnerability (CVE). Worse is a package that does not exist. Researchers had 16 language models write 576,000 code samples and found that “the average percentage of hallucinated packages is at least 5.2% for commercial models and 21.7% for open-source models” (Spracklen et al., USENIX Security 2025). An attacker can register such a made-up name and put malicious code in it. This is called slopsquatting. OWASP calls this A03 Software Supply Chain Failures.

    How to spot it: for every new package, check that it really exists, who maintains it and when it was last updated. Run npm audit, pip-audit or your own package manager’s check, and make sure there is a lockfile in the repository.

    How to fix it: update packages with known vulnerabilities, remove what you do not use and pin versions in a lockfile. Let a tool like Dependabot or Renovate suggest updates. And never install a package the AI mentions without looking it up first.

  5. 05

    Too much data in logs, error messages and API responses

    AI code likes to log everything, because that helps with debugging: full requests, tokens, email addresses. APIs often send back the whole record, including fields the interface never shows, such as a password hash or internal notes. And an error message sometimes shows a full stack trace to the visitor. This touches several OWASP categories: A09 (logging), A10 (error handling) and A01 (data you should not be able to see). For an AI tool, more information simply seems more helpful. Who gets to see that information later is not something it weighs.

    How to spot it: open your browser’s developer tools and look at what your APIs return, not just what the screen shows. Read a few lines of your production logs. Trigger an error and look at what the visitor sees.

    How to fix it: have each API return only the fields it needs, using a fixed list instead of the whole object. Do not log passwords or tokens, and no more personal data than necessary: logs fall under the GDPR too. Show visitors a generic error message and keep the details in the logs. Tips per type of log are in the Logging Cheat Sheet.

  6. 06

    Insecure file uploads

    An upload form is quick to build. The AI tool then often trusts the file name and file type the browser sends along. It sets no size limit and stores the files where the server can serve them as a web page. An uploaded SVG or HTML file with a script in it then runs on your own domain. OWASP counts unrestricted uploads under A06 Insecure Design.

    How to spot it: try a file with a double extension, such as invoice.pdf.html, or a very large file. Check where uploaded files end up and whether you can open them with a plain link without logging in.

    How to fix it: allow only the file types you really need and check the content, not just the extension. Give files a new, random name, store them outside the web root or in protected storage, set a maximum size and serve them as downloads. The File Upload Cheat Sheet has a complete list.

  7. 07

    CORS settings that are too open and missing headers

    CORS decides which other websites may call your API from the browser. When an AI tool runs into a CORS error, allowing everything is the quickest fix: every website, sometimes even with the user’s login credentials included. Security headers such as Content-Security-Policy and Strict-Transport-Security are often missing altogether, because the app works without them. Both fall under A02 Security Misconfiguration.

    How to spot it: search the code for a CORS setting with an asterisk (*) or origin: true. Scan the headers of your live app with Mozilla’s free HTTP Observatory.

    How to fix it: set a fixed list of domains in the CORS setting that may call your API. Add the basic headers through your framework or hosting: Content-Security-Policy, Strict-Transport-Security, X-Content-Type-Options and a rule that stops other sites from embedding your app. The HTTP Headers Cheat Sheet explains which values to choose.

Which flaws does a scanner find, and which does it miss?

You can automate part of this list. A secret scanner finds most keys, a package check finds known vulnerabilities and a header scan shows right away what is missing. Other flaws sit in the logic of your app. A scanner does not know which customer may see which invoice, so it rarely finds a missing permission check.

FlawOWASP Top 10:2025Does an automated scan find it?

Secrets in the code

A07 Authentication Failures

Usually, if you also scan the repository history

Missing permission checks (IDOR)

A01 Broken Access Control

Rarely, because it depends on your business rules

Injection (SQL, HTML, prompts)

A05 Injection

Partly: classic patterns yes, prompt injection hardly

Vulnerable and made-up packages

A03 Software Supply Chain Failures

Known CVEs yes, a made-up package not always

Too much data in logs and API responses

A09, A10 and A01

Rarely, because you need to know which fields are sensitive

Insecure file uploads

A06 Insecure Design

Partly

CORS too open and missing headers

A02 Security Misconfiguration

Yes, with a header scan of your live app

Pay special attention to the rows that say “rarely.” Those are the flaws where someone gets to see another customer’s data. To find them, you need someone who knows how the app is used.

How do you ask your AI tool for more secure code?

You can prevent part of these flaws in the prompt itself. Above all, tell the tool what it cannot know on its own:

  • Say who may see what, for example: a user only sees the invoices of their own company.

  • Ask for parameterized queries, and for no secrets in the code or in the browser.

  • For every suggested package, ask for the official name and the link to the package page, and check it yourself.

  • After building, have it write a test that tries to open the first account’s data with a second account.

  • Do not let it fix a CORS or permission error by opening everything up. Ask which setting is the minimum needed.

  • Put these rules in your AI tool’s project instructions, so it applies them every time. In Claude Code that is a CLAUDE.md file, in Cursor a rule in .cursor/rules, and in Lovable the project knowledge.

That makes the code better, but not guaranteed secure. The model that wrote the code looks at it with the same blind spots. The article on why an app built with AI needs a security audit explains when an independent review makes sense.

Are you building a whole app this way, also known as vibe coding? Then go through the vibe coding security checklist before you launch.

In a security audit by Voltti, AI models and our own skills search the code for exactly these flaws. Then a person with 20+ years of experience in web development reviews every finding. You get a report with the severity and the fix for each finding. The steps are in the article on how Voltti runs a security audit with AI. If your team works with AI tools and business data itself, also look at the practical safety checklist for AI and business data. Want someone to review your app? For businesses in Belgium and the Netherlands, we are happy to discuss what that would look like for your app.

Portrait of Seppe Gadeyne

Seppe Gadeyne is a developer and the owner of Voltti. He sets up AI agents for companies in Belgium and the Netherlands.

Updated on

Frequently asked questions

01Which security flaw is most common in AI-generated code?

Missing permission checks stand out. The OWASP Top 10:2025 puts Broken Access Control in first place, and AI tools miss this flaw easily because the code looks correct. Secrets in the code and injection are also common, because a lot of the example code that models learn from contains those flaws itself.

02What is IDOR?

IDOR stands for insecure direct object reference. The app lets you request data by a number or ID, but does not check whether that data belongs to you. Change the number in the link, and you see someone else’s data. It is a form of Broken Access Control.

03What is slopsquatting?

In slopsquatting, an attacker registers a package name that an AI tool made up. If a developer installs that suggested package without checking, they pull in the attacker’s malicious code. So for every new package, check that it really exists and who maintains it.

04Does a security scanner find all of these flaws?

No. Scanners are fairly good at finding keys in the code, known vulnerabilities in packages and missing headers. Flaws in the logic of your app, such as a missing permission check or an API that returns too many fields, they rarely find. For that, someone needs to know who may see what.

05Can I ask my AI tool to write secure code?

That definitely helps. Tell the tool who may see which data, and ask for parameterized queries and no secrets in the code. Then have it write a test with a second account. The code gets better, but not guaranteed secure: the model looks at its own code with the same blind spots.

Want your AI-built app reviewed?

Discuss your pilot