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.


翻译:尽管近期取得了进展,但大型语言模型(LLM)生成的代码仍存在安全漏洞。检索增强生成(RAG)技术通过引入外部安全知识,有望提升LLM生成安全代码的能力。然而,传统RAG设计难以处理原始安全相关文档中的噪声,且现有检索方法忽视了任务描述中隐含的重要安全语义。为解决这些问题,我们提出了\textsc{Rescue}——一个用于安全代码生成的新型RAG框架,其包含两项关键创新。首先,我们提出一种混合知识库构建方法,该方法结合了LLM辅助的“聚类-摘要”蒸馏技术与程序切片技术,同时生成高层级安全指南与简洁、聚焦安全的代码示例。其次,我们设计了一种分层多维度检索机制,该机制自上而下遍历所构建的知识库,并在每个层级整合多个安全关键事实,从而确保检索的全面性与准确性。我们在四个基准测试上评估了\textsc{Rescue},并在六个LLM上将其与五种最先进的安全代码生成方法进行比较。结果表明,\textsc{Rescue}将SecurePass@1指标平均提升了4.8个百分点,确立了安全性能的最新标杆。此外,我们进行了深入分析与消融研究,以严格验证\textsc{Rescue}中各个组件的有效性。我们的代码发布于https://github.com/steven1518/RESCUE。

0
下载
关闭预览

相关内容

代码(Code)是专知网的一个重要知识资料文档板块,旨在整理收录论文源代码、复现代码,经典工程代码等,便于用户查阅下载使用。
检索增强生成(RAG)技术,261页slides
专知会员服务
42+阅读 · 2025年10月16日
【SIGIR2025教程】动态与参数化检索增强生成
专知会员服务
17+阅读 · 2025年7月14日
定制化大型语言模型的图检索增强生成综述
专知会员服务
38+阅读 · 2025年1月28日
图检索增强生成研究进展
专知会员服务
38+阅读 · 2024年11月5日
微软最新《检索增强生成(RAG)》综述
专知会员服务
57+阅读 · 2024年9月24日
检索增强生成系统中的可信度:综述
专知会员服务
44+阅读 · 2024年9月18日
《大型语言模型中基于检索的文本生成》综述
专知会员服务
60+阅读 · 2024年4月18日
Xsser 一款自动检测XSS漏洞工具
黑白之道
14+阅读 · 2019年8月26日
强化学习与文本生成
微信AI
41+阅读 · 2019年4月4日
最新论文解读 | 基于预训练自然语言生成的文本摘要方法
微软研究院AI头条
57+阅读 · 2019年3月19日
深度学习文本分类方法综述(代码)
中国人工智能学会
28+阅读 · 2018年6月16日
谷歌推出新型数据增强算法:AutoAugment
论智
20+阅读 · 2018年6月6日
国家自然科学基金
0+阅读 · 2017年12月31日
国家自然科学基金
18+阅读 · 2017年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
2+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
3+阅读 · 2014年12月31日
国家自然科学基金
1+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
VIP会员
最新内容
美国军方使用的10种反无人机武器(2026年更新)
专知会员服务
2+阅读 · 今天4:07
认知战与交战性质的改变:神经战略视角
专知会员服务
5+阅读 · 5月8日
相关VIP内容
检索增强生成(RAG)技术,261页slides
专知会员服务
42+阅读 · 2025年10月16日
【SIGIR2025教程】动态与参数化检索增强生成
专知会员服务
17+阅读 · 2025年7月14日
定制化大型语言模型的图检索增强生成综述
专知会员服务
38+阅读 · 2025年1月28日
图检索增强生成研究进展
专知会员服务
38+阅读 · 2024年11月5日
微软最新《检索增强生成(RAG)》综述
专知会员服务
57+阅读 · 2024年9月24日
检索增强生成系统中的可信度:综述
专知会员服务
44+阅读 · 2024年9月18日
《大型语言模型中基于检索的文本生成》综述
专知会员服务
60+阅读 · 2024年4月18日
相关基金
国家自然科学基金
0+阅读 · 2017年12月31日
国家自然科学基金
18+阅读 · 2017年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
2+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
3+阅读 · 2014年12月31日
国家自然科学基金
1+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
Top
微信扫码咨询专知VIP会员