How to secure AI-generated code: a practical guide for engineering teams

Summary

Agents opened more than 40% of GitHub pull requests in September 2026, up from 17% in March. Review queues did not grow by the same amount. Somewhere in that gap, a lot of code is getting merged that no security-minded person has really looked at.

Securing AI-generated code comes down to two places. Before code gets written, give your agents security context that matches how your codebase works, so they write fewer vulnerabilities.

After it's written, review every pull request with something that understands your codebase, so the ones that slip through get caught before merge. Everything else in this post is detail on doing those two well.

What's different about AI-written code

Agents rarely write textbook SQL injection anymore. They write endpoints that check you're signed in but not that you own the record. They add a package because it solved the problem in front of them. They copy a permissive CORS config from an example. The code compiles, the tests pass, and the diff looks fine.

It's reaching production, too. Georgia Tech researchers traced 35 CVEs to AI coding tools in March 2026 alone, up from six in January (Vibe Security Radar).

By the end of March the count was 74, and March alone beat all of 2025. The researchers think the real number is five to ten times higher, because most AI-written code doesn't say it was AI-written.

A separate study of 302,600 AI-authored commits across 6,299 GitHub repos found AI introduced about 1.5 times as many security issues as it fixed.

None of this means AI coding is a bad idea. Richard Horne, who runs the UK's National Cyber Security Centre, told RSA Conference in March that moving away from "manually produced software that is consistently vulnerable is a huge opportunity".

His condition was that the safeguards get built in from the start. That's what the rest of this post is about.

‍

What we'd do, in order

  1. Give agents your security context. Short, specific guidance in the files each agent reads, naming your real helpers, like the function that scopes queries by org. Our guide to AGENTS.md and Claude Code skills has examples.
  2. Write down your threats. A short threat model tells agents and reviewers what matters most, like tenant data leaks or payment tampering.
  3. Review every pull request for security. Not just the ones that look risky. The review has to understand how your codebase fits together, or it'll wave through the missing access check that looks perfectly normal.
  4. Watch what agents install. Check every new dependency for known vulnerabilities, and keep checking as advisories come out.
  5. Review the whole codebase once. Find out what's already on your default branch, then keep it clean with PR reviews from there.
  6. Keep people on the scary stuff. Auth, payments, permissions and data access still deserve a human's eyes.
  7. Close the loop. When review finds a vulnerability, turn the fix into guidance so the next service doesn't repeat it.

‍

Security context
Agents know your auth and data rules
Agent writes code
Fewer vulnerabilities written
PR security review
Catches what still slips through
Fix or dismissal
Recorded with who and why
Every fix becomes guidance

‍

A quick reference for reviewers

A quick reference for reviewers
CheckWhy agents get it wrong
Records fetched by ID are scoped to the caller's user, team or orgThe ownership rules live outside the file being edited
Role checks match the action, not just signed-in state"Signed in" is the easy check, "admin of this team" is the right one
Queries and commands are parameterizedAgents copy examples that build strings
Secrets come from the environmentTest fixtures and seed scripts collect hard-coded keys
Webhooks verify signatures before writing anythingThe happy path works fine without it
New packages are deliberate and currentAgents install whatever solves the problem
Errors fail closed and don't leak detailsGenerated error handlers love returning the whole stack trace

‍

What doesn't work on its own

Rules files alone don't do it. In OctoBench, a 2026 benchmark from Fudan University, MiniMax and Peking University, the best model followed every instruction in a task only 28% of the time, and Claude Sonnet 4.5 and Gemini 3 Pro managed about 15%.

Asking the agent to review its own work doesn't either, since it shares the blind spots that wrote the code. Pattern-matching scanners flag a lot but miss business logic flaws, because nothing about a missing check looks wrong.

And reviewing only the PRs that look risky stops working when the risky change is a single line of code in a 400-line diff.

‍

How Dam Secure does it

From one fix to every repo
Finding fixed on PR #731
Invoice route was missing the teamId filter
Mon 10:14
Security context updated
AGENTS.md, a Claude Code skill, a Cursor rule and Copilot instructions
Mon 10:31
Rolled out to 14 repos
Reviewed and merged as one pull request per repo
Tue 09:02
Next agent PR scopes by teamId on its own
#760 in acme/reporting, no finding raised
Thu 15:48

‍

Dam Secure covers both sides of the code being written, from one understanding of your codebase.

Managed Security Context writes and maintains the guidance Claude Code, Cursor, Codex and GitHub Copilot read, and teams see 30% fewer vulnerabilities written.

PR Security Reviews check every pull request in under two minutes, with fewer than five findings per PR and the fix included, for a fifth of the cost of a DIY setup.

AI-Native SAST reviews the whole codebase and uncovers 40% more exploitable vulnerabilities than teams' last scanner.

And every fix, dismissal and PR comment feeds back in, so reviews get sharper and agents stop repeating what your team already fixed.

‍

"Dam Secure is far better than what we were getting from Claude Code Security." Sebastian Vaughan, Platform Engineering Manager, Dovetail

‍

FAQ

Is AI-generated code less secure than human-written code?

Not inherently. It's written faster than teams can review it, by agents that don't know your codebase's security rules, and that combination lets more flaws through.

What is vibe coding security?

Securing code written by prompting an AI and accepting what it produces without reading every line of code. Same answer: give the agent context, and review every change before it ships.

Can AI review AI-generated code?

Yes, if it's a separate review that understands your codebase. An agent marking its own homework doesn't count.

Want AI-generated code you can actually trust? Speak to our team.

GET STARTED WITH DAM SECURE

Your Security.
Enforced at AI Speed.

Attackers are already using Mythos-level models to find vulnerabilities. Join the teams putting frontier models on your side first.