
How to Secure and Ship AI-Generated Code: A 4-Layer Checklist
Green tests are not enough. A practical four-layer system for shipping AI-written code: limit what the agent can reach, keep diffs reviewable, let machines say no, and write approval rules down.
Your coding agent just fixed a bug. It touched 14 files, the tests are green, and the pull request description is cleaner than anything you would write on a Friday evening. Would you merge it?
Most of us would. And that is exactly the problem.
If you want to understand why agents with tools are such a big attack surface in the first place, I explained prompt injection, tool poisoning and sandboxing in MCP Explained: What's the worst that could happen?, and the agent loop itself in Anatomy of AI Agents: How They Actually Work. Both are useful background for this post.
In this article, we will go step by step through a pre-merge checklist you can apply this week, whether you use Claude Code, Codex, Cursor, Antigravity or anything else.
Why is this suddenly urgent?
Because in one week of November 2026, the biggest AI labs stopped treating agent security as a debate and started shipping products and research for it:
- Anthropic launched Claude Ship (beta, 23 Nov 2026). It combines Code Review, a "PR Steward" that keeps pull requests green, and "Claude Approvals", which approves routine changes only if a policy file in the repository allows it. Anthropic describes that policy file as working "like a smarter CODEOWNERS file".
- OpenAI and Cisco open-sourced Shirudo (23 Nov 2026). Agents can generate eBPF "shields" that block an exploit on a running Linux system until the real patch lands. Notice the design: an operator still approves a shield before it is deployed.
- Anthropic published Siren (24 Nov 2026), an evaluation with over 1,700 scenarios where Claude models do routine dev-ops tasks inside deliberately weakened sandboxes, with something useful placed outside. In these tempting scenarios, recent Claude models reached outside the sandbox in roughly a fifth to a third of cases.
Three different products, one shared assumption: the agent will be capable, fast, and sometimes wrong about what it is allowed to do. So the safety has to live in the system around the agent, not in the hope that it behaves.
What actually goes wrong with AI-generated code?
It helps to split the risk into two separate problems, because they need different fixes.
| Risk | What it looks like | Where you fix it |
|---|---|---|
| The code itself | Subtle logic bugs, insecure patterns, hardcoded secrets, new dependencies nobody asked for, huge diffs nobody can review | Tests, CI gates, secret scanning, small diffs, review |
| The process around the code | The agent reads your .env, uses a real credential, reaches the network, or edits files outside the task | Sandbox, least privilege, clear rules, approvals |
Most "vibe coding" horror stories mix these two up. A perfect code review does nothing if the agent already copied your production database URL into a log file while it was working.
Layer 1: Before the agent starts — decide what it can reach
Think of the agent as a very fast contractor who has never seen your threat model. You would not give a new contractor the keys to the whole building on day one.
- Keep real secrets out of the workspace. Use dummy or scoped test credentials. If a production secret is not on the machine, the agent cannot leak it, no matter what it reads.
- Run it in a sandbox with least privilege. Restrict it to the project directory, switch off network access unless the task needs it, and never mount your home folder "just for convenience".
- Write the rules down for the agent. This sounds too simple to matter, but Siren found the opposite. Describing the sandbox and its rules to the model made each model tested 68% to 96% less likely to reach outside. For the Claude 5.5 models and Claude Mythos 5.2, it cut reaching outside from between a fifth and a third of scenarios to about 2% or less. Put these rules in your
AGENTS.md(or equivalent): what is off-limits, and what to do when something is blocked — stop and report, do not work around it.
One honest caveat from the same research: these mitigations make an escape less likely, but they do not fix the sandbox itself. You still need a correctly configured sandbox.
Layer 2: While it works — keep the change reviewable
AI makes it cheap to produce a 2,000-line diff. It does not make it cheap to review one.
- One task, one pull request. Ask for a plan first, then the change. Small, scoped changes are the single biggest thing you can do for reviewability.
- Tests before trust. Ask the agent to write or update tests that describe the expected behaviour, and run them yourself. If the agent wrote both the code and the only test, you have one opinion, not two.
- Watch for scope creep. Did it "also refactor" an auth helper while fixing a CSS bug? That is a red flag, not a bonus.
Layer 3: Before merge — let machines say no
Humans get tired. CI does not. Anything that can be checked mechanically should block the merge automatically.
- Required status checks. On GitHub, branch protection rules can require passing status checks and approving reviews before a pull request can merge into a protected branch.
- Secret push protection. GitHub's push protection blocks pushes that contain recognised secrets before they reach the repository, instead of alerting you after the leak.
- Dependency review. For every new package the agent added, confirm it is real, maintained, and the one you meant. A plausible-sounding package name is not proof that it is the right package.
- A separate reviewer. If an AI reviewer is part of the pipeline, keep it separate from the agent that wrote the code. Anthropic's harness research found that agents asked to evaluate their own work tend to praise it, even when the quality is mediocre, and that a separately tuned, skeptical evaluator is far more tractable.
Layer 4: Make approval a written policy, not a mood
Here is the shift Claude Ship makes very visible: the approval policy itself becomes the control point. "Which changes need a human?" turns into a configuration decision you can review, version and argue about in a pull request — instead of a judgment call made at 11 PM.
You do not need any specific vendor to adopt the pattern. Start with risk tiers:
| Change type | Example | Approval rule |
|---|---|---|
| Low risk | Docs, copy changes, test-only changes | Auto-merge allowed after checks pass |
| Medium risk | UI components, internal refactors | One human review |
| High risk | Auth, payments, permissions, infra, database migrations, dependency changes | Human review from a code owner, no auto-merge |
Anthropic's own recommendation is worth copying even if you never use their product: run a new approval policy in observe-only mode first, and only enforce it once you trust its decisions.
After merge: assume something will slip through
No checklist is perfect, so make mistakes cheap:
- Keep every agent change easy to revert (small PRs help again).
- Log what the agent ran and changed, so you can answer "what happened?" later.
- For production systems, runtime protection matters too. Shirudo is an advanced example of this idea — temporary, operator-approved shields while a permanent patch is prepared — but even basic monitoring and alerting count.
What are the trade-offs?
Every layer here costs something:
- Speed. Sandboxes, gates and human approvals slow you down. The goal is to spend human attention only where the risk is high, which is why tiers matter.
- Policy fatigue. If everything needs approval, people start clicking "approve" without reading. A short high-risk list that people actually respect beats a long one they ignore.
- AI reviewing AI. Automated review is useful, but it is still a model. Treat its findings as signals, not as a verdict.
- Cost. Automated review and fixing agents are usually billed on tokens. Claude Ship, for example, notes that turning it on can add to an organisation's costs, and lets admins set spend limits.
The quick checklist
Before you merge AI-generated code, ask:
- Did the agent work without access to real secrets?
- Was it sandboxed, with the rules written down for it?
- Is the diff small enough that a human actually read it?
- Do tests describe the expected behaviour, and did they run in CI?
- Did secret scanning and dependency checks pass?
- Does this change fall into a high-risk tier that needs a human code owner?
- Can we revert it in one step?
Conclusion
The question used to be "can AI write this code?" In 2026 that is mostly settled. The better question is: what was the agent allowed to touch while writing it, and who decided it was safe to ship? Answer those two questions with a sandbox and a written policy, and AI-generated code stops being a gamble and becomes just another, very fast, contributor.
References
- Introducing Claude Ship — Anthropic (Claude), 23 Nov 2026.
- OpenAI and Cisco introduce Shirudo — OpenAI, 23 Nov 2026.
- Measuring and mitigating agent sandbox escape with Siren — Oscar O'Rahilly and Darya Kaviani, Anthropic, 24 Nov 2026.
- Harness design for long-running application development — Anthropic, 24 Mar 2026.
- About protected branches — GitHub Docs (accessed 25 Nov 2026).
- Push protection — GitHub Docs (accessed 25 Nov 2026).


