SAFEGuard: Detect Optimization-Based Jailbreak Attacks Through Harmful Semantic Analysis and Fluency Measurement

Quoc Viet Vo, Trung Le, Damith C. Ranasinghe, Ehsan AbbasnejadarXiv 2026

TL;DR

Large Language Models (LLMs) remain highly vulnerable to optimization-based jailbreak attacks that bypass safety guardrails using either highly fluent text or gibberish token suffixes. This paper introduces SAFEGuard, a training-free defense framework that combines a novel hybrid fluency measure with gradient-based semantic analysis to eliminate the blind spots of single-metric detectors. By evaluating both linguistic naturalness and malicious intent, SAFEGuard achieves near-perfect detection accuracy across diverse attack strategies while maintaining low benign refusal rates.

What Problem Does This Paper Address?

Aligned LLMs use safety guardrails to refuse harmful requests, but adversarial "jailbreak" attacks can systematically circumvent these boundaries. Optimization-based jailbreaks generally fall into two categories:

  1. Low-fluency attacks that append non-human-readable gibberish tokens (e.g., CGC, Adaptive) to manipulate the model's internal states.
  2. High-fluency attacks that optimize for stealth and natural language (e.g., COLDAttack, PiF) to blend in with benign prompts.

Existing defenses are specialized and brittle. Fluency-based filters (like Perplexity) easily catch gibberish but fail against fluent attacks, and they frequently misclassify rare but benign human prompts as anomalies. Conversely, semantic defenses (like gradient matching) excel at identifying fluent malicious intent but fail against gibberish suffixes because the random tokens cause the prompt's internal representation to drift away from known "harmful" clusters.

Figure 1: Llama-2-7B-Chat. a) Distributions of perplex- ity between benign and jailbreak prompts created by PiF and COLDAttack are substantially overlapped. b) Anomalies from benign prompts undermine the effectiveness of perplexity to distinguish benign prompts from jailbreak prompts (i.e. Au- toDAN and BEAST). Similar observations for Mistral-7B- Instruct in Appendix C.

The authors address this fundamental trade-off by designing a unified, two-stage framework that covers both defensive blind spots.

Methods

SAFEGuard operates as a two-stage, training-free cascaded detection pipeline:

  1. Hybrid Fluency Measurement (Stage 1): Instead of relying solely on token-level perplexity (PPL), SAFEGuard introduces a metric combining PPL with cross-layer distribution distance using Jensen-Shannon Divergence (JSD). This measures how token prediction distributions evolve from early layers to the final layer. Because optimization-based attacks often alter only a small fragment of a prompt, fluency is computed over sliding subsequences (windows) to detect localized token injections.
  2. Harmful Semantic Analysis via Gradient Matching (Stage 2): Prompts that pass the fluency check are evaluated for malicious intent. SAFEGuard computes the gradient of the prompt against safety-critical parameters ($\theta_s$) and checks its directional similarity to an "unsafe gradient reference" derived from known harmful prompts.

The paper also introduces SAFEGuard+, a threshold-free variant that trains a simple logistic regression classifier on these two scores using a small calibration dataset, eliminating the need for manual threshold tuning.

Datasets

  • Benign Prompts ($D_{base}$ and $D_{test}$): 500 diverse benign prompts sampled evenly (100 each) from AdvBench, TFQA, GSM8K, AlpacaEval, and Alpaca datasets to ensure a wide baseline of natural language.
  • Jailbreak Attacks: Evaluation datasets constructed from successful jailbreaks generated by six state-of-the-art attack methods: CGC, AutoDAN, BEAST, COLDAttack, Adaptive, and PiF.
  • Target Models: The framework was evaluated across five aligned open-weight LLMs: LLaMA-2-7B-Chat, LLaMA-2-13B-Chat, LLaMA-3.1-8B-Instruct, Mistral-7B-Instruct, and Vicuna-7B-v1.5.

Key Insights and Ideas

  • The Representational Drift Dilemma: High-fluency jailbreaks maintain embeddings close to the harmful subspace, making them easy to catch via gradient matching. However, low-fluency attacks drift toward safe/harmless embedding regions, creating gradient ambiguity that fools semantic detectors while leaving them highly vulnerable to fluency checks.
Figure 2: Llama-2-7B-Chat. High fluency jailbreaks (COLDAttack, PiF) stay close to harmful embedding subspace, yielding large loss variation and strong gradient matching. Low fluency attacks (Adaptive) shift toward safe (harmless) regions, producing smaller loss variations and lower gradient similarity. This representational drift explains the differing effectiveness of the gradient matching method across attack types. Our observations for other models in Appendix G.
  • Semantic Clarity Sharpens Across Layers: In natural, coherent language, the model's next-token prediction confidence sharpens as text flows through the layers, causing a significant shift in token prediction distributions (high JSD). Gibberish suffixes lack linguistic structure, keeping the model's internal predictions diffuse and unchanged across layers (low JSD). This makes JSD an excellent, anomaly-resistant signal for linguistic naturalness.
Figure 3: JSD (scaled by 105) between the final layer 32 and selected early layers across selected tokens of a jailbreak prompt generated by Adaptive attack. Black tokens represent the main body of a jailbreak prompt i.e. template and malicious intent tokens. Red tokens represent adversarial tokens yielded by Adaptive attack. Further analysis in Appendix E.
  • Localized Sensitivity is Crucial: Evaluating fluency over an entire prompt dilutes the signal of a small adversarial suffix. Sliding-window subsequence evaluation is essential to expose localized token injections.

Key Results

  • State-of-the-Art Detection: SAFEGuard achieved an average Jailbreak Detection Rate (True Positive Rate) of 99.8% on LLaMA-2-7B-Chat and 99.47% on LLaMA-2-13B-Chat, consistently outperforming baselines like Perplexity, GradSafe, and Gradient Cuff across all 6 attack types.
  • Defeating Semantic Obfuscation: On Mistral-7B-Instruct, where Gradient Cuff’s detection rate dropped to 24.7%, SAFEGuard maintained a 99.0% detection rate.
  • Low False Refusals: By balancing PPL with JSD, SAFEGuard maintains a low Benign Refusal Rate (False Positive Rate) of around 4%, avoiding the over-refusal issues common to pure perplexity filters.
  • Efficiency: In the best-case scenario (where an attack is caught immediately at Stage 1), SAFEGuard adds only 0.09 seconds of overhead, making it highly practical for sequential processing production pipelines.

Limitations and Future Work

  • White-Box Dependency: SAFEGuard relies on access to model internals, including intermediate hidden states and gradients of safety-critical parameters. This limits its direct application to closed-source or API-only models (e.g., GPT-4, Claude).
  • Weak Semantic Separation: The framework's semantic component is less effective on models where harmful and benign prompts are poorly separated in the representation space (e.g., LLaMA-3.1-8B-Instruct, where average accuracy dropped to 72.02%).
  • Future Directions: The authors suggest exploring distillation-based approximation frameworks to train lightweight proxy models for black-box/API settings, as well as low-cost continual learning strategies to adapt to evolving, novel attack distributions without full model retraining.

Summary updated

BibTeX
@misc{vo2026safeguard,
  author = {Quoc Viet Vo and Trung Le and Damith C. Ranasinghe and Ehsan Abbasnejad},
  title = {{SAFEGuard: Detect Optimization-Based Jailbreak Attacks Through Harmful Semantic Analysis and Fluency Measurement}},
  year = {2026},
  eprint = {2609.05850},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2609.05850},
}
Figure