Abstract
This empirical study analyzes over 700 AI-generated code snippets found in real-world GitHub projects, revealing that nearly 30% contain security vulnerabilities across 43 different categories. While these tools significantly boost productivity, the research demonstrates that "Copilot Chat" can successfully fix over half of these issues—but only if developers provide the AI with specific error messages from security scanning tools.
Introduction
As Large Language Models (LLMs) like GitHub Copilot become staples in the developer's toolkit, a "vicious cycle" emerges: AI models trained on public code (which may contain bugs) generate insecure code, which is then committed back to public repositories, potentially training the next generation of models on even more flaws.
Most prior research on AI code security has taken place in controlled "lab" settings. This paper addresses the "in the wild" gap by examining the security posture of AI-generated code that developers are actually using in production-level GitHub projects. The authors sought to understand the real-world risks associated with integrating AI suggestions into active codebases.
Methodology
The researchers followed a multi-step pipeline to move from raw data to verified insights, as illustrated in the figure below.
- Data Mining: Using the GitHub REST API to find projects with keywords like "by GitHub Copilot" or "use Codeium."
- Manual Filtering: Reviewing snippets to ensure they were truly AI-generated, resulting in 733 distinct code files.
- Static Analysis: Scanning code using a "belt and suspenders" approach with CodeQL, Bandit (for Python), and ESLint (for JavaScript).
- Vulnerability Verification: Manual review of scanner results to remove false positives and map issues to the Common Weakness Enumeration (CWE) system.
- Interactive Repair: Testing "Copilot Chat" repair capabilities using various prompt strategies, ranging from simple commands to feedback-enriched prompts.
Datasets
The study produced a curated dataset of 733 code snippets from 116 GitHub projects. The dataset focuses on Python and JavaScript because they are dynamically typed and do not require complex compilation for analysis. GitHub Copilot provided the vast majority of snippets (672), with smaller contributions from Amazon CodeWhisperer (38) and Codeium (23).
Analysis of AI-Generated Suggestions
AI-generated code in real projects is significantly longer and more complex (median 74 lines of code) than snippets typically studied in controlled experiments (averaging 26–40 lines). The nature of vulnerabilities often aligns with language use cases: Python flaws frequently involve system-level operations (e.g., OS Command Injection), while JavaScript flaws are dominated by web-facing issues like Cross-Site Scripting (XSS).
Key Results
- Vulnerability Rate: 27.3% of all analyzed snippets contained at least one security weakness. Python was slightly riskier at 29.5% compared to JavaScript's 24.2%.
- Diversity of Flaws: The team identified 43 different types of CWEs. Alarmingly, eight of these are in the "2023 CWE Top-25," representing the most dangerous software errors in existence.
- Repair Success: The study found a dramatic difference in repair efficacy based on prompting. A standard
/fixcommand had only a 19.3% success rate, whereas an "Enhanced Prompt" including specific technical warning messages from a scanner reached a 55.5% success rate.
Conclusion
While AI coding assistants offer significant productivity gains, they introduce substantial security risks when used "in the wild." The study highlights that some vulnerabilities, such as Code Injection (CWE-94) and OS Command Injection (CWE-78), remain "stubborn" and difficult for AI to fix even with detailed prompts. Future work will explore how different prompt engineering patterns can proactively prevent these vulnerabilities from being generated in the first place.
Summary updated