Seedlabs
Source research

SecAlign: Defending Against Prompt Injection with Preference Optimization

Sizhe Chen, Arman Serikuly Zharmagambetov, Saeed Mahloujifar, Kamalika Chaudhuri, David Wagner, Chuan Guo · 2025 · 366 citationsRead the paper

Large language models (LLMs) are becoming increasingly prevalent in modern software systems, interfacing between the user and the Internet to assist with tasks that require advanced language understanding. To accomplish these tasks, the LLM often uses external data sources such as user documents, web retrieval, results from API calls, etc. This opens up new avenues for attackers to manipulate the LLM via prompt injection. Adversarial prompts can be injected into external data sources to override the system's intended instruction and instead execute a malicious instruction. To mitigate this vulnerability, we propose a new defense called SecAlign based on the technique of preference optimization. Our defense first constructs a preference dataset with prompt-injected inputs, secure outputs (ones that respond to the legitimate instruction), and insecure outputs (ones that respond to the injection). We then perform preference optimization on this dataset to teach the LLM to prefer the secure output over the insecure one. This provides the first known method that reduces the success rates of various prompt injections to <10%, even against attacks much more sophisticated than ones seen during training. This indicates our defense generalizes well against unknown and yet-to-come attacks. Also, SecAlign models are still practical with similar utility to the one before defensive training in our evaluations. Our code is at https://github.com/facebookresearch/SecAlign

What the full text shows

Working prototype
  • Reduced attack success rates of various prompt injections to below 10%, including sophisticated attacks not seen during training. — <10% ASR (Tested on Llama-7B and other open-weight models using AlpacaFarm dataset)
  • SecAlign achieved a significantly larger log probability margin between desirable and undesirable outputs compared to StruQ. — -300 log prob (SecAlign) vs -140 log prob (StruQ) for undesirable outputs (Llama-7B experiments)
  • Maintained general-purpose utility similar to the model before defensive training. — Similar utility to baseline (Evaluated using AlpacaEval2 on AlpacaFarm samples)
Setup
Evaluated on Mistral-7B-Instruct, Llama3-8B-Instruct, Llama-7B, Mistral-7B, and Llama3-8B; used Cleaned Alpaca for training and AlpacaFarm (805 samples, 208 with data parts) for evaluation.
How
The authors proposed SecAlign, which treats prompt injection defense as a preference optimization problem. They constructed a preference dataset containing prompt-injected inputs with paired secure (desirable) and insecure (undesirable) outputs, then applied Direct Preference Optimization (DPO) to train the model to prefer secure responses.
Limitations
Requires explicit separation of instruction and data parts via delimiters. · Cannot achieve 100% security and may be vulnerable to multi-turn conversation injections. · Does not defend against jailbreaks or data extraction. · Most effective when injections are at the end of the data.

Summarized by Gemma 4 from the open-access full text · 19 Nov 2026. Read it