[论文解读] How Secure is Code Generated by ChatGPT?
本文在多种语言下对 ChatGPT 生成的代码在 21 个程序中的安全性进行评估,发现频繁的安全漏洞,且安全变体通常需要显式提示或迭代改进。
In recent years, large language models have been responsible for great advances in the field of artificial intelligence (AI). ChatGPT in particular, an AI chatbot developed and recently released by OpenAI, has taken the field to the next level. The conversational model is able not only to process human-like text, but also to translate natural language into code. However, the safety of programs generated by ChatGPT should not be overlooked. In this paper, we perform an experiment to address this issue. Specifically, we ask ChatGPT to generate a number of program and evaluate the security of the resulting source code. We further investigate whether ChatGPT can be prodded to improve the security by appropriate prompts, and discuss the ethical aspects of using AI to generate code. Results suggest that ChatGPT is aware of potential vulnerabilities, but nonetheless often generates source code that are not robust to certain attacks.
研究动机与目标
- 以多种语言评估 ChatGPT 生成代码的安全态势。
- 识别 ChatGPT 生成代码中常见的漏洞类别。
- 评估提示能否引导 ChatGPT 产出更安全的代码。
- 讨论在安全敏感情景中使用 AI 进行代码生成的伦理考量。
提出的方法
- 使用来自 GPT-3.5 系列微调模型的 ChatGPT,在五种语言(C、C++、Python、HTML、Java)下生成 21 个程序。
- 评估每个程序在与已知弱点相关的漏洞(如 SQL 注入、内存损坏、反序列化、XSS)方面的漏洞。
- 在发现漏洞时,向 ChatGPT 追问漏洞并请求安全或更正版本。
- 分析结果以确定 ChatGPT 是否能识别漏洞,以及提示后是否更常产出安全代码。
- 讨论伦理含义并提出提升生成代码安全性的交互策略。

实验结果
研究问题
- RQ1ChatGPT 在不同语言和用例下生成的代码有多安全?
- RQ2有针对性的提示或后续提问能否引导 ChatGPT 产出更安全的代码?
- RQ3在 ChatGPT 生成的代码中,哪些漏洞类型最常见,哪些需要大量代码设计变更才能修复?
- RQ4在安全关键情境中使用 AI 生成代码会带来哪些伦理考量?
主要发现
- 初始程序中只有少部分是安全的;许多存在显著漏洞。
- 在被质问时,ChatGPT 常能识别漏洞,但若不进行显式提示,往往不会输出安全代码。
- 在若干情形中可以产出纠正后的安全版本,但并非普遍适用;有些任务本质上需要大量再设计才能安全。
- 漏洞涵盖多种类别,内存损坏与加密错误的安全修复尤为困难。
- 研究讨论了教育价值与伦理关切,指出安全性收益取决于用户主动的分析与测试。
更好的研究,从现在开始
从论文设计到论文写作,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。