How to Catch Security Vulnerabilities in AI-Written Code

AI coding agents don’t write insecure code because they’re bad at security — they write insecure code because they optimize for “makes the feature work” and security is often an implicit, unstated requirement rather than something visible in the prompt. Catching AI code vulnerabilities before they ship means treating security review as a deliberate, separate pass, not something you assume happened automatically.
The Vulnerability Patterns That Show Up Most in AI-Written Code
- Unvalidated input — an agent handling the happy path correctly while skipping input sanitization for an edge case it wasn’t explicitly told to consider.
- Missing authorization checks — code that authenticates a user correctly but forgets to verify they’re authorized for the specific resource being accessed.
- Hardcoded secrets — API keys or credentials embedded directly in generated code, especially in example or scaffolding code that gets copy-pasted into a real project.
- Unsafe deserialization — trusting and directly processing structured data from an untrusted source without validation.
Build a Review Checklist and Actually Use It
A short, repeatable checklist catches most of these before merge. Run every AI-generated pull request through the same list, the same way you’d apply the OWASP Top Ten to human-written code:
- Does every user input get validated before use, not just the ones the happy-path test covers?
- Does every protected action check both authentication and authorization, not just one?
- Are there any hardcoded credentials, tokens, or keys anywhere in the diff?
- Does anything deserialize or process external data without validating its structure first?
Static Analysis Catches Volume, Not Logic
Automated scanning tools are genuinely useful for catching the mechanical issues — known-vulnerable dependencies, obvious injection patterns, secrets accidentally committed. What they consistently miss is logic flaws: an agent that “fixes” a bug by removing the check that caused it, which looks perfectly clean to a scanner but reopens a real hole. That specific failure mode is worth watching for explicitly, and it’s covered in more depth in our guide on reviewing AI-generated code before production.
Where This Matters Most
| Code Area | Review Priority |
|---|---|
| Authentication / authorization | Highest — always full manual review |
| Payment handling | Highest — full review + second reviewer |
| Internal tooling, non-user-facing | Moderate — automated scan + spot check |
| Static content, UI-only changes | Lower — automated scan usually sufficient |
Sandbox the Agent, Reduce the Stakes
None of this replaces good containment practices in the first place — running an agent inside a scoped sandbox, as covered in our AI coding agent setup guide, means a vulnerability that does slip through has a smaller blast radius while it’s being caught in review.
Frequently Asked Questions
Are AI agents worse at security than human developers on average?
Not categorically — but they don’t reliably infer unstated security requirements the way an experienced engineer’s instincts do, so security needs to be an explicit part of the prompt and the review, not assumed.
Can I just ask the AI to check its own code for vulnerabilities?
It’s a reasonable extra pass, but treat it as a supplement to human review and automated scanning, not a replacement — an agent’s self-review has the same blind spots as its original generation.
What’s the fastest way to start catching these issues today?
Add the four-question checklist above to your pull request template so it’s asked explicitly on every AI-assisted change, rather than relying on reviewers to remember it unprompted.
The Bottom Line
Catching AI code vulnerabilities is mostly a process problem, not a tooling problem — a short, explicit checklist applied consistently catches far more than hoping a general code review will happen to notice. Pair that habit with static analysis and a properly sandboxed development setup, and the risk drops substantially without slowing development to a crawl.
Related reading: How to Review AI-Generated Code Before It Reaches Production.







3 Comments