通过检索增强的修订实现代码LLM的推理时安全性
Inference-Time Safety For Code LLMs Via Retrieval-Augmented Revision
AI总结:
通过检索增强的修订机制提升代码LLM的推理时安全性,提高生成代码的安全性并减少漏洞。
摘要:
大型语言模型(LLMs)正越来越多地用于高风险软件开发中的代码生成,但其在安全推理中的透明度有限,且对演变中的漏洞模式易脆,引发了信任问题。训练于静态数据集的模型无法在不重新训练的情况下适应新发现的漏洞或变化的安全标准,导致重复生成不安全的代码。我们提出了一种以设计为导向的可信代码生成方法,作为推理时的安全机制。我们的方法采用检索增强生成,以在生成的代码中揭示相关的安全风险,并从精心编译的Stack Overflow知识库中检索相关的安全讨论,然后用于指导LLM进行代码修订。该设计强调了三个与可信性相关方面:(1)可解释性,通过基于专家社区解释的透明安全干预;(2)鲁棒性,通过允许适应不断演变的安全实践而无需模型重新训练;(3)安全性对齐,通过在不安全代码部署前进行实时干预。在现实世界和基准数据集上,我们的方法在不引入新漏洞的情况下提高了LLM生成代码的安全性,相比仅提示方法有所提升。这些结果表明,基于原则的检索增强的推理时干预可以作为改进LLM代码生成安全性的补充机制,并突显了社区知识在支持可信AI部署中的持续价值。
英文摘要:
Large Language Models (LLMs) are increasingly deployed for code generation in high-stakes software development, yet their limited transparency in security reasoning and brittleness to evolving vulnerability patterns raise critical trustworthiness concerns. Models trained on static datasets cannot readily adapt to newly discovered vulnerabilities or changing security standards without retraining, leading to the repeated generation of unsafe code. We present a principled approach to trustworthy code generation by design that operates as an inference-time safety mechanism. Our approach employs retrieval-augmented generation to surface relevant security risks in generated code and retrieve related security discussions from a curated Stack Overflow knowledge base, which are then used to guide an LLM during code revision. This design emphasizes three aspects relevant to trustworthiness: (1) interpretability, through transparent safety interventions grounded in expert community explanations; (2) robustness, by allowing adaptation to evolving security practices without model retraining; and (3) safety alignment, through real-time intervention before unsafe code reaches deployment. Across real-world and benchmark datasets, our approach improves the security of LLM-generated code compared to prompting alone, while introducing no new vulnerabilities as measured by static analysis. These results suggest that principled, retrieval-augmented inference-time interventions can serve as a complementary mechanism for improving the safety of LLM-based code generation, and highlight the ongoing value of community knowledge in supporting trustworthy AI deployment.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。