How to Check AI-Written Code for Security Flaws Before It Ships

Try Our Free Tools!
Master the web with Free Tools that work as hard as you do. From Text Analysis to Website Management, we empower your digital journey with expert guidance and free, powerful tools.

Quick Summary

AI coding assistants can accelerate development, but their output is not automatically secure. Research shows that AI-generated code frequently contains vulnerabilities, including cross-site scripting, missing authorization checks, and unsafe input handling. Package hallucinations and exposed credentials add risk, so treat every AI-generated suggestion as untrusted code.

Before shipping AI-written code, development teams should combine careful code reviews with automated security checks, including static analysis, dependency scanning, and secret detection. Verifying third-party packages, protecting credentials, testing running applications, and conducting threat modeling or penetration testing for critical features can help catch weaknesses before they reach production.

The goal is not to limit AI-assisted development but to ensure that faster coding never comes at the expense of application security.

Introduction

Most development teams now have an AI assistant somewhere in their workflow. In Stack Overflow’s 2025 Developer Survey, 84% of respondents said they use or plan to use AI tools in their development process. The same survey found that more developers distrust the accuracy of those tools (46%) than trust it (33%), and only 3% said they highly trust the output.

That gap between using a tool and trusting it is where security problems tend to hide. The code compiles, the tests pass, and the pull request looks tidy. Whether it is safe is a separate question, and it needs its own checks.

This guide covers the checks that catch the most common problems, in the order a team can realistically add them.

What the Research Says about AI-Generated Code

The clearest numbers so far come from Veracode’s 2025 GenAI Code Security Report. Veracode found that 45% of the code samples it tested failed security tests and introduced OWASP Top 10 vulnerabilities. Java was the riskiest language, with a 72% security failure rate. C# came in at 45%, JavaScript at 43%, and Python at 38%.

Cross-site scripting was one of the most frequent issues. The models failed to defend against it in 86% of the relevant samples. Veracode also checked whether newer models do better, and the short answer was no. Newer models wrote code that worked more often, but not more secure code.

That matches what you would expect from how these tools work. A model is very good at producing code that looks like what it has seen, and much public code skips input validation, output encoding, and authorization checks. The assistant is not trying to write insecure code. It just has no idea which inputs in your application are hostile.

A laptop displaying lines of code on its screen, with another blurred monitor in the background.

Treat Every Suggestion as Untrusted Code

The most useful mindset shift is simple: review AI output the way you would review a pull request from a contractor you have never worked with. It might be excellent. It might also be missing the one check that matters.

When you read a generated function, follow the data:

  • Where does input enter? Request parameters, headers, file uploads, webhooks, and messages from other services all count.
  • Is that input validated against what you expect, or only against what breaks the code?
  • Where does it leave? A database query, an HTML page, a shell command, a log line, or another API call each need their own handling.
  • Who is allowed to call this? New endpoints written by an assistant often work perfectly for the logged-in user and never check whether that user should see the record they asked for.

Missing authorization is worth calling out on its own. Scanners struggle to find it because the code isn’t technically wrong. It simply trusts an ID it should have checked.

Check Every Package the Assistant Names

AI assistants regularly suggest libraries that do not exist. A study of package hallucinations in code-generating models, published on arXiv, found that the average rate of hallucinated packages was at least 5.2% for commercial models and 21.7% for open-source models. Across the tests, the researchers collected 205,474 unique hallucinated package names.

Each name is an opportunity for an attacker. If a model keeps suggesting the same fake package, someone can register that name on npm or PyPI and wait for developers to install it. This is sometimes called slopsquatting, and it turns a harmless mistake into a supply chain attack.

A few habits close most of this gap:

  • Before adding any dependency, confirm it exists, who maintains it, how old it is, and how widely it is used.
  • Be suspicious of a package published recently with a name that sounds exactly like what you asked for.
  • Commit lockfiles and pin versions, so a later install cannot quietly pull something different.
  • If your organization uses a private registry or proxy, route installs through it and block packages that have not been approved.

Keep Secrets Out of Prompts and Generated Code

Hardcoded credentials were a problem long before AI assistants, and the tools have not fixed it. GitGuardian’s State of Secrets Sprawl 2025 report counted 23.8 million secrets leaked on public GitHub repositories in 2024, a 25% increase over the year before.

Assistants make this easier to get wrong in two ways. They often fill in example API keys or connection strings that developers forget to replace, and they work with whatever is in the open files, including the real .env file someone left open in the editor.

