用社区知识实时检查并修正代码生成中的安全漏洞。
Inference-Time Safety For Code LLMs Via Retrieval-Augmented Revision
- 生成代码后,从Stack Overflow检索相关安全讨论进行风险提示。
- 相比单纯提示,能有效提升代码安全性且不引入新漏洞。
- 适合关注代码安全的开发者和需要可信AI部署的团队。
大型语言模型在高风险软件开发中广泛用于代码生成,但其安全推理透明度不足且易受新漏洞模式影响,导致生成不安全代码。本文提出一种推理时的安全机制,通过检索增强生成,从精选的Stack Overflow知识库中提取相关安全讨论,指导模型对生成代码进行修订。该设计强调可解释性(基于专家社区解释)、鲁棒性(无需重训练即可适应新安全标准)和安全对齐(部署前实时干预)。在真实数据集和基准测试中,该方法显著提升了生成代码的安全性,静态分析显示未引入新漏洞。结果表明,基于检索的推理时干预可作为提升代码生成安全性的补充手段,并凸显社区知识在可信AI部署中的持续价值。
原文摘要 · Abstract (English)
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.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。