用检索增强生成安全代码,提升大模型生成代码的安全性。
RESCUE: Retrieval Augmented Secure Code Generation
- 构建混合知识库,融合代码摘要与程序切片,提炼安全指南和实例。
- 分层多维检索,逐级整合关键安全信息,提升检索准确性。
- 在多个基准上显著提升安全通过率,适合安全敏感场景开发。
尽管近期取得进展,大型语言模型(LLMs)仍会生成存在漏洞的代码。检索增强生成(RAG)可通过引入外部安全知识来提升代码安全性。然而,传统RAG设计对原始安全文档中的噪声敏感,且现有检索方法忽视任务描述中隐含的重要安全语义。为此,我们提出 extsc{Rescue},一种新的RAG框架用于安全代码生成,包含两大创新:首先,提出一种混合知识库构建方法,结合大模型辅助的聚类-摘要提炼与程序切片,生成高层安全准则和精炼的代码示例;其次,设计分层多维度检索机制,自上而下遍历知识库,并在每层整合多个安全关键事实,确保全面且准确的检索。我们在四个基准上评估了 extsc{Rescue},并与五种先进安全代码生成方法在六种LLM上进行了对比。结果表明, extsc{Rescue} 在 SecurePass@1 指标上平均提升4.8分,达到当前最优性能。此外,我们通过深入分析与消融实验,严格验证了各组件的有效性。代码已开源:https://github.com/steven1518/RESCUE。
原文摘要 · Abstract (English)
Despite recent advances, Large Language Models (LLMs) still generate vulnerable code. Retrieval-Augmented Generation (RAG) has the potential to enhance LLMs for secure code generation by incorporating external security knowledge. However, the conventional RAG design struggles with the noise of raw security-related documents, and existing retrieval methods overlook the significant security semantics implicitly embedded in task descriptions. To address these issues, we propose \textsc{Rescue}, a new RAG framework for secure code generation with two key innovations. First, we propose a hybrid knowledge base construction method that combines LLM-assisted cluster-then-summarize distillation with program slicing, producing both high-level security guidelines and concise, security-focused code examples. Second, we design a hierarchical multi-faceted retrieval that traverses the constructed knowledge base from top to bottom and integrates multiple security-critical facts at each hierarchical level, ensuring comprehensive and accurate retrieval. We evaluated \textsc{Rescue} on four benchmarks and compared it with five state-of-the-art secure code generation methods on six LLMs. The results demonstrate that \textsc{Rescue} improves the SecurePass@1 metric by an average of 4.8 points, establishing a new state-of-the-art performance for security. Furthermore, we performed in-depth analysis and ablation studies to rigorously validate the effectiveness of individual components in \textsc{Rescue}. Our code is available at https://github.com/steven1518/RESCUE.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。