MetaDefense: Defending Finetuning-based Jailbreak Attack Before and During Generation

Weisen Jiang, S. J. Pan
10/1/2025
13 citations

Official code: ws-jiang/MetaDefense (8 stars, Python, Apache-2.0, last commit 2025-10-10).

Abstract

This paper introduces MetaDefense, a novel framework for defending against finetuning-based jailbreak attacks in large language models (LLMs). We observe that existing defense mechanisms fail to generalize to harmful queries disguised by unseen attack templates, despite LLMs being capable of distinguishing disguised harmful queries in the embedding space.

Show full abstract

Based on these insights, we propose a two-stage defense approach: (i) pre-generation defense that detects harmful queries before response generation begins, and (ii) mid-generation defense that monitors partial responses during generation to prevent outputting more harmful content. Our MetaDefense trains the LLM to predict the harmfulness of both queries and partial responses using specialized prompts, enabling early termination of potentially harmful interactions. Extensive experiments across multiple LLM architectures (LLaMA-2-7B, Qwen-2.5-3B-Instruct, and LLaMA-3.2-3B-Instruct) demonstrate that MetaDefense significantly outperforms existing defense mechanisms, achieving robust defense against harmful queries with seen and unseen attack templates while maintaining competitive performance on benign tasks. Code is available at https://github.com/ws-jiang/MetaDefense.

View on arXivView PDF

Code Implementations(1)

Apache-2.0

Official Implementation of MetaDefense (NeurIPS 2025)

82Python, ShellOct 6, 20251 years agoApache-2.0

Similar Papers

Cite this paper

@article{jiang2025metadefense,
  title  = {MetaDefense: Defending Finetuning-based Jailbreak Attack Before and During Generation},
  author = {Weisen Jiang and S. J. Pan},
  year   = {2025},
  eprint = {2510.07835},
  archivePrefix = {arXiv},
  doi    = {10.48550/arXiv.2510.07835},
  url    = {https://doi.org/10.48550/arXiv.2510.07835},
  journal = {Neural Information Processing Systems}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2510.07835.svg)](https://codeofpaper.com/paper/2510.07835)

Discussion