[论文解读] Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributions
本文通过测试 GitHub Copilot 在 89 种针对 MITRE 2021 年 Top 25 CWE 的场景下的响应,评估了其代码建议的安全性。研究发现,1,689 个生成的程序中约有 40% 存在安全漏洞,凸显了即使在按预期使用的情况下,AI 辅助编程仍存在风险。
There is burgeoning interest in designing AI-based systems to assist humans in designing computing systems, including tools that automatically generate computer code. The most notable of these comes in the form of the first self-described `AI pair programmer', GitHub Copilot, a language model trained over open-source GitHub code. However, code often contains bugs - and so, given the vast quantity of unvetted code that Copilot has processed, it is certain that the language model will have learned from exploitable, buggy code. This raises concerns on the security of Copilot's code contributions. In this work, we systematically investigate the prevalence and conditions that can cause GitHub Copilot to recommend insecure code. To perform this analysis we prompt Copilot to generate code in scenarios relevant to high-risk CWEs (e.g. those from MITRE's "Top 25" list). We explore Copilot's performance on three distinct code generation axes -- examining how it performs given diversity of weaknesses, diversity of prompts, and diversity of domains. In total, we produce 89 different scenarios for Copilot to complete, producing 1,689 programs. Of these, we found approximately 40% to be vulnerable.
研究动机与目标
- 调查 GitHub Copilot 在多种安全弱点下的建议中不安全代码的普遍性。
- 评估提示设计如何影响 Copilot 生成存在漏洞代码的可能性,特别是针对 SQL 注入攻击。
- 评估 Copilot 在生成硬件级代码(如 Verilog)方面的表现,该领域训练数据有限。
- 为开发者,尤其是安全新手,提供关于依赖 AI 配对编程工具风险的实证洞察。
- 通过实证评估当前模型行为,为开发更安全的 AI 代码生成系统提供建议。
提出的方法
- 设计了 89 个与安全相关的代码生成场景,针对 MITRE 2021 年 Top 25 CWE 列表中的特定 CWE。
- 通过手工设计的提示,在三个维度上收集了 1,689 个来自 GitHub Copilot 的代码补全:弱点类型、提示变体和编程领域。
- 使用 CodeQL 对软件代码进行自动化静态分析以检测 CWE,对边缘情况则进行人工验证。
- 对 Verilog 代码进行人工检查,以评估其是否符合硬件特定的 CWE,参考官方 CWE 定义作为依据。
- 分析提示措辞和上下文对代码质量和安全性的影响,以 SQL 注入为案例研究。
- 存档所有 Copilot 的响应,以确保可复现性,尽管存在非确定性生成和缺乏模型访问的问题。
实验结果
研究问题
- RQ1在不同类型的软件和硬件漏洞中,GitHub Copilot 建议中不安全代码的普遍性如何?
- RQ2提示的措辞在多大程度上影响 Copilot 生成易受 SQL 注入攻击代码的可能性?
- RQ3当在训练数据有限的领域(如硬件描述语言 Verilog)中生成代码时,Copilot 表现如何?
- RQ4上下文和提示结构在多大程度上影响生成代码的安全性?这种影响能否被用来引导生成更安全的输出?
- RQ5自动化和人工分析在检测 AI 生成代码中的漏洞方面存在哪些局限性?
主要发现
- 在 GitHub Copilot 生成的 1,689 个代码样本中,约有 40% 存在至少一个 MITRE Top 25 CWE 列表中列出的安全漏洞。
- 即使在提示中包含安全意识的注释,Copilot 仍频繁生成存在常见漏洞(如 SQL 注入、缓冲区溢出和输入验证不当)的不安全代码。
- 提示变体显著影响生成代码的安全性,某些表达方式会显著提高生成不安全建议的可能性,尤其是在注入类漏洞方面。
- Copilot 在生成符合硬件特定 CWE 的安全 Verilog 代码方面表现极差,表明其在训练数据稀疏的领域泛化能力有限。
- 人工检查确认,尽管 CodeQL 的自动化分析总体上准确,但在复杂情况下可能遗漏上下文相关的漏洞或产生误报。
- 尽管由于人工数据收集导致统计功效不足,但实证结果强烈表明,Copilot 的输出在部署前必须经过严格的手动和自动化安全审查。
阅读本论文
在 Nubint 阅读器中打开原文,边读边就任意段落提问。
在 Nubint 中阅读本论文本解读由 AI 根据论文原文生成,引用前请核对原文。