TL;DR
AI code generation tools like GitHub Copilot are rapidly adopted, but their security implications are not fully understood. This study empirically analyzes code generated by Copilot and other tools on GitHub, finding that a significant portion contains security weaknesses across various categories, including critical ones like insufficient randomness and code injection. While Copilot Chat shows promise in fixing these issues, its effectiveness varies, highlighting the continued need for developer vigilance and robust security analysis.
What Problem Does This Paper Address?
The increasing integration of AI-powered code generation tools, such as GitHub Copilot, into developer workflows promises to boost productivity. However, these tools often learn from vast amounts of publicly available code, which may contain existing vulnerabilities. This raises concerns about whether AI-generated code itself introduces new security weaknesses or amplifies existing ones, potentially leading to insecure software. While prior research has explored code generation's correctness and effectiveness, a comprehensive empirical study focusing on the security of AI-generated code in real-world, open-source development scenarios has been limited. This paper aims to fill that gap by investigating the security weaknesses present in code generated by popular AI tools and exploring the potential of AI-assisted tools like Copilot Chat to fix these issues.
Methods
The researchers conducted an empirical study involving several key steps:
- Data Collection: They used GitHub's REST API to search for projects and files containing AI-generated code, focusing on Python and JavaScript due to their popularity and dynamic typing. They employed keyword-based searches and manual filtering to identify relevant code snippets.
- Security Analysis: Collected code snippets were analyzed using static analysis tools, specifically CodeQL, Bandit (for Python), and ESLint (for JavaScript). The study aimed for broad coverage by using multiple tools and considering any detected security weakness as a vulnerability.
- Weakness Classification: Detected security weaknesses were manually mapped to Common Weakness Enumeration (CWE) categories to understand their nature and prevalence.
- Fixing with Copilot Chat: A subset of code snippets with identified security weaknesses was used to evaluate Copilot Chat's ability to fix these issues. They tested different prompting strategies, including a basic prompt, a slash command (/fix), and an enhanced prompt with detailed warning messages.
- Analysis and Interpretation: The results were statistically analyzed to determine the frequency and types of security weaknesses, their distribution across application domains, and the effectiveness of Copilot Chat in fixing them.
Datasets
The primary dataset for this study was collected from GitHub. The researchers gathered code snippets from open-source projects that explicitly indicated the use of AI code generation tools like GitHub Copilot, CodeWhisperer, and Codeium. They specifically targeted Python and JavaScript code. The dataset comprised 733 code snippets in total, with 672 from Copilot, 38 from CodeWhisperer, and 23 from Codeium. A curated dataset, including code snippets, analysis results, and filtered results, has been made available online for replication purposes.
Key Insights and Ideas
- Ubiquitous Security Weaknesses: AI-generated code is not inherently secure. A significant percentage of code snippets from popular tools contain security flaws, underscoring the need for caution.
- Diversity of Threats: The security weaknesses are not confined to a few common issues but span a wide range of CWE categories, meaning developers face a broad spectrum of potential risks.
- Top-25 CWEs are Present: Many of the identified weaknesses fall into the MITRE's Top-25 Most Dangerous Software Errors list, indicating that AI tools are generating code with well-known, high-severity vulnerabilities.
- Language and Domain Specificity: While certain CWEs are common across languages, others are more prevalent in specific languages (e.g., OS Command Injection in Python, Cross-site Scripting in JavaScript) and application domains (e.g., Web Applications for JavaScript, Utility Tools for Python).
- Copilot Chat's Potential, But Not a Silver Bullet: Copilot Chat can fix a notable portion of security issues, but its success rate is highly dependent on the type of weakness and the quality of the prompt. Enhanced prompts with specific context significantly improve its effectiveness.
Key Results
- Prevalence of Weaknesses: Approximately 27.3% of the 733 analyzed code snippets contained security weaknesses. This breaks down to 29.5% for Python snippets and 24.2% for JavaScript snippets.
- Number of Weaknesses: A total of 628 security weaknesses were identified across the 200 snippets that contained at least one weakness. The average incidence was 3 security weaknesses per code snippet.
- Top CWEs: The most frequent CWEs identified were CWE-330 (Use of Insufficiently Random Values), CWE-94 (Improper Control of Generation of Code), and CWE-79 (Cross-site Scripting).
- CWE Top-25: Eight of the identified CWEs were among the 2023 CWE Top-25 list, accounting for a substantial portion of the detected weaknesses.
- Copilot Chat Fix Rate:
- Using the
/fixcommand alone fixed 19.3% of security weaknesses.
- Using the
- A basic prompt improved this to 31.8%.
- An enhanced prompt, providing warning messages, achieved the highest fix rate of 55.5%.
- Fix Effectiveness Varies: Copilot Chat was highly effective for issues like hard-coded passwords (CWE-259) and incorrect function arguments (CWE-685), but struggled with complex issues like command injection (CWE-78, CWE-94).
Limitations and Future Work
Limitations:
- Construct Validity: The study acknowledges potential biases in keyword-based search, manual data filtering, and manual CWE association. They mitigated this by using multiple annotators and a security expert.
- External Validity: The dataset was primarily sourced from GitHub, which might not be fully representative of all open-source development environments. The study also noted a higher proportion of code snippets from game projects, potentially limiting the comprehensiveness of security scenarios. The static analysis tools themselves have limitations in coverage and can produce false positives.
- Tool Limitations: Static analysis tools cannot detect all types of vulnerabilities.
Future Work:
The authors plan to:
- Collect more code snippets from a wider range of open-source repositories and newer AI tool releases.
- Analyze the application scenarios of these snippets and study how practitioners use Copilot and fix issues.
- Compare their findings with other emerging Generative AI code generation tools.
Summary updated