Insight / Verifying AI-generated code

How to secure an AI-generated codebase: a step-by-step plan

You have an app that works, written largely by AI. Here is the order we would secure it in, and why.

Swallowtail Systems™  /  Published November 30, 2026  /  3 min read

Key points
  • Start with what is exposed to the internet and what holds data. Fix secrets and access rules before anything else.
  • Research finds AI-assisted and human code both ship vulnerabilities, and that most vulnerable changes merge without any security discussion.
  • Finish by turning every fix into a test, so the next AI-generated change cannot quietly undo it.

Why the order matters

AI coding tools produce working software quickly, and the security evidence is mixed rather than damning. In Veracode's 2025 tests, 45 percent of AI coding tasks produced code with a known flaw 1. A 2026 study of 21,454 agent-written pull requests found vulnerabilities in 11.5 percent, close to the human rate in the same study, and found that almost none of the vulnerable changes received security review 2. The practical conclusion is that the risk is less about who wrote the code and more about whether anyone checked it. With limited time, check the places where a single mistake exposes the most.

Step 1: Inventory what is live

List every deployed app, its address, its hosting, its database and storage, the third-party services it calls and the AI models it uses. Note who can publish changes. Many organizations discover AI-built tools nobody in IT knew about; see Shadow AI is a governance problem first.

Step 2: Find and rotate secrets

Search the code, the history and the browser bundle for keys and tokens. GitGuardian's 2026 research reports that commits co-authored by one popular coding agent leaked secrets at roughly twice the baseline rate 3. Rotate anything found, move secrets server-side, and add secret scanning to every repository.

Step 3: Test access rules with real accounts

Broken access control is first on the OWASP Top 10:2025 4. If the browser talks directly to a hosted database, the rules on each table and storage bucket are the control 5. Test with two ordinary accounts and a signed-out session: can one user read or change another's records? Can someone outside the team join it?

Step 4: Check sign-in, payments and admin paths

Confirm that every sensitive action checks who the user is on the server, every time. Confirm that prices, plans and refunds cannot be changed from the browser. Look for admin routes that are hidden but not protected.

Step 5: Review dependencies and agent tools

Pin versions, remove unused packages and check for known vulnerabilities. If the app or its builders use agents with tools or MCP servers, inventory those too; see The tool layer.

Step 6: Review the AI features themselves

If the app includes AI features, map them against the OWASP Top 10 for LLM Applications 2025 6. The highest-value check is whether any feature combines private data, untrusted content and a way to send data out 7.

Step 7: Make it stick

Turn each fix into a regression test. Add required human review for changes to sign-in, access rules, payments and secrets. Write the secure patterns you settled on into the instructions your AI coding tools use. See The code verification flywheel.

For teams shipping AI-built software

What this means for your team

  1. Inventory first: you cannot secure what you have not listed.
  2. Fix secrets and access rules before cosmetic findings.
  3. Test with accounts that should be refused.
  4. Require human review on the change types that matter most.
  5. Keep every reproduction as a test.

This is Swallowtail Systems™ analysis of public sources. Figures belong to the organizations cited, and we link every one. Nothing here describes a client engagement.

Sources

  1. Veracode, 2025 GenAI Code Security Report
  2. Le, Vijayanandan et al., "On the Security of Agentic Pull Requests in GitHub: An Empirical Study", University of Calgary
  3. GitGuardian, The State of Secrets Sprawl 2026
  4. OWASP Top 10:2025
  5. Supabase documentation, Row Level Security
  6. OWASP Gen AI Security Project, Top 10 for LLM Applications 2025
  7. Simon Willison, "The lethal trifecta for AI agents" (June 16, 2025)

? FAQ

Common questions

Is AI-generated code less secure than human code?

The research is mixed. Controlled tests find many AI coding tasks produce known flaws, while a 2026 study of real pull requests found agent and human vulnerability rates were close. In both cases the bigger problem was that changes were rarely reviewed for security.

Can the AI tool that wrote the code check it?

Built-in scans are a good default and catch real problems. They do not replace an independent review, because the same tool may share the blind spots that produced the flaw and cannot know who should see which data.

How long does it take to secure an AI-built app?

It depends on size and how much data the app touches. The steps above are ordered so that the highest-risk issues are found first, which is why a first pass on secrets and access rules is usually the place to start.

What should we fix first?

Exposed secrets and access rules that let one user see or change another user's data. Those are the findings most likely to lead directly to a data exposure.

/ Next step

Want this read against your own systems?

In a 30-minute briefing we walk through what we would look for in your environment and what a written scope would include.

Request a briefing