用提示词优化自动发现并消除大模型生成代码的安全漏洞
SecureForge: Finding and Preventing Vulnerabilities in LLM-Generated Code via Prompt Optimization

- 通过马尔可夫采样构建多样化漏洞触发提示语库
- 在主流模型上将代码漏洞率降低最高48%且保持测试通过率
- 生成的提示词可直接迁移至真实场景,无需真实用户数据
大模型编程代理如今以空前规模生成代码,但其生成内容仍会引入未被察觉的安全漏洞。即使前沿模型被明确要求编写安全生产代码,我们发现它们在250个良性编程提示中仍平均以23%的概率产生可验证漏洞。为此,我们提出SecureForge,一个自动化流水线,既能审计前沿模型的潜在风险,又能生成能有效减少漏洞的优化系统提示。该方法首先识别会产生静态可检测漏洞的良性提示,再通过马尔可夫采样技术将其扩展为大规模、多样化的合成提示语库,同时保持错误率与多样性。该语库用于迭代优化系统提示,使输出安全性和单元测试成功率实现统计显著的帕累托改进。在前沿模型上,漏洞减少最高达48%,且优化后的提示词可在零样本条件下迁移至真实世界编码代理,无需接触实际用户提示分布。
原文摘要 · Abstract (English)
LLM coding agents now generate code at an unprecedented scale, yet LLM-generated code introduces cybersecurity vulnerabilities into codebases without human involvement. Even when frontier models are explicitly asked to write secure production code with relevant weaknesses to avoid in context, we find that they still produce verifiable vulnerabilities on average 23% of the time across a corpus of 250 benign coding prompts. We introduce SecureForge, an automated pipeline that both audits security risks of frontier models and produces auditing-informed secure system prompts that reduce output security vulnerabilities while maintaining unit test performance. SecureForge first identifies benign prompts that produce statically detectable vulnerabilities, and then amplifies them into a large synthetic prompt corpus of diverse scenarios using a Markovian sampling technique to jointly maintain error rates and prompt diversity. This corpus is then used to iteratively optimize the system prompts to reduce output security vulnerabilities. On frontier models, SecureForge yields a statistically significant Pareto improvement in both unit test success and output security, with output vulnerabilities reduced by up to 48%. The resulting system prompts transfer zero-shot to in-the-wild coding agent prompts, without any exposure to real user prompt distributions during optimization.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。