NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Is Your AI-Generated Code Really Safe? Evaluating Large Language Models on Secure Code Generation with CodeSecEval

Jiexin Wang, Xitong Luo, Liuwen Cao, Hongkui He, Hailin Huang, Jiayuan Xie, Adam Jatowt, Yi CaiarXiv 2024 · DOI

Abstract

Large Language Models (LLMs) are increasingly used as coding assistants, yet they often inherit and propagate security vulnerabilities from their training data. This paper presents CodeSecEval, a novel benchmark designed to evaluate the security of AI-generated code. By testing seven leading LLMs across 180 samples and 44 CWE types, we find that while models frequently prioritize functionality over safety, "vulnerability-aware" prompting can significantly improve secure code generation. Our results show that top-tier models like Claude 3 Opus can nearly triple their success rate in generating safe code when provided with security-focused instructions.

Introduction

The "black box" nature of LLMs means their output is only as secure as the data they were trained on. With an estimated 81% of open-source codebases containing vulnerabilities, AI assistants like GitHub Copilot risk suggesting insecure code. Existing benchmarks for evaluating code security often lack executable test cases or ground-truth pairs for repair tasks. CodeSecEval addresses these limitations by providing a rigorously curated, fully runnable dataset with automated test cases.

The CodeSecEval Dataset

CodeSecEval is a meticulously curated dataset of 180 samples covering 44 critical vulnerability types (CWEs). It is divided into SecEvalBase, which enhances existing security datasets with executable code and test cases, and SecEvalPlus, which focuses on the "2023 CWE Top 25 Most Dangerous Software Weaknesses."

Comparison of CodeSecEval with existing security benchmarks, highlighting its completeness, executable code, and inclusion of test cases.

As shown in the comparison above, CodeSecEval distinguishes itself by ensuring all samples are fully runnable and includes automated test cases to verify both functional correctness and security—a significant advancement over previous datasets like SecurityEval or CyberSecEval.

Methodology

We evaluated seven LLMs (InCoder, CodeGen, StarCoder, CodeLlama-Instruct, GPT-3.5, GPT-4, and Claude 3 Opus) using four experimental setups: Direct Code Generation, Vulnerability-aware Generation, Direct Code Repair, and Repair with Explanation. Success was measured using the Pass@k metric, which calculates the probability that at least one of the top $k$ generated samples passes all tests.

Example of a CodeSecEval entry showing a vulnerability-aware problem and its secure resolution for OS Command Injection (CWE-78).

Results and Analysis

The evaluation revealed a significant "security blind spot" in modern LLMs. By default, models often prioritize functionality over safety. However, the most effective way to improve security is through better prompting.

Claude 3 Opus and GPT-4 dominated the benchmark. In the vulnerability-aware generation task, Claude 3 Opus achieved a Pass@1 score of 39.89%, a nearly 3x improvement over its direct generation score of 13.83%. Interestingly, while large models improved with explanations during code repair, smaller models like StarCoder sometimes performed worse, suggesting that added complexity can be counterproductive for less capable models.

Conclusion

CodeSecEval provides a vital tool for measuring and improving the security of AI-generated code. Our findings underscore the importance of "vulnerability-aware" prompting and suggest that future improvements in AI security will come from both better models and more sophisticated interaction strategies.

Summary updated

BibTeX
@misc{wang2024is,
  author = {Jiexin Wang and Xitong Luo and Liuwen Cao and Hongkui He and Hailin Huang and Jiayuan Xie and Adam Jatowt and Yi Cai},
  title = {{Is Your AI-Generated Code Really Safe? Evaluating Large Language Models on Secure Code Generation with CodeSecEval}},
  year = {2024},
  eprint = {2407.02395},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2407.02395},
}
Figure