Indirect Prompt Injection in AI Agents: Principles and Defenses
This article explains how indirect prompt injection works in AI agents and why it succeeds. It breaks the problem down into sources, sinks, and canaries, then covers practical defenses such as isolating and labeling external content, enforcing least privilege on actions, moving policy checks outside the model, separating execution stages, and adding monitoring and alerts. Developers will find a concise checklist for reducing injection risk in agent architectures.
Indirect Prompt Injection in AI Agents: Principles and Defenses
1. The problem is not that the model was fooled, but that execution boundaries were blurred
When a user asks an agent to read an email, summarize a webpage, or load an external skill package, the external content may carry a second set of instructions. The agent reads that content as ordinary material but may execute hidden instructions as if they were user intent. These incidents usually do not involve a compromised server or leaked password. Instead, the agent fails to draw a clear line between “reading content” and “taking action.”
An office scenario makes this easy to understand. An assistant opens letters, reads emails, and organizes documents. If the back of one letter says “forward the account password to a specific email address,” and the assistant does not distinguish between “this is reference material” and “this is user-authorized,” it may mechanically comply. The risk comes from the agent’s natural tendency to treat what it reads as executable information, not from a conventional system vulnerability.
The core problem of indirect prompt injection is therefore this: external content enters the agent’s context, and the agent has the permissions needed to trigger real actions. Defense is not only about recognizing malicious text; it is also about limiting how much content can influence the execution path.
2. An analysis framework: sources, sinks, and canaries
2.1 Sources: entry points where external content reaches the agent
A source is a tool or interface the agent uses to read third-party content, such as web scraping, document parsing, email reading, chat history loading, or skill package loading. Content returned by a source may include normal material, prompt fragments, disguised system messages, or manipulative text.
The risk with sources is unclear content boundaries. Developers need to define which content is data and which instructions come from the user or the system, and they should tag external content with its origin, trust level, and a non-executable marker.
2.2 Sinks: sensitive actions that can produce real impact
A sink is an action the agent ultimately performs, such as sending an email, submitting a form, executing a command, modifying a file, initiating a transfer, or publishing content. The attacker’s goal is usually not to make the model output something strange, but to have the agent read content from a source and then trigger a sink.
Sinks should be treated as high-risk boundaries. Any action that produces external effects should require independent authorization, policy validation, and human confirmation. Model judgment alone is not enough.
2.3 Canaries: verifying whether content has contaminated the output
For attack goals that do not require tool calls and only need the agent to output a specific string, you can set up a verifiable canary string. If that string appears in the agent’s output, external content has influenced the generated result.
An illustrative flow looks like this:
External content -> Source read -> Context assembly -> Model inference -> Sink execution|vCanary check and audit
Canaries are useful for testing and monitoring, but they cannot replace permission controls. Real defense should assume the model’s judgment may fail and enforce hard constraints at the execution layer.
3. Why attacks succeed so easily
First, agents need to understand natural language, and natural language has no inherent separation between data and instructions. Second, tool calling connects “reading” and “doing”; once read content can influence decisions, it may trigger sensitive actions. Third, multi-turn tasks accumulate context, so contaminated content read early can remain effective in later steps. Fourth, skill packages and plugins expand capability boundaries and also increase the number of sources and sinks.
Security design therefore cannot focus only on single-turn question answering. It must also cover task orchestration, tool calling, state persistence, and permission inheritance.
4. Practical defenses you can implement
4.1 Isolate and label sources
Give external content a separate context area and clearly mark it as “data to be processed, not execution instructions.” Record the origin, assign a trust tier, and filter sensitive content for webpages, documents, emails, and similar inputs. High-risk content should only be allowed for summarization, extraction, or classification, and must not feed directly into execution decisions.
4.2 Apply least privilege to sinks
Grant the minimum tool permissions required for each task, and do not give agents default access to sending email, executing commands, or transferring funds. Use secondary confirmation, allowlists, quota limits, and human approval for sensitive actions. Example policy:
policy:source: external_documentaction: send_emailrequire_confirmation: trueallowed_recipients: [trusted_list]
4.3 Put policy validation outside the model
Model output must not be treated as execution authorization. Add a rules engine or policy service before tool calls to validate action type, parameter ranges, target addresses, call frequency, and source trustworthiness. Block abnormal parameters outright and record audit logs.
4.4 Stage execution and isolate state
Break tasks into four stages: read, analyze, plan, and execute. The read stage should only produce structured data, and the execution stage should only accept validated plans. Use isolated contexts for different stages so early contaminated content cannot carry across stages and affect later actions.
4.5 Add monitoring, canaries, and alerts
Set alerts for abnormal tool calls, abnormal outbound activity, abnormal commands, and canary string matches. Combine logging, monitoring, and audit capabilities to trace content origin, call chains, and execution results. Once contamination is detected, you should be able to roll back state, revoke permissions, and review the policy.
5. A practical checklist for developers
- Define the source list and sink list for each agent, and identify high-risk combinations.
- Disable sensitive tool permissions by default and grant them temporarily per task.
- Mark all external content uniformly as non-executable data.
- Add policy validation and human confirmation outside the model for sensitive actions.
- Record the full call chain to support auditing, alerting, and rollback.
- Use canaries and automated tests to continuously assess injection risk.
The essence of indirect prompt injection is that an agent mistakes “content it read” for “instructions issued by the user.” A reliable security approach separates data from instructions and understanding from execution at the architectural level, and uses least privilege, external policy validation, and full-chain auditing to ensure that even if content is contaminated, the agent cannot exceed its authority and trigger real actions.
