Test-Time Immunization: A Universal Defense Framework Against Jailbreaks for (Multimodal) Large Language Models

Yongcan Yu, Yanbo Wang, Ran He, Jian Liang · OpenReview

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.

Introduction

Current safety alignments and defense mechanisms for Large Language Models (LLMs) and Multimodal LLMs (MLLMs) are typically static and attack-specific. A defense designed to catch adversarial text prompts often fails against novel strategies, such as image-based typography attacks (e.g., Figstep), where malicious text is embedded within an image. Because attackers constantly invent new jailbreak techniques, it is impossible to anticipate and train against all of them in advance.

TIM's adaptive defense mechanism compared to static pre-guarded models.

TIM addresses the need for a universal, adaptive defense. Rather than building a static wall, TIM acts like a biological immune system: it learns to recognize when it has been tricked and patches its own vulnerabilities in real-time. As shown in Figure 1, while a pre-guarded model might fail against a novel attack (Attack B), TIM detects the successful jailbreak and adaptively guards the model, preventing subsequent similar attacks from succeeding.

The TIM Framework

The Test-time IMmunization (TIM) framework consists of two main decoupled components designed for efficiency and stability during inference.

Efficient Jailbreak Detection via a "Gist Token"

Instead of relying on slow, external proxy models to evaluate outputs, TIM injects a trainable "gist token" at the end of the generated sequence. Leveraging the KV cache, this token efficiently summarizes the harmfulness of the response. A lightweight binary classifier then reads the hidden state of this token to flag if a jailbreak has occurred. This method adds only approximately 0.4% overhead to standard inference time.

Adaptive Defense Training (Test-Time Adaptation)

When a jailbreak is detected, the malicious prompt is paired with a standard refusal response (e.g., "I cannot fulfill this request") and stored in a short-term "jailbreak memory." The model then undergoes rapid, on-the-fly fine-tuning using Low-Rank Adaptation (LoRA). To maintain general performance and prevent over-defense, benign QA pairs are mixed in as regularization during this process.

The TIM framework architecture, highlighting the gist token detection and decoupled defense training.

Decoupling for Stability

A major challenge in test-time learning is that updating model weights can inadvertently degrade the detector's performance. TIM solves this by decoupling the modules: the detector reads from an intermediate transformer layer, while LoRA updates are only applied to the layers following this detection point. This ensures the "immune system" remains functional even as the model adapts.

Experimental Evaluation

Datasets and Attacks

The framework was evaluated using a variety of datasets for both training and testing. Detection training utilized AdvBench, MM-SafetyBench, and clean datasets like GPT4-LLM-Cleaned. Evaluation focused on sophisticated attacks including Figstep (typographic visual prompts) for MLLMs and GCG (Greedy Coordinate Gradient) for LLMs.

Key Results

  • Attack Success Rate (ASR): TIM reduced the ASR of complex attacks like Figstep to near 0% (e.g., 1.4% on LLaVA-v1.6-Vicuna-7B).
  • Immunity: In tests measuring the last 50% of sequences (ASR-50), TIM achieved 0.0% ASR across almost all tested attacks, proving that the model becomes completely immune after initial exposure.
  • Helpfulness: The Over-Defense Rate (ODR) remained low, with the model falsely rejecting normal prompts only 0.0% to 2.3% of the time.
  • Efficiency: Despite the overhead of test-time training, overall average inference time dropped (e.g., from 7.18s to 5.49s) because the model learned to provide short refusals rather than long, harmful responses.

Conclusion and Future Work

TIM demonstrates that detection and adaptation are more effective than preemptive, static defenses. By seeing only a small number of successful jailbreaks, the model can adapt its weights to become immune to specific attack vectors. Future research will focus on scaling TIM to larger models (70B+ parameters) and improving detector performance for extremely long context windows, where performance currently shows some degradation.

Summary updated

BibTeX
@misc{yu2025test,
  author = {Yongcan Yu and Yanbo Wang and Ran He and Jian Liang},
  title = {{Test-Time Immunization: A Universal Defense Framework Against Jailbreaks for (Multimodal) Large Language Models}},
  year = {2025},
  eprint = {2505.22271},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2505.22271},
}
Figure