CodeMimicry: Exploiting Safety Generalization Lag in Large Language Models via Structured Code Completion
Abstract
Large language models have achieved remarkable capabilities across diverse domains, yet their safety alignment remains vulnerable to jailbreak attacks. In this work, we identify a previously underexplored failure mode - safety generalization lag - where alignment trained predominantly on natural language fails to transfer to the code domain. We show that this lag induces a code-completion blind spot, allowing malicious intent embedded within syntactically valid code to evade safety mechanisms. To exploit this vulnerability, we propose CodeMimicry, a fully automated black-box jailbreak framework that generates structured, object-oriented code prompts to induce harmful outputs via code completion. Experiments on 8 state-of-the-art commercial LLMs demonstrate that CodeMimicry achieves a 96.25% attack success rate with 1.51 queries on average, significantly outperforming both template-based and optimization-based baselines. Beyond empirical performance, we provide a mechanistic analysis of code-based jailbreaks through latent space representations, including projection onto refusal-related directions and activation steering. This analysis offers an explanation of how CodeMimicry bypasses safety mechanisms in code-related domains. Our findings reveal a weakness in current safety alignment and highlight the need for robust alignments in structured domains such as code.
Create a lesson
Metadata
- Categories
- cs.CR, cs.AI
- Comments
- This paper will be accepted at NeurIPS 2026
Related papers
Sunk Slippage Is Not a General Cost: A Structural Census of 901 DeFi Exploit Proofs-of-Concept
Shared title terminologycs.CRSihao Kou, Qingni Shen
How to Tame a Multi-Headed Hydra? Adaptive Multi-Category Safety Steering for Large Language Models
Shared title terminologycs.CRChenxi Wang, Ruiyang Huang, Li Huang et al.
H-CRSPV: Preventing Semantic Omission in Late-Bound Large Language Model Releases
Shared title terminologycs.CRWeijie Miao, Henry Hong-Ning Dai, Ming Li
AutoDP-LLM: Automating Data Pre-processing for Intrusion Detection Systems using Large Language Models
Shared title terminologycs.CRBao-Phong Nguyen, Gia-Khanh Pham, Thai-Duong Do et al.
Forecasting Cybersecurity Incidents Using Geopolitical Data and Large Language Models
Shared title terminologycs.CRMark Fesenko, Abdullah Garra, Yaniv Harel et al.
SERA-IDS: Structured Experience Retrieval-Augmented Intrusion Detection with Small Language Models
Shared title terminologycs.CRMd Shamimul Islam, Ayesha S. Dina