Test-Time Immunization: A Universal Defense Framework Against Jailbreaks for (Multimodal) Large Language Models
Yongcan Yu, Yanbo Wang, Ran He, Jian Liang
TL;DR
TIM tackles jailbreak vulnerabilities in both text-only and multimodal large language models by introducing Test-time Immunization, a universal defense that evolves during inference. It detects jailbreak attempts with a lightweight gist token and a binary classifier, and when detected, performs safety fine-tuning via a LoRA module using the identified jailbreak instructions, while decoupling detection from defense updates to prevent interference. The framework leverages online test-time adaptation and memory to continually improve both detection and defense, reducing jailbreak success rates with minimal impact on normal queries. Extensive experiments on LLMs and MLLMs show TIM achieves near-zero attack success, low overhead, and robust performance against diverse jailbreak strategies, highlighting its practical potential as a flexible safety mechanism.
Abstract
While (multimodal) large language models (LLMs) have attracted widespread attention due to their exceptional capabilities, they remain vulnerable to jailbreak attacks. Various defense methods are proposed to defend against jailbreak attacks, however, they are often tailored to specific types of jailbreak attacks, limiting their effectiveness against diverse adversarial strategies. For instance, rephrasing-based defenses are effective against text adversarial jailbreaks but fail to counteract image-based attacks. To overcome these limitations, we propose a universal defense framework, termed Test-time IMmunization (TIM), which can adaptively defend against various jailbreak attacks in a self-evolving way. Specifically, TIM initially trains a gist token for efficient detection, which it subsequently applies to detect jailbreak activities during inference. When jailbreak attempts are identified, TIM implements safety fine-tuning using the detected jailbreak instructions paired with refusal answers. Furthermore, to mitigate potential performance degradation in the detector caused by parameter updates during safety fine-tuning, we decouple the fine-tuning process from the detection module. Extensive experiments on both LLMs and multimodal LLMs demonstrate the efficacy of TIM.
