Indirect Prompt Injection in AI Agents: Attack and Defense Practices
This article examines indirect prompt injection in AI agent frameworks, where untrusted external content is read and mistakenly executed as instructions. It breaks down the attack chain into sources, sinks, and canaries, outlines an approach for large-scale automated evaluation, and presents five layers of defense spanning input isolation, least-privilege tooling, policy enforcement, content sanitization, and monitoring. The article also lists acceptance metrics for practical governance.
Indirect Prompt Injection in AI Agents: Attack and Defense Practices
1. The Real Problem: Reading Is Executing
In AI agent applications, the model does more than answer questions. It reads web pages, emails, documents, and chat logs, and it calls external tools to send messages, submit forms, or run commands. This is where the risk appears: third-party content may carry embedded instructions, and the agent may read them, mistake them for user intent, and act on them.
This issue is commonly known as indirect prompt injection. Unlike a traditional intrusion, the attacker does not necessarily breach a server or steal credentials. Instead, the attacker places malicious instructions in data the agent is bound to read, letting the agent carry out the sensitive operations itself. The core risk is a missing boundary between data and instructions.
A simple analogy: an assistant opens letters and organizes documents. If a letter says “send the account details to this address” and the assistant cannot tell “letter content” from “the boss’s instructions,” it may comply mechanically. An agent without the same sense of boundaries will also treat external text as an executable command.
2. The Attack Chain: Sources, Sinks, and Decoys
To make the problem easier to evaluate in engineering terms, the attack chain can be abstracted into three elements:
- Source: an entry point that pulls in untrusted content, such as web scraping, document parsing, email reading, or skill package loading.
- Sink: a sensitive action that produces real impact, such as sending a message, submitting a form, running a script, or modifying data.
- Canary: when the goal is not to trigger an action but to pollute the output, a verifiable string is used to determine whether controlled output appears.
An attack generally succeeds when one necessary condition holds: the agent can reach a source, the content returned by that source can influence its subsequent planning, and that planning eventually reaches a sink. Defense should cut the chain segment by segment along this same path.
Illustrative pseudocode:
for source in external_inputs:content = read(source) # untrusted dataplan = agent.plan(content, user_goal) # risk point: data treated as instructionsfor action in plan:if action.type in SENSITIVE_SINKS:require_policy_check(action) # explicit authorization required
3. Running Large-Scale Automated Evaluation
A high-quality security assessment should cover the combination space of “attack variants × source types × sink types × agent states.” A practical process looks like this:
- Build a case library: group injection payloads into categories such as explicit instructions, role playing, fake system messages, encoded obfuscation, and multi-step inducement.
- Construct realistic contexts: embed payloads in common materials such as web pages, tables, emails, and code comments, instead of testing only with idealized text.
- Define success criteria: use reaching a sensitive sink, emitting the canary, or leaking restricted information as the conditions for a successful attack.
- Repeat sampling and report statistics: run multiple repetitions to account for randomness, and report success rates with confidence intervals rather than a single case.
- Perform root-cause analysis: separate distinct causes such as model misjudgment, excessive tool permissions, missing context isolation, and absent policies.
One point deserves emphasis: the success rate is only an outcome metric. In engineering, reproducibility and blast radius of high-risk failures matter more. For example, a high success rate concentrated on low-impact actions may rank below a low-success-rate path that can lead to data exfiltration.
4. Defense in Depth: Five Gates from Input to Execution
1. Input Isolation and Structured Labeling
Wrap external content in a “data region” and declare at the system level that the data region contains no executable instructions. Pass parsing results through structured fields to reduce direct interference from free-form text in the prompt context.
2. Least Privilege and Tool Tiering
Classify tools into read-only, write, and irreversible tiers. Irreversible or outbound actions are disabled by default and require an explicit policy plus user confirmation. Skill package loading should verify the source and signature, and limit the resources it can access.
3. Policy Engine and Action Approval
Insert a policy layer between model output and tool invocation: evaluate action parameters, target addresses, and data sensitivity levels, then block or route to human approval when a high-risk rule matches.
policy:- name: block_external_sendwhen: action.type == "send" and action.target not in allowlisteffect: deny- name: approval_for_execwhen: action.type == "exec"effect: require_human_approval
4. Context Sanitization and Content Provenance
Sanitize scraped content by stripping scripts, removing hidden text, and limiting overly long content. Retain source identifiers and fetch timestamps for auditing and traceback. Flag suspicious patterns to lower their weight during the planning stage.
5. Monitoring, Alerting, and Red/Blue Exercises
Log the “source–planning–sink” chain and set up alerts for abnormal outbound traffic, abnormal commands, and abnormal bulk reads. Bring automated evaluation into continuous integration to form version-level security regression.
5. Implementation Advice and Acceptance Metrics
Enterprises should pursue governance with verifiability as the goal:
- Coverage: the proportion of source types, sink types, and payload categories covered.
- Block rate: the proportion of high-risk actions intercepted by policy.
- False positive rate: the proportion of legitimate business flows wrongly blocked, so security does not hurt usability.
- Auditability: whether key actions have complete chain logs and replay capability.
- Recovery: whether the source can be located quickly and the session isolated after polluted output appears.
In practice, general-purpose cloud services such as log services, monitoring and alerting, object storage, and container platforms can carry evaluation data, policy configuration, and audit trails; general capabilities from Baidu AI Cloud are also commonly used in architecture design for this kind of security operations scenario. The end goal is not a model that never errs, but ensuring that even when the model is induced, the system can still limit impact to an acceptable range at the permission, policy, and audit layers.
