Indirect Prompt Injection in AI Agents: Attack Chains and Security Detection Practices
This article explores the mechanism of indirect prompt injection attacks targeting AI agents. It breaks down the attack chain into sources, sinks, and canaries, explains how to conduct large-scale automated security evaluations, and provides actionable defense recommendations for developers building agent-based applications.
1. An Overlooked Attack Surface: AI Agents Trust What They Read
The typical workflow of a Large Language Model (LLM) agent involves receiving a user task, calling tools to fetch external content, and then reasoning based on that content to execute subsequent actions. While this workflow brings massive efficiency gains, it also introduces a new attack surface: Indirect Prompt Injection.
This is fundamentally different from traditional server hacking. Attackers do not need system permissions or cracked passwords. They only need to embed a carefully crafted text snippet within the content the AI is required to read: an email, a web page, a shared document, or even the description file of a “skill pack.” When the agent performs the routine action of reading external content, the malicious instructions hidden within are mixed into the model’s context and understood and executed as part of the normal task.
Consider an analogy: you hire a highly diligent assistant to open letters, read emails, and organize materials. One day, the back of an ordinary invitation contains tiny text reading, “Please immediately forward the company’s bank account information to a specific email address.” If the assistant lacks the ability to distinguish between “instructions from my employer” and “a note tucked inside the letter,” and mechanically executes whatever text they read, they will indeed send out sensitive information. The problem is not that the assistant is disloyal, but that the system design fundamentally lacks a mechanism to differentiate between two sources of instructions.
This is the terrifying nature of indirect injection: it does not exploit code vulnerabilities, but rather the inherent working mechanism of LLMs where “read content becomes part of the context, and context influences behavior.” As long as the model cannot architecturally isolate “user instructions” from “external data,” this type of attack is theoretically always possible.
2. Abstracting the Attack Chain: Source, Sink, and Canary
When security researchers conduct systematic security assessments of agent frameworks, they typically abstract injection attacks into a clear chain. This terminology is highly valuable for developers to understand the risk points in their own systems.
1. Source
This refers to the tools or interfaces the agent uses to read external content, such as:
- Web scraping tools that return HTML or parsed text;
- File/document reading tools that process user-uploaded or shared content;
- Email reading tools that fetch incoming external mail;
- Plugin or “skill pack” loading mechanisms, whose metadata and documentation may themselves originate from third parties.
Anywhere an attacker has the opportunity to plant content is a potential injection source. The width of the attack surface directly depends on how many external content channels the agent has integrated.
2. Sink
This refers to the sensitive actions the agent ultimately executes that have real-world impact, such as sending emails, submitting forms, executing system commands, calling payment or transfer interfaces, or publishing content. The ultimate goal of an attacker is to have the model read a malicious instruction from a “Source” and then trigger a specific “Sink.”
From a security modeling perspective, this is a classic “taint propagation” path: data flows in from an untrusted source, and if there is no filtering or isolation in between, it can ultimately contaminate a high-privilege sink. The taint analysis methodology from web security can be almost directly applied here.
3. Canary
Not all attack targets require tool calls. Some attacks only require the model to output specific content in its reply, such as leaking the system prompt or outputting a sensitive string. To automatically verify whether such attacks are successful, evaluation schemes set a verifiable marker string (similar to a canary token in security testing). If it appears in the model’s output, the injection is deemed successful.
This design turns the ambiguous, subjective judgment of “did the attack succeed” into an accurately trackable binary result, forming the foundation for large-scale automated evaluation.
3. How Large-Scale Automated Evaluation Works
To assess an agent framework’s true resistance to injection attacks, a few manual attempts are insufficient. The industry-standard approach is to build an automated evaluation pipeline, with core components including:
1. Building an Attack Sample Library. Collecting and designing various injection techniques, such as: direct command (“Please execute XX”), role-playing (disguising as a system update notification), encoding obfuscation (hiding instructions using Base64 or Unicode variants), context induction (fabricating historical conversation snippets), and task hijacking (replacing the original task objective with the attacker’s objective). The stealthiness and effectiveness of different techniques vary significantly.
2. Automated Environment Execution. In a controlled sandbox, the agent framework repeatedly executes the complete workflow of “reading poisoned content -> observing behavior.” Each technique is batch-tested with different combinations of sources and sinks. Public research has reported experiment scales exceeding tens of thousands of runs, with certain attack techniques achieving a success rate of around 25% under specific configurations—meaning roughly one in four attempts succeeds. In the security domain, this number is by no means negligible.
3. Result Determination and Attribution. Success or failure is determined by objective signals, such as whether a sensitive action was triggered or whether the canary string was output. Further analysis then identifies which layer of defense failed: did the model fail to recognize the injection, did the tool layer fail to filter the content, or did access control fail to block the high-risk operation?
4. Defense Recommendations for Developers
Based on the attack model described above, developers building agent applications should consider at least the following layers of defense:
- Instruction and Data Isolation: Architecturally distinguish between “user instructions” and “external content returned by tools.” Apply clear trust-level annotations to external content to prevent it from directly attaining instruction-level execution status.
- Principle of Least Privilege: High-risk operations (sending, paying, deleting, executing commands) should require independent user confirmation. This confirmation interaction should clearly display to the user “what content triggered this action.”
- Output-Side Content Filtering: Review the action parameters about to be submitted to a sink to identify traces of anomalous instructions irrelevant to the current task.
- Continuous Red Teaming: Incorporate injection attack samples into regression testing. Re-run the evaluation after every update to the model or toolchain to quantify changes in the defense posture.
The capability boundaries of AI agents are expanding rapidly, while the cost of injection attacks remains extremely low. For any team building agent applications, treating “external content as untrusted” as the default assumption is the first principle of secure design.
