Abstract
Large language models (LLMs) have demonstrated impressive performance and have come to dominate the field of natural language processing (NLP) across various tasks. However, due to their strong instruction-following capabilities and inability to distinguish between instructions and data content, LLMs are vulnerable to prompt injection attacks. These attacks manipulate LLMs into deviating from the original input instructions and executing maliciously injected instructions within data content, such as web documents retrieved from search engines. Existing defense methods, including prompt-engineering and fine-tuning approaches, typically instruct models to follow the original input instructions while suppressing their tendencies to execute injected instructions. However, our experiments reveal that suppressing instruction-following tendencies is challenging. Through analyzing failure cases, we observe that although LLMs tend to respond to any recognized instructions, they are aware of which specific instructions they are executing and can correctly reference them within the original prompt. Motivated by these findings, we propose a novel defense method that leverages, rather than suppresses, the instruction-following abilities of LLMs. Our approach prompts LLMs to generate responses that include both answers and their corresponding instruction references. Based on these references, we filter out answers not associated with the original input instructions. Comprehensive experiments demonstrate that our method outperforms prompt-engineering baselines and achieves performance comparable to fine-tuning methods, reducing the attack success rate (ASR) to 0 percent in some scenarios. Moreover, our approach has minimal impact on overall utility.
Introduction
The core strength of Large Language Models (LLMs)—their ability to follow complex instructions—is also their greatest security weakness. LLMs often struggle to distinguish between a developer's system instructions and "data content" (like a web page) that might contain malicious commands. This leads to prompt injection attacks, where an attacker hijacks the model's output to perform unauthorized actions, leak data, or spread phishing links.
Existing defenses typically try to "suppress" the model's urge to follow injected instructions through reminders or fine-tuning. However, the authors argue that suppression is a losing battle because it fights the model's fundamental nature. Analysis of failure cases reveals that even when a model is successfully attacked, it often "knows" it is following a secondary instruction, sometimes even prefacing its response with phrases like "For the second instruction..."
Methodology: Defense with Instruction Reference
Instead of suppressing the model's instruction-following tendency, this research proposes a defense-in-depth strategy called Defense with Instruction Reference. This approach leverages the model's awareness of context by requiring it to explicitly tag and reference the instructions it executes.
The pipeline consists of three main steps:
- Tagging and Splitting: The input (both the original instruction and the retrieved data) is split into distinct lines. Each line is prefixed with a unique tag, such as
[L 1],[L 2], etc. The original user instruction is always placed at[L 1]. - Prompting and Response Generation: The system prompt is modified to instruct the LLM to follow a specific algorithm: identify the tag of the instruction it is about to execute, reiterate that instruction, provide the answer, and conclude with a special
[end]token. This forces the model to be "self-aware" of which part of the input it is treating as a command. - Filtering: A simple programmatic filter processes the LLM's output. Any response block not associated with the tag
[L 1]is discarded.
To ensure the model adheres to this structured output format, the authors use In-Context Learning (ICL), providing examples of the desired behavior within the prompt.
Experimental Results
The method was evaluated across several benchmarks including direct injection (AlpacaFarm) and indirect injection (Inj-SQuAD and Inj-TriviaQA). Malicious instructions included phishing, advertising, and propaganda. Utility was also evaluated using standard SQuAD, TriviaQA, and SST2 datasets to ensure the defense did not degrade general performance.
Security and Robustness
In direct injection scenarios, the method reduced the Attack Success Rate (ASR) to 0% for several models against common attacks like "Ignore" or "Fake Completion". It significantly outperformed prompt-engineering baselines like "Sandwich" or "Spotlight" and proved comparable to or better than specialized fine-tuning methods like StruQ.
Utility Preservation
Unlike fine-tuning, which often degrades general performance, this method had a minimal impact. The average accuracy drop across standard tasks was only 1.53%, compared to over 5% for fine-tuning approaches. This suggests that the structured output requirement does not significantly hinder the model's reasoning capabilities.
Conclusion
By turning the LLM's output into a structured list of (Tag, Instruction, Response) tuples, the security problem shifts from a complex natural language understanding task to a simple string-matching task for the filter. While there is a slight token overhead and potential sensitivity to formatting in smaller models, the approach effectively leverages the inherent capabilities of LLMs to provide a robust defense against prompt injection. Future work will explore making the tagging mechanism more resilient to adaptive attacks that might attempt to manipulate the tagging system itself.
Summary updated