What is AI agent unexpected code execution?
AI agent unexpected code execution is an agent running unintended, often attacker-chosen code through interpreters, shells or scripting tools after prompt injection, a malicious document or manipulated tool output; it ranks fifth in OWASP's 2026 agentic risk list.
Unlike a code execution tool, the legitimate component an agent uses for calculation and analysis, this is that component used for an attacker's purpose. Sandbox escape, where running code breaks out of isolation, is often its next step.
- Topic
- AI security
- Also known as
- Unexpected code execution, agent RCE
How AI agent unexpected code execution works
- The attack mostly targets agents with a code tool or shell access.
- An attacker hides code-running instructions in a document, web page or tool output the agent will read.
- Taking the instruction as part of its task, the agent writes the code or runs ready-made code.
- The code can read credentials, exfiltrate data, establish persistent access or spread to the host if isolation is weak.
In more detail
Paths that need no code tool: improper output handling
Unexpected code execution does not require an interactive Python or bash tool: if an agent passes unvalidated LLM output into backend functions using shell=True, eval(), pickle/YAML deserialisation or SQL/template engines, a plain text-generation or lookup tool can still trigger remote code execution (OWASP LLM05: Improper Output Handling). Parameterised APIs and strict schema validation prevent this vector.
Installing packages at run time and slopsquatting
Coding and data-analysis agents frequently install libraries dynamically, creating a supply-chain vector: attackers poison repositories or register package names that LLMs commonly hallucinate ("slopsquatting"). Enforcing pinned, internal allowlisted package mirrors and blocking runtime package installation inside sandboxes reduces the risk of malicious dependency execution.
Separating untrusted reading from code execution
High-assurance architectures separate untrusted data ingestion from code-capable execution via the "dual-LLM" or quarantined-reader pattern. A low-privilege reader model extracts structured fields from external documents first and only validated schema values, never raw external text, reach the planning agent that controls code execution tools.
MCP tool poisoning and tool definitions that change later
Beyond classic indirect prompt injection in documents, MCP servers themselves can embed hidden instructions in tool descriptions ("tool poisoning") or alter definitions after approval ("rug pull"). Pinning MCP tool manifests by cryptographic hash and auditing tool responses helps prevent unexpected code execution triggered by compromised tool metadata.
Commonly confused terms
| Term | How it differs |
|---|---|
| AI agent sandbox escape | Unexpected code execution occurs inside the execution layer when an agent runs attacker-influenced or unintended code; sandbox escape is the subsequent step where that running code breaches the sandbox to hit the host or network. |
| Indirect prompt injection | Prompt injection is the manipulation vector in untrusted text/data; unexpected code execution is the downstream high-severity consequence when injected instructions trigger code generation, shell execution or command injection. |
| Faulty code generation | Hallucinated or buggy code happens when the model generates faulty syntax or wrong logic while trying to solve a legitimate task; unexpected code execution refers specifically to adversary-steered or unintended execution paths that violate security intent. |
Why AI agent unexpected code execution matters for on-premises AI
Agents that can run code are the most powerful and the riskiest, since one instruction becomes a program in a single step. Research shows models connected to tools through MCP can be steered into malicious code execution and credential theft. Controls are isolation with restricted network and file access, no credentials in the environment, approval for runs triggered by external content and logging every run.
While reviewing a supplier spreadsheet, a manufacturer's data analysis agent is steered to run a command hidden in a cell; since code runs with network access off, no credentials and a time limit, nothing leaves and monitoring catches the event.
Common questions
How does unexpected code execution through an agent happen?
Hidden instructions in a document, web page or tool output the agent reads steer it to run the attacker's code.
What is the most important control against it?
Running code in an isolated environment with restricted network and file access and no credentials. Runs triggered by external content also need approval.
Related terms
- Code execution tool
- AI agent sandbox
- AI agent sandbox escape
- Prompt injection
- Indirect prompt injection
- Agent privilege abuse
Sources
- OWASP, Top 10 for Agentic Applications for 2026
- Radosevich and Halloran, MCP Safety Audit: LLMs with the Model Context Protocol Allow Major Security Exploits, 2025
In the glossary: Glossary, A to D