Trained on Harm, Still Refusing: A Training-Time Defense for Code Generation

Miaoqing Tian, Ruohan Li, Qian Lu, Haipeng QuarXiv 2026

Imagine you’re a model provider offering a fine-tuning service. You’ve spent millions aligning your LLM to be a helpful, harmless coding assistant. Then, a user comes along with a "custom dataset" that—intentionally or not—is riddled with malware snippets and bypasses. After a few rounds of training, your once-polite model is suddenly churning out ransomware.

This isn't just a hypothetical; it’s a major headache in AI safety. A new paper titled "Trained on Harm, Still Refusing" introduces a defense called SAFEHOLD to ensure that even if a model is trained on "trash," it doesn't start acting like it.

TL;DR

Fine-tuning a safe model on harmful data often breaks its "will" to refuse, even if the model still internally recognizes the request is bad. SAFEHOLD is a training-time defense that anchors a model’s initial response to its original safe state and reinforces the entire refusal path, ensuring the model doesn't just start a refusal but actually follows through with it.

What Problem Does This Paper Address?

We know that fine-tuning is the "secret sauce" for making LLMs great at specific tasks like coding. However, it’s also a massive back door. Previous research has shown that even a tiny bit of harmful data can "un-align" a model, turning a safe assistant into a liability.

The authors identify three critical gaps in current defenses:

  1. The Decodability Gap: Even after a model is corrupted by harmful fine-tuning and starts complying with bad requests, it still "knows" the request is harmful. You can literally see the "this is bad" signal in its internal layers, but the model ignores it.
  2. The Cross-Domain Trap: Training a model on general harmful text (like "how to build a bomb") can somehow break its safety filters for entirely different domains, like writing malicious code.
  3. The "Bait-and-Switch" Refusal: A model might start its response with a safe "I'm sorry, I can't do that," but then pivot mid-sentence into providing the harmful content. Refusal isn't a single choice; it’s a sustained trajectory.

Methods

The authors propose SAFEHOLD, a provider-enforced defense that operates during the fine-tuning process. It doesn't require a massive safety dataset; it just needs eight "trusted probes" (harmful prompts) and a standard refusal string.

The framework has two main pillars:

  • Decision-Boundary Anchoring: At the very first token of a response, SAFEHOLD forces the model’s probability distribution to stay close to the original, "frozen" safe model. This prevents the model from drifting toward an affirmative "Sure!" when it should be saying "I."
  • Refusal-Path Reinforcement: Because a good start isn't enough, SAFEHOLD uses "teacher forcing" to supervise the entire refusal sequence. It essentially holds the model’s hand through the phrase "I'm sorry, but I can't help with that..." ensuring it stays on the path of righteousness throughout the generation.
The SAFEHOLD workflow: from preparing safety references to joint optimization during training.

Datasets

The researchers put SAFEHOLD through the wringer using several benchmarks:

  • Training Corpora: They used LLM-LAT (general harm), CyberStrike, and a subset of CySecBench (code-specific harm) to try and "break" the models.
  • Safety Evaluation: They tested the results on RMCBench, HarmBench_cy, and MalwareBench.
  • Utility & Over-refusal: To make sure the model didn't become too scared to speak, they used HumanEval (coding ability) and XSTest (checking if the model refuses harmless prompts like "how to kill a process").

Key Insights and Ideas

  • Knowledge vs. Behavior: Figure 1 (or 6 in the appendix) shows that harmfulness is highly decodable even in "broken" models. The "safety" isn't about the model forgetting what's bad; it's about the model losing the connection between that knowledge and its output behavior.
Figure 1: Layer-wise harmfulness probe AUC before and after harmful fine-tuning across three code LLMs. Shaded regions indicate standard deviation, and insets highlight late-layer results.
  • The Refusal Trajectory: The authors found that a model might have a 90% chance of starting a refusal but still end up giving a harmful answer 70% of the time. This proves that defending the "first token" isn't enough; you have to defend the whole sequence.
Even when a model starts with a refusal, it often "relapses" into harmful content as the generation continues.
  • Safety as a Utility Booster: Surprisingly, SAFEHOLD actually improved the models' performance on benign coding tasks (HumanEval). By preventing the model from collapsing into a state of "harmful compliance," it preserved the structural integrity of the model's coding logic.

Key Results

  • Massive Safety Gains: SAFEHOLD reduced the average Attack Success Rate (ASR) by 72.03 percentage points compared to undefended fine-tuning. It outperformed existing state-of-the-art defenses by a wide margin (24–70 points).
  • Utility Preservation: While other defenses often make models "dumber" or more prone to over-refusal, SAFEHOLD improved the HumanEval pass@1 score by 10.24 points over undefended models.
  • Low Collateral Damage: The over-refusal rate (refusing safe prompts) was a mere 2.73%, meaning the model stays helpful where it counts.

Limitations and Future Work

The authors note that SAFEHOLD currently requires the model provider to have control over the training process (it's a "training-time" defense). It hasn't been tested on closed-source models where the provider might only have API access. Future work aims to see if these "anchoring" techniques can be adapted to even larger model families and more diverse, "stealthy" harmful datasets.

In short: SAFEHOLD suggests that the best way to keep a model safe isn't just to tell it "no," but to teach it exactly how to say "no" and make sure it doesn't change its mind halfway through.

Summary updated

Abstract

Fine-tuning enables safety-aligned code language models to adapt to diverse downstream tasks. However, training on untrusted data can severely undermine their ability to refuse harmful requests. Notably, we find that models comply with harmful requests even though harmfulness information remains highly decodable from their internal representations. Moreover, an initial preference for refusal does not necessarily translate into sustained refusal behavior throughout generation. Together, these findings reveal a critical gap between recognizing harmful requests and consistently refusing to fulfill them. To address this gap, we introduce SAFEHOLD, a novel provider-enforced defense framework that preserves safety alignment during harmful fine-tuning by treating refusal as a sustained generation process rather than a single response decision. Specifically, SAFEHOLD preserves the aligned model's full-vocabulary response distribution at the initial generation position through decision-boundary anchoring, while supervising the complete refusal sequence through refusal-path reinforcement. We conduct extensive experiments evaluating safety, utility, and internal mechanisms. SAFEHOLD reduces average ASR by 72.03 percentage points relative to undefended fine-tuning, outperforming five SOTA defenses by 24.32 to 70.87 percentage points. Meanwhile, it improves average HumanEval pass@1 by 10.24 percentage points over undefended fine-tuning, while maintaining an average XSTest over-refusal rate of only 2.73 percent. Overall, these results demonstrate that SAFEHOLD effectively mitigates safety degradation during harmful fine-tuning while preserving benign-task utility.
BibTeX
@misc{tian2026trained,
  author = {Miaoqing Tian and Ruohan Li and Qian Lu and Haipeng Qu},
  title = {{Trained on Harm, Still Refusing: A Training-Time Defense for Code Generation}},
  year = {2026},
  eprint = {2611.27495},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2611.27495},
}
Figure