The AI tools themselves are now a target too. In the s1ngularity supply chain attack in August 2025, malicious code hijacked locally installed AI command-line tools such as Claude Code, Gemini CLI, and Amazon Q CLI to search developer machines for secrets, leaking about 2,000 secrets and 20,000 files across 225 victims. For more figures on how attackers use and abuse AI, these AI cyber attack statistics collect the sourced numbers in one place.

The practical steps are familiar. Run secret scanning as a pre-commit hook and again in CI. Keep real credentials in a secrets manager rather than in files the assistant can read. Treat any key that appears in a prompt, a chat log, or a generated file as exposed, and rotate it.

Digital illustration of a human head with code and circuitry, next to the text AI CODING and programming code on the left side.

Put Automated Checks Where AI Code Lands

Manual review does not scale when an assistant can produce hundreds of lines in a minute. The answer is to make sure every change passes through the same automated gates, whoever or whatever wrote it.

A reasonable baseline looks like this:

  • Static analysis (SAST) on every pull request, tuned so high-severity findings block the merge instead of piling up in a dashboard.
  • Dependency scanning to flag known vulnerable versions and packages with no history.
  • Secret scanning in CI as a second line behind the pre-commit hook.
  • Dynamic testing (DAST) against a staging build, which catches problems that only show up when the application is actually running.

If your team uses AI for larger features, it also helps to label AI-assisted pull requests. That is not about blame. It tells reviewers where to slow down.

Teams building features on top of language models have a second layer to worry about. The OWASP Top 10 for LLM Applications lists Improper Output Handling as LLM05: model output passed to a browser, a database, or a shell without validation. The same rule applies to code an assistant writes and to text a model returns at runtime. Neither should be trusted just because it came from a tool you pay for.

Ask for Secure Code, Then Verify It Anyway

Prompts make a difference. Asking for parameterized queries, strict input validation, output encoding, and least-privilege access usually produces better code than asking for a feature and nothing else. Some teams keep a short security preamble that every developer pastes into their assistant, or set it as a default instruction.

That is worth doing, but it is not a control. A model can follow the instruction in one function and ignore it in the next. Everything in the steps above still applies.

Test the Running Application

Static tools and code review catch a lot, but some of the most damaging flaws only appear in a running system. Broken access control between users, business logic that can be abused in the wrong order, race conditions, and chained low-severity issues rarely look dangerous on their own.

This is where threat modeling and hands-on testing earn their keep. Before a major release, walk through how an attacker would use each new feature. For applications that handle payments, health data, or customer records, an independent penetration test gives you a view your own team cannot easily get, because they know how the system is supposed to work.

If you want a framework to hang all of this on, NIST’s Secure Software Development Framework (SP 800-218) maps well to AI-assisted development. Its practices on reviewing code, verifying third-party components, and testing executable code apply whether a person or a model wrote the first draft.

A Short Checklist

How to Check AI-Written Code for Security Flaws Before It Ships: A Short Checklist.

AI coding assistants are not going away, and they do make good developers faster. Teams that avoid trouble keep their security checks the same, no matter who wrote the code.

Before AI-written code ships, make sure that:

  • Someone has followed the data from input to output and checked authorization.
  • Every new package has been confirmed as real, maintained, and pinned.
  • No secrets are in the code, the prompts, or the files the assistant can read.
  • SAST, dependency scanning, and secret scanning have run and passed.
  • The feature has been tested as a running application, not only as source code.

None of these steps are new. What has changed is how much code now reaches them, and how confident that code looks when it arrives.

Try Our Free Tools!
Master the web with Free Tools that work as hard as you do. From Text Analysis to Website Management, we empower your digital journey with expert guidance and free, powerful tools.
Disclosure: Some of our articles may contain affiliate links; this means each time you make a purchase, we get a small commission. However, the input we produce is reliable; we always handpick and review all information before publishing it on our website. We can ensure you will always get genuine as well as valuable knowledge and resources.

Article Published By

Neil Hemmings

I'm Neil Hemmings from Anaheim, CA, with an Associate of Science in Computer Science from Diablo Valley College. As Senior Tech Associate and Content Manager at RS Web Solutions, I write about AI, gadgets, cybersecurity, and apps – sharing hands-on reviews, tutorials, and practical tech insights.
Share the Love
Related Articles Worth Reading