Coding agents copy the vulnerabilities already in your code base
An ETH Zurich study found coding agents reuse insecure patterns from nearby code, and often recognise the flaw when asked. What it means for review.
Coding agents read a lot of your repository before they write anything. That is what makes them useful: they pick up your conventions, helpers and structure. A study from ETH Zurich, posted to arXiv in November 2026, measures the other side of that habit. When the code around a task is insecure, agents write insecure code more often, and when asked afterwards, they usually say they were following the existing code. For teams that let agents write a growing share of their software, old vulnerabilities stop being static debt and start acting as templates.
What the researchers tested
The authors, Chenhao Sun, Niels Mündler-Sasahara, Mark Vero and Martin Vechev, built two experiments that give an agent exactly the same task and change only the security of the code around it.
- Backend generation. 96 tasks (24 scenarios, each in Python Flask, Go net/http, JavaScript Express and Rust Actix), adapted from the AutoBaxBench benchmark. The agent implements a new backend application in a repository that already contains other, functionally unrelated applications. Those neighbours come in a secure and an insecure version, covering seven weakness classes including path traversal (CWE-22), OS command injection (CWE-78) and SQL injection (CWE-89). End-to-end exploits then check the result.
- Real C/C++ repositories. 20 pairs of tasks from five widely used open-source projects, taken from the CyberGym data set. The agent fills in a masked region at the site of a known memory-safety bug, such as a missing bounds check of the kind catalogued as an out-of-bounds write. In one task of each pair, similar code elsewhere in the repository carries the security fix; in the other, it doesn't. A proof-of-concept exploit tells whether the bug came back.
The agents were real harnesses with their recommended settings: Claude Code 2.1.228 running Claude Opus 5 and Claude Opus 5.5, Codex 0.147.0 running GPT-5.6 Sol and GPT-6 Astra, and GLM 5.3. Only GLM 5.3, Opus 5 and GPT-5.6 Sol were run on both experiments; Opus 5.5 and GPT-6 Astra were added for the C/C++ tasks.
What they found
Every agent wrote less secure code when the surrounding code was insecure, in both settings.
- Backend tasks. Compared with writing the application with no neighbours, insecure neighbours lowered the share of correct and secure implementations by 3.4 percentage points on average (from 2.1 for Opus 5 to 4.5 for GLM 5.3). Secure neighbours raised it by 4.4 points on average. The gap between secure and insecure context averaged 7.8 points.
- C/C++ tasks. The effect was much larger. The share of secure implementations was 35.4 points lower on average with insecure analogous code than with secure code, ranging from 25.0 to 49.0 points across the five models. The largest drop was GPT-5.6 Sol, from 91.0% secure with secure context to 42.0% with insecure context.
The difference between the two settings makes sense. A backend neighbour is unrelated code the agent happens to see; in the C/C++ tasks, the agent has to lean on similar code to implement the function at all. The authors also found that insecure neighbours whose weakness class didn't apply to the task made little consistent difference. Agents copy patterns that fit what they are writing, not every flaw they read.
Prompting moves the numbers, but doesn't fix them
Two common prompt habits were tested. Asking the agent to follow the repository's existing conventions made propagation worse in both directions: secure code helped more and insecure code hurt more. For GLM 5.3 on backend tasks, the drop under insecure neighbours doubled from 4.5 to 9.0 points. A generic security reminder, or an explicit warning to be careful with existing code, reduced the effect but never removed it. With the security hint, GPT-5.6 Sol reached 100% secure implementations with secure context, but only 68.0% with insecure context.
The agents often knew
The most uncomfortable result is in the follow-up questions. For vulnerable C/C++ implementations, the researchers asked the agent, in a separate branch of the conversation, to review its code for bugs. The three agents analysed this way (GLM 5.3, Opus 5.5 and GPT-6 Astra) identified the vulnerability they had just introduced in 69.9% of cases on average, from 64.2% for Opus 5.5 to 75.8% for GPT-6 Astra. Asked instead why they had implemented it that way, they attributed the choice to following another code site with a similar flaw in 76.5% of cases.
Yet they rarely said anything unprompted. In their final responses, agents warned about the vulnerability in 11.8% of vulnerable implementations for Opus 5.5, 5.9% for Opus 5, and never for GPT-5.6 Sol, GLM 5.3 or GPT-6 Astra.
Reading the results carefully
A few limits are worth keeping in mind before quoting the numbers.
- It is a controlled study. The backend repositories are synthetic, and the C/C++ set is 20 task pairs. The effect is consistent across every agent tested, but the exact sizes will differ in your code.
- The C/C++ projects are public. The authors checked whether agents tried to retrieve or recall the projects' original code, and report it in an appendix, but training-data exposure can't be ruled out entirely.
- The harness versions are already old. Claude Code and Codex release often, and harness defaults are one of the levers the paper says matter.
- "Points" are differences in rates. The paper reports them as percentages; we write them as percentage points to make clear they are differences between two shares.
None of that weakens the central finding: in the same task, the security of existing code changed the security of new code, for every model the authors tried.
What it means for engineering and security leaders
The usual mental model is that each agent change is reviewed on its own merits. This study suggests the quality of the existing code base is an input to every future change. An unfixed injection flaw in one service is not just one vulnerability; it raises the odds that the next similar endpoint an agent writes repeats it, and agents that are told to match house style are the most likely to do so.
It also changes what review should look for. A reviewer who sees new code that matches a familiar pattern in the repository is inclined to approve it. That familiarity is exactly what the agents exploit, without any malice. The paper's finding that agents can recognise the bug when asked suggests a cheap extra step: ask.
The authors' own recommendations split three ways. Model providers should train for secure choices when conventions conflict with them. Harness developers should avoid defaults that push blind consistency and add a review stage after implementation. Developers should add simple security reminders to task prompts and, above all, fix existing vulnerabilities before delegating new work in that area.
Practical takeaways
- Fix known flaws before you delegate nearby work. If a scanner or audit has flagged a pattern in a module, clear it before an agent writes more code there.
- Be careful with "follow existing conventions". Keep style instructions in
AGENTS.mdorCLAUDE.mdspecific (naming, layout, test structure) rather than a general instruction to imitate surrounding code. - Add a security line to task prompts. It won't stop propagation, but in this study it reduced it for every agent.
- Make self-review a step, not an afterthought. Before accepting a change, ask the agent in a fresh turn to review its own diff for vulnerabilities. In the study, agents found their own bug most of the time when asked.
- Review for copied patterns. When an agent's change mirrors existing code, check that the original was safe rather than treating the similarity as reassurance.
- Track repeat findings. If the same weakness class keeps reappearing in agent-written changes, look for the template it is being copied from.
Related reading: where the review bottleneck sits when agents write more code.