Skip to content
← All posts

How to secure AI-generated code before it reaches production

Coding agents write features in hours. Here is a practical workflow for keeping that code secure and compliant while it is written, rather than finding out at the end.

Sentrasec Engineering

A coding agent can produce in an afternoon what a team used to write in a sprint. Teams are shipping products in weeks that would have taken quarters. The question that has not sped up is the one every serious buyer eventually asks: is this product secure, and is it compliant?

The short answer to "how do I secure AI-generated code" is: put the security checks inside the loop the agent is already working in, triage what they find against your actual application, and keep the evidence. A review at the end cannot keep up with the volume. The rest of this post is the long answer.

AI-generated code fails in familiar ways

Code written by an agent does not invent new classes of vulnerability. It produces the same ones people do, the ones in the OWASP Top 10:

  • Injection. String-built SQL, shell commands assembled from request fields, templates rendered with unescaped input.
  • Secrets in source. An API key pasted into a config file "for now", a .env committed with the first push.
  • Dependencies nobody chose. A package added to solve a small problem, at whatever version the model remembered, with whatever advisories it carries.
  • Copied defaults. Infrastructure definitions with public buckets, wide IAM policies or containers running as root, because the example it learned from did the same.
  • Missing checks. Endpoints that work but never verify who is calling them.

What changes is volume and speed. When most of a codebase is written by an agent, the human reviewer reads less of it, and a security review scheduled before launch arrives with far more to read than anyone can.

Move the checks to where the code is written

The NIST Secure Software Development Framework has said for years that security belongs in every phase of development, not at the end. With coding agents that is no longer a best practice. It is the only version that scales.

In practice, that means four places:

  1. In the agent. The agent should be able to scan its own change and read the findings while it is still working on it. The Model Context Protocol (MCP) makes this possible: a security tool exposes "scan", "list findings" and "get finding" as tools the agent can call.
  2. Before the push. A CLI that runs the same checks on a laptop and in CI, so nothing new appears at review time.
  3. Before deploy. Dependencies, secrets, infrastructure as code and container images checked against current vulnerability data.
  4. Against the running app. Dynamic testing of the deployed application, because some problems only exist at runtime.

The same rules should run in all four places. If the agent's check and the CI check disagree, people stop trusting both.

Triage before you fix

Running more checks earlier produces more findings. If every one of them is handed to the agent as "fix this", the agent spends its time on noise and the codebase churns.

Severity scores do not help much here. A critical in a test fixture and a critical behind a public endpoint carry the same score. What matters is evidence from your application:

  • Is the vulnerable code reachable?
  • Can attacker-controlled input get to it?
  • Does it ship to production, or only run in tests and build tooling?
  • Is there already a control that mitigates it?

Findings that pass those questions need action. The rest can be deferred, with the reason kept on record so the decision can be checked later.

Give the agent a plan, then verify

An agent told "fix CVE-XXXX" will usually do something. Whether it is the right thing is another matter. It works far better with a plan: where to change, what must not change, which tests prove the fix, and what "done" means.

Then verify. After the change is pushed, re-scan that commit and check the tests from the plan. A finding should close because the evidence says it is fixed, not because the agent said so.

Keep the evidence from day one

The first large customer will ask for SOC 2 or ISO 27001. If your scans, triage decisions, fixes and re-scans are already kept as records and mapped to the controls they support, that conversation is about a report. If not, it is about reconstructing months of work from chat history.

Starting on day one costs almost nothing. Starting at the first enterprise deal costs a quarter.

Keep checking after launch

A clean scan is a snapshot. New advisories are published every day against packages you already ship. Re-scan on a schedule against current vulnerability data, and send new findings to the channel that owns the application. That applies twice over to older applications nobody is actively changing: they collect advisories fastest because nobody is updating their dependencies.

A checklist

  • The agent can scan its own changes over MCP.
  • The same checks run in the CLI and in CI.
  • Dependencies, secrets, IaC and containers are checked before deploy.
  • The running application is tested.
  • Findings are triaged from evidence before anyone fixes them.
  • Fixes come from a plan with tests, and are verified by a re-scan.
  • Every step is kept as evidence and mapped to your compliance framework.
  • Scans keep running on a schedule after release.

This is the workflow Sentrasec is built around: it sits with your team and your coding agents through development, not at the end, and tells you how secure and compliant your product is before the market asks. If you want to see it on your own code, book a demo or read how it works for AI-generated code.

How to secure AI-generated code before it reaches production · Sentrasec