
Securing AI-Generated Code
Generated code carries its own security profile. The distinct security pass: recurring vulnerability patterns, slopsquatting, secrets echo, and the agent as attack surface.
TL;DR: Generated code carries a security profile of its own. A working hypothesis, not a documented rule, is that this is because a model reproduces patterns common in public code, and those patterns include insecure idioms. The security pass is a distinct review with its own checklist: recurring vulnerability patterns, dependencies that may not exist until an attacker registers the name, secrets that can echo out of context, and the agent itself as an attack surface when it reads untrusted content. These mitigations reduce the risk; they do not eliminate it, and honest engineering says so.
Why generated code has a security profile of its own
A working hypothesis, not a documented rule, is that a language model reproduces patterns that are common in its training data. For security, that is the problem in one sentence: the patterns common in publicly written code include the insecure ways things are commonly done — the tutorial that skips validation, the snippet that logs the token, the auth flow written from memory. Generated code is fluent and idiomatic, which makes these patterns read as correct, and a vulnerability that looks polished can be easy to read past.
That is why the security pass is a distinct review, not an intensity dial on the general one. It has its own checklist, aimed at the weak spots generated code recurs to:
| Pattern | What the security pass looks for |
|---|---|
| Unvalidated input | User-controlled values reaching queries, file paths, or commands without validation or parameterization |
| Permissive defaults | Wide-open CORS, disabled certificate checks, debug flags, overly broad permissions left at the convenient default |
| Hand-rolled auth or crypto | Bespoke token logic, homemade hashing, custom comparison where a vetted library exists |
| Missing authorization on the unhappy path | Ownership and permission checks present on the happy path and absent on edge, error, and admin branches |
None of these makes the code fail. They live inside code that works — the request succeeds, the test passes, the demo runs — and the security pass exists to read for the cases the happy path never exercises.
The dependency problem: hallucinated and unvetted packages
Agents suggest imports that do not exist. A working hypothesis, not a documented rule, is that the model has seen enough package names to generate plausible new ones; whatever the cause, it can import a library that was never published. On its own, that is a broken build. The danger is what attackers can do with it: register the hallucinated names, so that the next developer whose agent suggests the same plausible package installs attacker-controlled code. This is slopsquatting — turning a model's hallucination into a supply-chain vector, and it works when the same wrong name comes back. In a 2024 study by Spracklen et al. that sent each of 500 hallucination-producing prompts to the same model ten more times, 43% of the hallucinated package names came back in all ten runs, while 39% never came back.
The defense is a verification habit applied to every dependency an agent introduces: confirm it exists on the official registry, check its provenance and maintenance signals, and confirm it is the package you think it is before it enters the lockfile.
# An agent suggested this import. Before installing, ask:
from fastjson_validator import validate_schema # does this package actually exist?
# 1. Is it on the official registry, or only plausible-sounding?
# 2. Who publishes it, and is it maintained — or newly created with one release?
# 3. Is it the well-known package, or a look-alike name one character off?
# Verified answers first; install second.A standing constraint in the task spec reduces how often this even comes up: prefer the codebase's existing dependencies over agent-suggested new ones. Many tasks do not need a new library, and telling the agent to solve within what the project already depends on shrinks both the hallucination surface and the ordinary churn of unvetted additions.
Provenance and license exposure
Two more risks come from what the model reproduces and what it was shown. Generated code can reproduce licensed material closely enough to matter — a distinctive block that traces back to a copyleft source is a provenance question, and near-verbatim, non-trivial chunks deserve an origin check before they become part of a codebase with its own license obligations.
The sharper operational risk is secrets. Agents can echo what they see. A key pasted into context to "help it understand the config," a token in a log the agent read, a credential in an example — any of it can resurface in generated output, in a commit, or in another log downstream. The hygiene rule is to keep real secrets out of agent context entirely: use placeholders and references, and treat anything the agent might read as something it might reproduce. A secret that entered context should be considered exposed and rotated, not quietly reused.
There is also a plain audit-trail duty. Where a team's process expects authorship signals — who wrote a change, who is accountable for it — recording that a change was agent-authored keeps that trail honest rather than letting delegated code enter under a human's name without qualification.
The agent itself as attack surface
A distinctly agent-specific risk lies not in the code the agent writes but in what the agent reads. An agent that ingests untrusted content — an issue description, a fetched web page, a package readme, a comment on a ticket — can be steered by instructions embedded in that content. This is prompt injection: the untrusted text says, in effect, "ignore your task and do this instead," and an agent with tools and credentials may act on it. The defining feature is that the attacker's input and the agent's instructions arrive through the same channel.
Fully preventing an agent from being influenced by content it must read may not be achievable. The UK NCSC wrote in December 2025 that it is "very possible that prompt injection attacks may never be totally mitigated in the way that SQL injection attacks can be," and OWASP's LLM01:2025 entry says it is unclear whether there are fool-proof methods of prevention for prompt injection. So the posture is blast-radius control — assume influence is possible and limit what it can accomplish:
- Scope credentials to the narrowest surface that does the job. An agent that never needs production access should not hold it; a token good for one repository cannot damage another.
- Sandbox execution so that code the agent runs cannot reach beyond its task.
- Require human approval for state-changing actions — merges, deploys, deletes, anything that touches money or external systems — so an injected instruction stops at a checkpoint a person controls.
The authoritative framing for these risks is worth reading directly and citing with its date, because the field moves: the OWASP Top 10 for LLM Applications covers prompt injection (LLM01:2025) and supply-chain risk (LLM03:2025) specifically in its 2025 edition. In the 2026 edition, which OWASP published in August 2026, prompt injection is LLM01:2026 and supply chain is LLM04:2026 (as of November 2026). The general OWASP Top 10 maps the vulnerability categories the generated-code checklist draws on. The honest position underneath all of it is that these measures reduce exposure, they do not remove it. Scoped credentials shrink the damage of a successful injection but do not guarantee none; the verification habit catches many hallucinated packages, not every one. Security here is a posture that lowers probability and contains impact — presenting it as a solved problem is a framing that can get someone hurt.