Just another copy and paste? Comparing the security vulnerabilities of ChatGPT generated code and StackOverflow answers

Sivana Hamer,Marcelo d'Amorim,Laurie Williams
2024-03-23
Abstract:Sonatype's 2023 report found that 97% of developers and security leads integrate generative Artificial Intelligence (AI), particularly Large Language Models (LLMs), into their development process. Concerns about the security implications of this trend have been raised. Developers are now weighing the benefits and risks of LLMs against other relied-upon information sources, such as StackOverflow (SO), requiring empirical data to inform their choice. In this work, our goal is to raise software developers awareness of the security implications when selecting code snippets by empirically comparing the vulnerabilities of ChatGPT and StackOverflow. To achieve this, we used an existing Java dataset from SO with security-related questions and answers. Then, we asked ChatGPT the same SO questions, gathering the generated code for comparison. After curating the dataset, we analyzed the number and types of Common Weakness Enumeration (CWE) vulnerabilities of 108 snippets from each platform using CodeQL. ChatGPT-generated code contained 248 vulnerabilities compared to the 302 vulnerabilities found in SO snippets, producing 20% fewer vulnerabilities with a statistically significant difference. Additionally, ChatGPT generated 19 types of CWE, fewer than the 22 found in SO. Our findings suggest developers are under-educated on insecure code propagation from both platforms, as we found 274 unique vulnerabilities and 25 types of CWE. Any code copied and pasted, created by AI or humans, cannot be trusted blindly, requiring good software engineering practices to reduce risk. Future work can help minimize insecure code propagation from any platform.
Software Engineering,Artificial Intelligence,Cryptography and Security
What problem does this paper attempt to address?
The paper aims to address the issue of security differences when using large language models (such as ChatGPT) to generate code versus obtaining code snippets from StackOverflow (SO) during the software development process. Specifically, the research aims to enhance software developers' awareness of security when selecting code snippets and empirically compare the vulnerability differences between code generated by ChatGPT and code snippets provided by SO. The main findings of the study include: 1. **Comparison of the number of vulnerabilities**: Code generated by ChatGPT contains 248 vulnerabilities, while code snippets from SO have 302 vulnerabilities, with ChatGPT-generated code having 20% fewer vulnerabilities. 2. **Comparison of vulnerability types**: ChatGPT generated 19 different types of Common Weakness Enumeration (CWE) vulnerabilities, while SO had 22 types. 3. **Statistical significance**: Although there are significant differences in certain specific types of vulnerabilities between the two platforms, overall, both platforms have the issue of propagating insecure code. The paper emphasizes that developers should not blindly trust code from any source, whether AI-generated or human-written, and recommends adopting good software engineering practices to mitigate risks. Additionally, future work could further investigate why LLM-generated code has fewer vulnerabilities than code on SO and how to reduce the propagation of insecure code.