[Paper Review] How Secure is Code Generated by ChatGPT?
The paper evaluates the security of code generated by ChatGPT across 21 programs in multiple languages, finding frequent security vulnerabilities and that secure variants often require explicit prompting or iterative refinement.
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.
Motivation & Objective
- Assess the security posture of code generated by ChatGPT across multiple languages.
- Identify common vulnerability classes present in ChatGPT-generated code.
- Evaluate whether prompting can guide ChatGPT to produce more secure code.
- Discuss ethical considerations of using AI for code generation in security-sensitive contexts.
Proposed method
- Generate 21 programs in five languages (C, C++, Python, HTML, Java) using ChatGPT from GPT-3.5-series fine-tuned model.
- Evaluate each program for vulnerabilities related to known weaknesses (e.g., SQL injection, memory corruption, deserialization, XSS).
- Probe ChatGPT about vulnerabilities and request secure or corrected versions when vulnerabilities are found.
- Analyze results to determine whether ChatGPT recognizes vulnerabilities and how often secure code is produced after prompting.
- Discuss ethical implications and propose interaction strategies for improving security in generated code.

Experimental results
Research questions
- RQ1How secure are the code samples generated by ChatGPT across different languages and use-cases?
- RQ2Can targeted prompting or follow-up questions lead ChatGPT to produce more secure code?
- RQ3What types of vulnerabilities are most common in ChatGPT-generated code, and which require substantial code redesign to fix?
- RQ4What ethical considerations arise from using AI-generated code in security-critical contexts?
Key findings
- Only a minority of initial programs were secure; many exhibited significant vulnerabilities.
- ChatGPT often recognizes vulnerabilities when questioned but frequently does not produce secure code without explicit prompting.
- Corrected secure versions could be produced in several cases, but not universally; some tasks inherently require substantial redesign to be secure.
- Vulnerabilities spanned many categories, with memory corruption and cryptographic misuses showing notable difficulty for secure fixes.
- The study discusses pedagogical value and ethical concerns, noting that security benefits depend on user-initiated analysis and testing.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.