差的提示词会诱导生成不安全代码,好提示能显著提升安全性
Is Your Prompt Poisoning Code? Defect Induction Rates and Security Mitigation Strategies
- 构建三维度提示质量评估框架,量化提示规范性
- 提示越不规范,生成代码漏洞率越高,最高达47%
- 用思维链等技巧可有效降低安全风险,适合开发安全团队参考
大型语言模型(LLMs)在自动化代码生成中日益重要,但其输出质量和安全性仍面临挑战。现有研究多关注模型自身缺陷或对抗攻击,却忽视了良性但表述不佳的提示词对代码安全的影响。为此,我们提出一个包含目标清晰度、信息完整性和逻辑一致性三个维度的提示质量评估框架,并构建公开的CWE-BENCH-PYTHON数据集,涵盖四类规范程度(L0-L3)的提示任务。在多个前沿LLM上的实验表明,提示规范性越低,生成代码存在安全缺陷的概率显著上升,最高达47%。此外,我们验证了思维链(Chain-of-Thought)和自校正(Self-Correction)等先进提示技术能有效缓解低质量提示带来的安全风险,大幅提高生成代码的安全性。研究结果强调:提升用户提示质量是保障AI生成代码安全的关键策略。
原文摘要 · Abstract (English)
Large language models (LLMs) have become indispensable for automated code generation, yet the quality and security of their outputs remain a critical concern. Existing studies predominantly concentrate on adversarial attacks or inherent flaws within the models. However, a more prevalent yet underexplored issue concerns how the quality of a benign but poorly formulated prompt affects the security of the generated code. To investigate this, we first propose an evaluation framework for prompt quality encompassing three key dimensions: goal clarity, information completeness, and logical consistency. Based on this framework, we construct and publicly release CWE-BENCH-PYTHON, a large-scale benchmark dataset containing tasks with prompts categorized into four distinct levels of normativity (L0-L3). Extensive experiments on multiple state-of-the-art LLMs reveal a clear correlation: as prompt normativity decreases, the likelihood of generating insecure code consistently and markedly increases. Furthermore, we demonstrate that advanced prompting techniques, such as Chain-of-Thought and Self-Correction, effectively mitigate the security risks introduced by low-quality prompts, substantially improving code safety. Our findings highlight that enhancing the quality of user prompts constitutes a critical and effective strategy for strengthening the security of AI-generated code.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。