
How We Keep AI-Written Code Secure: Defense in Depth for the Age of Generated Code
PentaTrail Dev Team, Pentacon Research, Inc.···14 min read
Contents
You can't keep AI-written code safe by "being careful."
Hand the work to an AI and the code grows several times faster than a person writes it, and the holes grow at the same pace: a permission left unclosed, a secret left sitting in the code, an input check that never got written. Once you've taken the speed, one person reviewing by eye no longer keeps up.
Appendix: what each layer does, and what it stops
Table 1: the six layers
| Layer | What it does | What it stops |
|---|---|---|
| Perimeter | Sifts out bots and suspicious automation at the edge | Indiscriminate scanning, automated bulk access |
| Delegated auth | Delegates the core to passkeys and external login | Phishing, password leaks, session fixation, token forgery |
| Data boundary | Binds every row to its owner, closes default-open grants | Tenant crossing, privilege escalation |
| Coding standards | Feeds pre-agreed standards in on every pass | Injection, XSS, embedded secrets, disguised files |
| CI gatekeeper | Runs a bundle of machine checks, blocks until all green | Regression, misread authorization, supply chain, committed keys |
| Two-track review | Layers a different AI lineage, with a person at the end | Defects a single lineage misses in unison |
Table 2: the checks running in CI
| Check | What it stops |
|---|---|
| Secret scanning | Intrusion via a key or token committed by mistake |
| Permission gate | Elevated-function and anonymous-access holes reaching a merge |
| Permission tests | A sealed hole reopening through a later change |
| Integration tests | Behaviour, including auth and permissions, being misread |
| Types, build, lint | Deviation from the standards |
| Dependency gate | Pulling in a freshly published malicious version via auto-update |
Table 3: the standards decided before writing
| Standard | What it stops |
|---|---|
| Validate input against an allowlist, parameterize queries | SQL injection, malformed values |
| Escape values rendered to the screen | Cross-site scripting |
| Keep secrets out of code, in a key vault | Keys leaking along with the source |
| Check extension and content type on uploads | Disguised files getting in |
Visualize your attack surface with PentaTrail CTEM/ASM
From discovery to vulnerability validation and remediation — all powered by the CTEM framework.
Get Started


