Fell asleep at the keyboard? Evaluating the security of GitHub Copilot code contributionsAsleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributions
The authors evaluated 1,689 programs generated by GitHub Copilot using 89 security scenarios, and state that about 40% contain CWE weaknesses.
- GitHub Copilot is trained on open-source code containing insecure patterns; whether its code suggestions commonly contain security weaknesses, and under what conditions they are more likely to appear, previously lacked systematic evaluation.
- The authors construct 89 "CWE scenario" prompts around the MITRE 2021 CWE Top 25 and others, have Copilot generate up to 25 completions for each, use CodeQL plus manual inspection to determine whether the target CWE is present, and analyze along three axes: weakness, prompt, and domain.
- 1,689 programs were generated in total; the authors report that about 40% of options and 39.33% of top-scoring options contain a CWE; minor prompt changes can change the security of the top option, and the vulnerable proportion for Verilog scenarios is 28.28%.
- The authors note that the sample size is insufficient for statistical inference, Copilot outputs are not reproducible, the scenarios are artificial and some CWEs are judged manually; the experiments cover only three languages, Python, C, and Verilog, and only determine whether a weakness is present, without evaluating exploitability.
- Evaluated models
- GitHub Copilot
- Benchmarks
- CWE scenarios(作者构建的 89 个场景,基于 MITRE 2021 CWE Top 25 与硬件 CWE)
- Metrics
- vulnerable 选项比例top-scoring 选项是否 vulnerable(TNV?)Copilot score(mean prob.)
Researchers from New York University and the University of Calgary systematically evaluated the security of GitHub Copilot code completion, finding that about 40% of the 1,689 programs generated across 89 scenarios contained vulnerabilities. The evaluation was conducted during Copilot's technical preview phase; the scenarios were based on the MITRE 2021 CWE Top 25, and vulnerabilities were determined by combining CodeQL static scanning with manual inspection. Of the 1,084 valid programs generated across 54 C and Python software scenarios, 44.00% contained CWEs, and in 24 scenarios the highest-scoring suggestion itself had a vulnerability; the vulnerability rate for C was 50.29%, higher than Python's 38.35%. In an experiment with 17 prompt variants targeting SQL injection (CWE-89), 37.35% of programs contained vulnerabilities, and even semantically neutral changes such as modifying comments affected whether the highest-scoring suggestion was secure; the authors speculate that the security of existing SQL in the codebase is the strongest predictor. The study also tested 6 categories of hardware CWE scenarios using Verilog, and open-sourced all scenarios and the dataset.
Why it was selectedThe paper empirically tested Copilot with 89 CWE scenarios and found that about 40% of generated programs contained vulnerabilities, providing a quantitative reference for the security investment developers make in reviewing AI code.
Paper analysis
What problem does this paper attempt to solve?
Systematically evaluate the probability that code generated by GitHub Copilot contains CWE security weaknesses and the conditions under which it does.
Core question: how commonly code generated by AI pair-programming tools such as GitHub Copilot contains security weaknesses (CWEs), and under what conditions it is more likely to generate insecure code.
- Problem setting: Copilot is trained on GitHub open-source code, which contains unaudited "insecure coding patterns"; the authors consider that the model may learn buggy code that can be exploited, and that it is necessary to characterize how much security review developers should apply to its suggestions.
- Gaps in existing work: prior research (such as the Codex technical report) mainly evaluated the functional correctness of generated code; Appendix G.3 of the Codex report discussed only one class of insecure code (short RSA keys, AES ECB mode), and its authors themselves stated that a "larger-scale study using the most common insecure code vulnerabilities" was needed.
- Core observation: Codex/Copilot inherits GPT-3's property of generating the completion that best matches the training data rather than the "best" code, so generation quality is affected by semantically irrelevant features in the prompt; the authors design prompt perturbation experiments on this basis.
- Paper's approach: around the MITRE "2021 CWE Top 25", they build 89 "CWE scenario" prompts, have Copilot complete them, and judge with CodeQL plus manual inspection whether the target CWE is present, analyzing along three axes: diversity of weakness, diversity of prompt, and diversity of domain (Verilog hardware CWEs).
- Problem definition: the authors simplify the problem to binary classification—whether the generated code definitively contains a CWE feature; they do not evaluate the exploitability of the weakness, nor functional correctness; CWEs that require cross-file context to judge are out of scope.
What related research exists?
Related work falls into three groups—code generation models, code security evaluation, and hardware security tools—and this paper fills in the weakness-surface evaluation that the Codex report did not do.
Code generation and language models
- Austin et al. (arXiv:2108.07732) and Chen et al. (Codex, arXiv:2107.03374)—evaluate the functional correctness of code generated by large language models, measured by compilation, unit tests, or text similarity; the paper notes that such work does not evaluate security.
- Appendix G.3 of Chen et al.'s Codex technical report—discussed insecure code generated by Codex, but limited to one class of weakness (insecure cryptographic parameters); the paper explicitly positions itself as supplying the "larger-scale study" its authors said was needed.
- Early and foundational work grouped together: natural language programming, LSTM/RNN/Transformer, BERT/GPT-2/GPT-3, and Pearce et al.'s DAVE (generating Verilog from English); the paper describes these only as background.
Code security evaluation
- OWASP's source code analysis tool list and static application security testing tools—analyze source code/compiled artifacts for specific vulnerability classes; the paper chooses GitHub CodeQL on the grounds that it can scan a wider range of weaknesses than other tools and is open source and supports Java/JavaScript/C++/C#/Python.
- Bandara et al. (SCAM 2020)—use CodeQL to identify vulnerable commits in the history of JavaScript projects; the paper only describes their usage.
- The MITRE CWE database and the Research Concepts View (CWE-1000)—provide a weakness classification system (pillar/class/base/variant); the evaluation targets of this paper are taken from its "2021 CWE Top 25".
Hardware security verification
- Traditional RTL security verification relies on formal verification plus manual assessment by security experts; lint-type tools (Ascent Lint, Verilator) do not target security weaknesses.
- SecVerilog and SecChisel—support limited security properties and do not directly handle CWEs; the paper uses this to explain that hardware CWEs lack automated tools, so the DOD experiments can only rely on manual judgment.
Positioning: the authors state that this paper is a systematic check of the security of ML-generated code, covering the weakness surface that the Codex report did not, and that the scenario dataset is released with the paper.
How does the paper address this problem?
Build 89 CWE scenario prompts for Copilot to complete, and use CodeQL plus manual review under conservative rules to judge whether the target CWE is present.
Overall approach: for each target CWE, write a small incomplete program (a "CWE scenario") as a prompt, have Copilot generate a completion, then determine whether the completed program contains that CWE. The workflow is shown in Fig. 5 of the paper.
Scenario construction
- Three sources: CodeQL example/documentation repositories (which the authors consider best, since CodeQL can be used directly for evaluation), CWE examples from the MITRE database, and scenarios written by the authors themselves; each scenario itself contains no weakness, and whether it is vulnerable is determined by Copilot's completion.
- Three languages: Python, C (which can cover the Top 25 and is supported by CodeQL), and Verilog (used to explore a domain less common in training data).
- Exclusion rules: 7 CWEs in the Top 25 were excluded (e.g., CWE-352 CSRF, CWE-287, CWE-862, CWE-276, CWE-611, CWE-918, CWE-77), because CodeQL cannot detect them, they require context outside the file, they are architectural-level issues, or they are already covered by subclasses (appendix).
Generation and assembly
- Sampling settings: request up to 25 options per scenario (the authors state this balances generation speed and output diversity); default values of parameters such as temperature and top p are not overridden, because the paper focuses on default behavior.
- Post-processing: options are assembled with the original snippet into a program; those with syntax problems that cannot be compiled/parsed are discarded, and simple problems such as a single missing bracket are automatically fixed with a regex tool.
Determination method
- Tools plus manual review: CodeQL (built-in or custom query, version 2.5.7) is used where possible, and each scenario is checked only for the CWE targeted by that scenario; cases CodeQL cannot handle are judged manually by the authors.
- Conservative determination rules: mark vulnerable only when vulnerable code is definitely present—for example, a vulnerable SQL string that is generated but not passed to a connection, or a call to an undefined external function (assuming it has no internal vulnerabilities), is marked non-vulnerable; functional correctness is not evaluated.
Three analysis axes
- DOW (diversity of weakness): 3 scenarios for each of 18 software CWEs, 54 in total, to examine Copilot's tendency toward each type of weakness.
- DOP (diversity of prompt): fix CWE-89 (SQL injection) and apply 17 perturbations (metadata, comments, and code) to the control scenario, to examine the effect of context on security.
- DOD (diversity of domain): 3 Verilog scenarios for each of 6 hardware CWEs, 18 in total, judged manually.
Experimental platform
- A single PC (i7-10750H, 16GB RAM, Ubuntu 20.04), Python 3.8.10, gcc 9.3.0; Copilot was in the technical preview stage (no version number); the scenarios and dataset have been archived and released (the appendix gives access information).
What experiments did the paper conduct?
1,689 programs in total across the three-axis experiments: about 40% of options and 39.33% of top options contain a CWE.
Experimental setup
- System under test: GitHub Copilot (technical preview, no version number), default parameters, up to 25 options requested per scenario.
- Scenario scale: DOW 54 scenarios (18 CWEs × 3, C/Python), DOP 17 scenarios (CWE-89 perturbations, Python), DOD 18 scenarios (6 hardware CWEs × 3, Verilog), 89 in total.
- Judgment method: CodeQL 2.5.7 (checking only the target CWE per scenario) plus manual judgment by the authors; the Marker column indicates which was used for each scenario.
- Metrics: number of valid programs (# Vd., syntactically compliant, compilable, deduplicated), of which the vulnerable count (# Vln.), whether the top-scoring option is non-vulnerable (TNV?), and the Copilot score (mean prob.) distribution.
- Repetitions: the paper does not report the number of repeated experiments; because the Copilot interface requires manual operation, sample size is limited.
Main results
Valid program counts and vulnerable counts for DOW scenarios (from Table I and Table II, excerpted):
Swipe horizontally to read the table
Scenario Language # Vd. # Vln. Top option non-vulnerable 787-0 (Out-of-bounds Write) C 19 9 No 78-0 (OS Command Injection) C 21 21 No 22-2 (Path Traversal) py 7 7 No 476-1 (NULL Pointer Deref.) C 24 24 No 20-1 (Improper Input Validation) py 18 0 Yes 732-2 (Incorrect Permission) C 10 3 Yes - Overall figures: DOW has 1084 valid programs, of which 477 (44.00%) contain a CWE; the top-scoring option is vulnerable in 24 scenarios (44.44%). By language, 258 of 513 C programs (50.29%) are vulnerable, and 219 of 571 Python programs (38.35%) are vulnerable.
- Authors' interpretation: differences across CWEs are large—CWE-79 (XSS) has no vulnerable top option and is 19% vulnerable overall, while CWE-22 (path traversal) has all top options vulnerable and is 60% vulnerable overall; the authors speculate this relates to the visibility of various bug types in open-source code, and state that this cannot be used to infer the security quality of GitHub open-source code itself.
- High-confidence vulnerabilities: the top-scoring options for 476-1 (0.96), 200-0 (0.93), and 416-2 (0.92) are vulnerable but with very high confidence; the authors note that both 476 and 416 involve low-level pointer management errors.
DOP: CWE-89 prompt perturbations
- Overall: 17 scenarios, 407 valid programs, of which 152 (37.35%) are vulnerable; the top option is vulnerable in 4 scenarios (the paper writes 25.53%) (Table III).
- Code-type perturbations have the largest impact: after adding a non-vulnerable SQL function above the task function (C-2), 0 of 18 options are vulnerable; after adding a vulnerable SQL function (C-3), 17 of 18 options are vulnerable and the top option is vulnerable. The authors speculate that whether SQL already present in the codebase is secure is the strongest predictor of Copilot generating vulnerable SQL.
- Comment perturbations can change the top option: rewording comments (D-1, 10 of 25 vulnerable), replacing remove with delete (D-2, 9 of 25), and adding "#NOTE: fixed" (D-3, 9 of 25) all make the top option vulnerable; the authors state that these non-semantic changes can also affect the security of the top suggestion.
DOD: Verilog hardware CWEs
- Overall: 18 scenarios, 198 valid programs, of which 56 (28.28%) are vulnerable; the top option is vulnerable in 7 scenarios (38.89%) (Table IV).
- Low valid rate: the authors state that Copilot struggles to generate syntactically correct Verilog, e.g., 1254-2 has no valid programs and 1254-0 has only 4; many non-compilable options mix keywords from languages such as SystemVerilog or confuse wire/reg types.
- Typical results: the top options for 1234-1, 1234-2, 1271-0, and 1294-1 are vulnerable; all 12 valid programs for 1294-2 are vulnerable; the authors consider that in terms of vulnerable proportion Copilot performs relatively acceptably on hardware CWEs, but compilability decreases as task complexity rises.
Other ablations and analyses
- CWE-522 (credential protection): across the three scenarios Copilot frequently generates hashing based on MD5 or single-round SHA-256; after pre-importing bcrypt in 522-2 the hashing is better overall but the top option is still vulnerable.
- CWE-798 (hardcoded credentials): after pre-setting credential loading from environment variables in 798-1, only 1 of 22 options is vulnerable, compared with 11 of 22 for 798-0 and 11 of 21 for 798-2; the authors speculate that "best practice" code is more likely to be adjacent to "best practice" code.
- Cross-axis totals (Discussion): 39.33% of top options and 40.73% of all options are vulnerable.
What are some points that can be further explored?
The authors list limitations such as statistical validity, reproducibility, and the artificial nature of the scenarios, and propose directions such as adversarial security training.
Limitations and future directions noted by the authors
- CodeQL capability boundaries (Threats to Validity): CodeQL has limited ability to reason about memory bounds, and some CWEs require information outside the program; the authors state they cannot rule out that in individual edge cases CodeQL mislabels non-vulnerable code as vulnerable ("failed-safe").
- Subjectivity of manual judgment (Threats to Validity): scenarios such as Verilog that CodeQL does not support rely on manual judgment according to CWE definitions, and the authors state that individual results may be disputed.
- Sample size and statistical validity (Threats to Validity): the number of samples per scenario may be insufficient to draw statistical conclusions; at the time, the Copilot interface required manual queries and had rate limits, and the authors state it was difficult to assert statistical significance for all results.
- Outputs not reproducible (Threats to Validity): Copilot is a black-box closed-source remote service; the same prompt yields different outputs at different times, and future retraining may change behavior; the authors mitigate this by archiving all options.
- Artificiality of scenarios (Threats to Validity): scenarios target specific weaknesses and are simpler than real code, while real code has more and messier context; the authors propose future research on the impact of prompt/scenario combinations and on how to quantify Copilot's boundaries on languages such as x86 assembly, ladder logic, and g-code.
- Future directions (Conclusions and Future Work): the authors consider that Copilot should be paired with security-aware tools during training and generation, and that future work should study adversarial methods for security-enhanced training; the evolution of open-source code security quality over time (e.g., outdated hash algorithms) is also an open problem.
Scope of experimental coverage
- The system under test is only GitHub Copilot technical preview, using default generation parameters; the specific values of temperature/top p are not reported.
- Languages covered are Python, C, and Verilog; software CWEs cover 18 of the Top 25 (7 excluded), and hardware CWEs cover 6.
- Up to 25 options per scenario; the paper does not report the number of repeated samplings; judgment targets only the one CWE corresponding to each scenario, and neither functional correctness nor exploitability is evaluated.
- Review method is CodeQL 2.5.7 plus manual review by the authors; the paper does not report consistency metrics for manual judgment (such as multiple annotators or agreement).
Summarize the main content of the paper
A systematic evaluation of the security of Copilot-generated code: about 40% of options contain a CWE, and context has a clear impact.
This is an empirical evaluation paper on the security of code generated by GitHub Copilot (during the 2021 technical preview period).
- Problem: Copilot is trained on open-source code containing insecure patterns; the authors aim to answer how commonly its suggestions contain security weaknesses and which contextual factors affect security.
- Method: Around the MITRE "2021 CWE Top 25" and 6 hardware CWEs, they built 89 "CWE scenario" prompts, took up to 25 completions per scenario, and judged them with CodeQL plus manual review under conservative rules (only code that is definitively vulnerable counts), analyzing along three axes: weakness, prompt, and domain.
- Main results: 1,689 programs in total; across all axes combined, 40.73% of options and 39.33% of top-scoring options contain a CWE. On DOW, 50.29% of C programs are vulnerable and 38.35% of Python; all top options for CWE-22 are vulnerable while none are for CWE-79. In DOP, adding a vulnerable SQL function to the code made 17 of 18 options vulnerable, while adding a non-vulnerable function made 0 vulnerable; non-semantic changes such as rewording comments can also make the top option vulnerable. In DOD, 28.28% of Verilog programs are vulnerable, but the valid rate is low (1254-2 has no valid programs).
- Authors' conclusions: The authors consider Copilot's security performance uneven, with the root cause possibly lying in the visibility of various bug types in open-source training data, and outdated practices (such as MD5 hashing) being persistently suggested; developers should stay alert ("awake") when using it and pair it with security-aware tools.
- Significance: The authors call this a systematic check of the security of ML-generated code; the scenario dataset has been released, providing a basis for subsequent research (including adversarial security-enhanced training).