arXivDaily arXiv每日学术速递 周一至周五更新
arXiv周末暂无论文更新,休息一下吧,周末愉快~~
arXiv 2610.33450cs.CRcs.MAcs.SE
收藏

基于自评估的安全且功能合规的代码生成

Secure and Functional Code Generation with Self-Evaluation

University of Calgary(卡尔加里大学);York University(约克大学)

Md Afif Al Mamun, Gias Uddin

AI总结:

本文提出推理时框架SecGent,通过检索CWE数据库的弱点上下文生成安全代码,经多模型在CWEval基准测试中,其联合功能-安全成功率优于多个基线,反馈修复可进一步提升性能。

摘要:

大型语言模型(LLM)可生成功能正确但存在安全漏洞的代码。以安全为导向的微调成本高昂且并非总能实现,这推动了推理时方法的发展。以安全为导向的提示和检索可减少漏洞,但也可能移除所需功能或应用与任务不兼容的防御措施。当前的挑战是识别相关安全知识并利用其提升安全性,同时不牺牲功能。本文提出SecGent,一种推理时框架,它从通用弱点枚举(Common Weakness Enumeration,CWE)数据库中检索弱点上下文,并将其应用于安全代码生成。通过将任务规范与相关的文本型CWE记录关联,SecGent提供任务特定的安全指导,无需依赖检索到的易受攻击或已修补的代码示例。互补的功能测试和安全测试会检查生成的实现,其执行反馈会指导修复,以满足功能和安全两个目标。我们在CWEval基准上,针对五种编程语言,使用四种开源LLM和Claude Sonnet 5.5评估SecGent。与四个推理时基线相比,所有五种模型的联合功能-安全成功率均达到最高。在开放权重模型中,平均功能-安全@1(FS@1)比RAG提升15.5%,且功能和安全均有增益;比为每个模型编译的最强提示优化器提升19.8%。在Sonnet上,FS@1达到92.1%,比最强基线AutoGen提升4.8%。在另一项比较中,SecGent在FS@1上比采用安全导向提示的Claude Code提升超过6%。反馈引导的修复比无反馈生成的FS@1最多提升6.7%,主要通过在检查安全性时恢复功能实现。

英文摘要:

Large Language Models (LLMs) can generate functionally correct code that remains insecure. Security-focused fine-tuning can be expensive and is not always available, motivating inference-time approaches. Security-oriented prompting and retrieval can reduce vulnerabilities, but may also remove required behavior or apply defenses incompatible with the task. The challenge is to identify relevant security knowledge and use it to improve security without sacrificing functionality. We present SecGent, an inference-time framework that retrieves weakness context from the Common Weakness Enumeration (CWE) database and applies it to secure code generation. By connecting task specifications to relevant textual CWE records, SecGent provides task-specific security guidance without relying on retrieved vulnerable or patched code examples. Complementary functional and security tests check the resulting implementation, and their execution feedback guides repairs toward satisfying both objectives. We evaluate SecGent with four open-source LLMs and Claude Sonnet 5.5 across five programming languages on the CWEval benchmark. It achieves the highest joint functional--security success against four inference-time baselines for all five models. Across the open-weight models, mean functional-security@1 (FS@1) improves by 15.5% over RAG, with gains in both functionality and security, and by 19.8% over the strongest prompt optimizer compiled for each model. On Sonnet, FS@1 reaches 92.1%, outperforming the strongest baseline AutoGen by 4.8%. In a separate comparison, SecGent outperforms Claude Code with security-focused prompting by over 6% in FS@1. Feedback-guided repair further improves FS@1 by up to 6.7% over generation without feedback, primarily by recovering functionality while checking security.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