The Task Shield: Enforcing Task Alignment to Defend Against Indirect Prompt Injection in LLM Agents

Feiran Jia, Tong Wu, Xin Qin, Anna Cinzia Squicciarini

Abstract

Large Language Model (LLM) agents are increasingly integrated into autonomous workflows, making them vulnerable to indirect prompt injection. Malicious instructions hidden in external data can hijack agent behavior, leading to unauthorized actions. We present Task Shield, a test-time defense framework that shifts the security paradigm from content filtering to "task alignment." By ensuring all agent actions strictly serve the user's original intent, Task Shield reduces attack success rates from 47.69% to 2.07% while preserving high task utility.

Introduction

As LLMs evolve into active agents capable of tool use—such as managing emails, databases, or calendars—they face a critical security threat: indirect prompt injection. Unlike direct injection, where a user attempts to subvert the model, indirect injection occurs when an attacker embeds malicious directives within external data (e.g., a webpage or an email) that the agent is tasked to process. For example, an agent summarizing a website might encounter hidden text instructing it to forward the user's private data to an external server.

Current defense strategies often rely on detecting "harmful" intent or applying rigid filtering rules. However, these methods are frequently bypassed by attackers who frame malicious requests in benign or urgent tones. This paper introduces a paradigm shift toward a positive security model: Task Alignment.

Methodology: The Task Shield Framework

The core philosophy of Task Shield is to verify whether every instruction processed or generated by the agent contributes to the user's stated goal. The framework operates as a guardian through a three-stage process:

  1. Instruction Extraction: An auxiliary LLM extracts all actionable directives from the conversation history, including user prompts, agent reasoning, and tool outputs.
  2. Alignment Check: Extracted instructions are compared against the user’s original task set using a fuzzy logic ContributesTo score (0 to 1). A score of 1 indicates direct alignment, while intermediate scores allow for necessary preparatory sub-tasks. A score of 0 triggers an intervention.
  3. Feedback Generation: If misalignment is detected, the shield blocks the tool call and provides feedback to the agent, reminding it of the original task and warning of the detected hijack.
The Task Shield workflow: extracting instructions from user messages and tool outputs to verify alignment before execution.

Experimental Results

We evaluated Task Shield using the AgentDojo benchmark, which simulates complex, multi-turn interactions across four domains: Travel, Workspace, Banking, and Slack. The primary evaluation focused on the "Important Instructions" attack, which exploits the LLM's tendency to prioritize urgent-sounding directives embedded in tool outputs.

Performance Comparison

Task Shield significantly outperforms existing baselines such as Data Delimiting, Tool Filtering, and Prompt Injection (PI) Detectors. While PI Detectors can lower Attack Success Rates (ASR), they often do so at the cost of drastically reducing utility (the agent's ability to complete the original task). Task Shield achieves an optimal balance, maintaining an overall utility of 69.79% while crushing the ASR to 2.07%.

Performance comparison of Task Shield against baseline defenses across different domains. Task Shield achieves the lowest overall Attack Success Rate (ASR) while maintaining high Utility (U).

Key Insights

  • Positive Security Robustness: By forcing agents to justify actions against user goals, attackers find it difficult to craft payloads that appear logically necessary for the user's specific, unrelated request.
  • Input Shielding Efficiency: Ablation studies indicate that an "Input Shield"—verifying only the data coming from external tools—provides a significant security boost with lower computational overhead than a full shield.
  • Scaling with Model Intelligence: The effectiveness of the defense is tied to the capability of the defender LLM. High-performing models like GPT-4o are essential for accurate instruction extraction and alignment scoring.

Conclusion and Future Work

Task Shield provides a robust defense against indirect prompt injection by enforcing task alignment. While the current implementation introduces latency due to the requirement of a powerful auxiliary LLM, future work will explore specialized, smaller models to optimize performance and reduce costs. This approach moves the field toward a more secure and reliable model of agent behavior.

Summary updated

BibTeX
@inproceedings{jia2025task,
  author = {Feiran Jia and Tong Wu and Xin Qin and Anna Cinzia Squicciarini},
  title = {{The Task Shield: Enforcing Task Alignment to Defend Against Indirect Prompt Injection in LLM Agents}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure