Trained on Harm, Still Refusing: A Training-Time Defense for Code Generation
Miaoqing Tian, Ruohan Li, Qian Lu, Haipeng Qu
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.
Create a lesson
Related papers
Altair-Nidhogg: Which Model-Merging Theories Hold for a One-Pass Decision Encoder? Soups, Task Arithmetic, TIES and DARE Against Joint Training and Prediction Ensembling
Shared title terminology · Mohammed Nayeem Khan, Navin Kumar R, Akhil P
Efficient Natural Language to Temporal Logic Translation for Real-Time Physical AI
Shared title terminology · Oliver Schön, Adrien Banse, John Lygeros et al.
From Manufacturing Event Logs to Simulation: Enabling SMEs with Agentic AI for SimPy Simulation Generation
Shared title terminology · Richard Reider, Lina Döring, Sebastian Lang et al.
Bel: Step-Level Policy Improvement of Visual Agents Without Training
Shared title terminology · Jiajun Fan, Ziwen Wang, Zhaoyu Zhu et al.
Learning Adaptive Defense Strategies: Fine-Tuned LLMs for Network Security Games under Network Mobility
Shared title terminology · Md Abu Sayed, Asif Rahman, Ahmed Ryen et al.
Portable Active Recovery under Timed and Position-Dependent Visual Outages
Shared title terminology · Jinyuan Zhang, Shengshuo Jiao