Jailbreaking in the Haystack
Rishi Rajesh Shah, Chen Henry Wu, Shashwat Saxena, Ziqian Zhong, Alexander Robey, Aditi Raghunathan
TL;DR
This work addresses safety risks arising from expanding context windows in transformer-based LLMs by introducing the NINJA jailbreak, which embeds a harmful goal inside a long, benign, thematically related context. The key insight is that goal positioning within the context—especially placing the dangerous instruction at the beginning—can dramatically increase attack success while the context remains unobtrusive, revealing a structural vulnerability in long-context processing. The authors formulate a compute-aware scaling law, showing that under a fixed compute budget, longer contexts can be more effective than increasing attack attempts, and demonstrate this across multiple models (e.g., LLaMA-3.1-8B-Instruct, Qwen2.5-7B-Instruct, Mistral-7B-v0.3, Gemini 2.0 Flash) with ASR improvements up to near 60%. The findings imply urgent need for defenses that consider context structure and positional biases, not just content filtering, to safeguard future long-context and agentic systems.
Abstract
Recent advances in long-context language models (LMs) have enabled million-token inputs, expanding their capabilities across complex tasks like computer-use agents. Yet, the safety implications of these extended contexts remain unclear. To bridge this gap, we introduce NINJA (short for Needle-in-haystack jailbreak attack), a method that jailbreaks aligned LMs by appending benign, model-generated content to harmful user goals. Critical to our method is the observation that the position of harmful goals play an important role in safety. Experiments on standard safety benchmark, HarmBench, show that NINJA significantly increases attack success rates across state-of-the-art open and proprietary models, including LLaMA, Qwen, Mistral, and Gemini. Unlike prior jailbreaking methods, our approach is low-resource, transferable, and less detectable. Moreover, we show that NINJA is compute-optimal -- under a fixed compute budget, increasing context length can outperform increasing the number of trials in best-of-N jailbreak. These findings reveal that even benign long contexts -- when crafted with careful goal positioning -- introduce fundamental vulnerabilities in modern LMs.
