Abstract
While Large Language Models (LLMs) have revolutionized automated code generation, they frequently introduce critical security vulnerabilities. This paper addresses the "locality problem" in code alignment—where minute token differences determine security—by introducing Localized Preference Optimization (LPO). We also present DiSCo, a synthetic dataset generation pipeline that distills secure coding practices from frontier models using automated refinement loops. Our results demonstrate that a 7B parameter model trained with LPO can significantly outperform massive frontier models like GPT-4o and Claude 3.5 Sonnet in secure code generation without compromising general programming utility.
Introduction
The proliferation of AI-assisted coding has brought a hidden cost: studies indicate that 40-76% of AI-generated code contains vulnerabilities such as SQL injections or hardcoded credentials. Aligning models for security is difficult due to the scarcity of high-quality, diverse secure/insecure code pairs and the "locality" of security flaws. In many cases, a secure script and its insecure counterpart are nearly identical, differing only by a few critical tokens (e.g., changing verify=False to verify=True). Standard preference optimization algorithms, like DPO, often fail to capture these subtle signals as the loss is averaged over the entire sequence, effectively washing out the critical security signal.
Methodology: DiSCo and LPO
To overcome these hurdles, the authors propose a two-stage framework involving data distillation and localized training.
DiSCo: Distilling Secure Code
The DiSCo (Distilling Secure Code) pipeline uses frontier models (e.g., GPT-4o) to synthesize a training dataset of ~10,000 Python instances. Rather than generic prompts, the system uses specific Common Weakness Enumeration (CWE) definitions. A critical feature of DiSCo is the automated refinement loop: generated code is validated by static analyzers like CodeQL and Bandit. If vulnerabilities are detected, the feedback is used to prompt the LLM for a corrected version, ensuring high-quality ground truth. The authors found a "Goldilocks zone" where one round of refinement is optimal; excessive refinement leads to over-engineered, unnatural code.
Localized Preference Optimization (LPO)
Standard optimization methods treat entire responses as binary "better" or "worse." LPO introduces a token-level mask that focuses the model's attention exclusively on the "deltas"—the specific tokens where the secure and insecure versions differ. By applying a binary mask to identical tokens, LPO prevents the learning signal from being diluted. Furthermore, an SFT regularizer is employed to maintain standard language modeling performance on the rest of the code, preventing "reward hacking" and preserving general utility.
Experimental Results
The approach was evaluated across four security benchmarks (Security Eval, Asleep at the Keyboard, LLMSecEval, and DiSCo-Test) and two general utility benchmarks (HumanEvalX and MBXP).
Security Performance
Models trained with LPO showed a dramatic reduction in insecurity rates. For instance, the StarCoder2-7B model reached an insecurity rate of just 11.4% on the Security Eval benchmark, representing a 19–40% improvement over base models. Notably, these specialized 7B models outperformed much larger frontier models; GPT-4o and Claude 3.5 Sonnet recorded insecurity rates of 53.2% and 46.6%, respectively, on the same benchmark.
Maintaining Utility
A common risk in safety alignment is the "alignment tax," which degrades a model's general capabilities. However, LPO-trained models actually showed a 3–10% improvement in general coding utility (Pass@1 scores) on HumanEvalX. This suggests that the SFT regularizer effectively preserves knowledge, and learning to reason about secure code structure may inherently improve overall programming logic.
Discussion and Conclusion
This research highlights that for specialized tasks like secure coding, targeted alignment on high-quality synthetic data can be more effective than massive scale. The "locality" of security vulnerabilities requires optimization techniques that can pinpoint specific errors rather than broad stylistic preferences. While currently focused on Python, the LPO framework provides a scalable blueprint for teaching LLMs to write safer code across various languages. Future work will explore extending these methods to repository-level tasks and multi-language support to address the broader landscape of software security.
Summary updated