GO TO A SECTION

↑ ↓ to navigate · Enter to openExplore all sections →
FDEInterviews logo
IEEE S&P 2022

Asleep at the Keyboard? Assessing the Security of GitHub Copilot's Code Contributions

About 40% of Copilot's suggestions at 89 security-sensitive spots had a known weakness. The code already in the file mattered most: 0 of 18 bad with safe neighbors, 17 of 18 with unsafe ones.

Breakdown by FDE Interviews · 11 min read · Updated

About the original · 2021
AUTHORS
Pearce, Ahmad, Tan, Dolan-Gavitt, Karri
FROM
New York University, University of Calgary
PUBLISHED
Aug 2021 · IEEE S&P 2022
CITED BY
1,120Semantic Scholar · Nov 2026
IN THIS BREAKDOWN · 10 SECTIONS

You can read 25% of this breakdown.

01

The paper in one minute

GitHub Copilot, released in preview in June 2021, suggests code from the surrounding context. Its model was trained on public code, which includes insecure patterns, and its documentation warned that suggestions can contain them. This paper measures how often that happens in situations where a security mistake is easy to make.

The authors wrote short program prefixes that stop at the point where a security-relevant line comes next, such as building a database query, copying into a buffer or hashing a password. Each prefix targets one weakness from MITRE's 2021 list of the 25 most dangerous software weaknesses (CWEs). For each, they asked Copilot for up to 25 suggestions, kept those that formed a valid program, and checked each one with GitHub's CodeQL scanner or by hand. They counted a suggestion as vulnerable only when the weakness was definitely present, so partial completions were scored as safe.

They tested three axes. Diversity of weakness covered 18 CWEs with 3 scenarios each in C and Python: 477 of 1,084 valid programs were vulnerable (44%), and the top-ranked suggestion was vulnerable in 24 of 54 scenarios. Diversity of prompt took one SQL task and changed the prompt in 16 small ways, such as rewording comments, adding an author name or adding nearby code: 152 of 407 were vulnerable. Diversity of domain asked for hardware descriptions in Verilog for 6 hardware CWEs: 56 of 198 were vulnerable, and Copilot often failed to produce valid code.

Across all 89 scenarios, about 40% of suggestions contained the weakness. Some weaknesses were far more common than others: NULL pointer dereference and OS command injection appeared in more than 80% of suggestions for their scenarios. The clearest prompt effect was the code already present: with a safe database function nearby, none of 18 SQL suggestions was vulnerable; with an unsafe one, 17 of 18 were. The authors also note outdated practices, such as weak password hashing, persisting from older training code.

THE IDEA, VISUALLY
VULNERABLE SHARE BY CWE (TABLES I-II) 476 NULL pointer dereference 88% 78 OS command injection 83% 522 weakly protected credentials 66% 22 path traversal 60% 89 SQL injection 58% 434 unrestricted file upload 51% 190 integer overflow 49% 119 buffer bounds 40% 125 out-of-bounds read 37% 798 hard-coded credentials 35% 200 information exposure 35% 787 out-of-bounds write 35% 502 unsafe deserialization 29% 306 missing authentication 29% 416 use after free 28% 732 incorrect permissions 24% 20 input validation 22% 79 cross-site scripting 19% All 54 scenarios: 477 of 1,084 valid programs, 44%. arXiv 2108.09293, NYU and Calgary, IEEE S&P 2022 ONE SQL TASK, SMALL PROMPT CHANGES (TABLE III) control 6/25 author flag: library author 4/25 author flag: paper author 11/24 tabs 9/25 misspellings 13/23 comment reworded 10/25 'fixed sql injection' note 12/25 'in a secure manner' 10/25 comment removed 13/25 docstring 6/25 credentials from env vars 8/25 safe SQL function nearby 0/18 unsafe SQL function nearby 17/18 postgres library 6/24 sqlite3 library 9/25 Line: the control's 24%. At 25 samples, only the unsafe-neighbor case clears chance after 16 comparisons (our Fisher tests).
Left: share of Copilot's valid completions that contained the targeted weakness, per CWE, summed over three scenarios each (Tables I and II). Right: Table III, one SQL task under small prompt changes; the line marks the control's rate, and only the unsafe-neighbor case differs beyond chance at 25 samples.
Research notes: the problem, approach and results
685 of 1,689

valid generated programs that contained the targeted weakness, about 40% (our sum of Tables I to IV)

35 of 89

scenarios where Copilot's top-ranked suggestion was vulnerable (Section V)

0/18 vs 17/18

vulnerable SQL suggestions with a safe vs an unsafe database function already in the file (Table III)

THE PROBLEM

Code assistants trained on public repositories learned from code that includes insecure patterns. Teams adopting them need to know how often suggestions carry known weaknesses, and what in the context makes that more or less likely.

THE IDEA

Write 89 short programs that stop where a security-relevant completion begins, covering 18 of MITRE's 2021 Top 25 software weaknesses, variations of one SQL task, and hardware weaknesses in Verilog; collect up to 25 Copilot suggestions for each; check them with CodeQL or by hand.

THE RESULT

685 of 1,689 valid programs (about 40%) contained the targeted weakness, and the top-ranked suggestion was vulnerable in 35 of 89 scenarios. Rates ran from 88% for NULL pointer dereference to 19% for cross-site scripting. Unsafe code nearby pushed the SQL task to 17 of 18 vulnerable.

02

Why it matters to an FDE

Generated code needs security review like any other code. About 40% of suggestions in these risky spots had a known weakness, and the top suggestion was vulnerable in 39% of scenarios. When a customer team adopts a coding assistant or an agent that writes code, keep static analysis, dependency checks and review in the path. Do not let the speed of generation remove them.

The surrounding code sets the pattern. The strongest effect in the study was the code already in the file. If a codebase builds queries by string concatenation, the assistant will copy that. Fixing the existing patterns, and putting safe helper functions where the assistant can see them, is a better lever than prompt wording.

Prompt wording is mostly noise at this sample size. Adding "in a secure manner" or a note that SQL injection was fixed did not reliably help. In our recount, only the unsafe-neighbor case clears chance after allowing for 16 comparisons. Do not promise a customer that comments will make generated code safe.

Some weakness classes are much riskier. Memory-safety bugs in C, OS command construction and credential handling were the worst. Point scanners and reviewers at those first in generated code.

This also applies to agents. Code agents that write and run code at scale produce the same patterns faster. Put the security scanner inside the agent's loop or the merge gate, not after deployment.

THE FULL BREAKDOWN

Understand the mechanism. Try it on a real problem.

Unlock this breakdown and 290 more papers.

Premium opens every paper in full, plus every answer, concept, course and guide on the site. One payment for six months of access.

6 months · One payment · No auto-renewal

Get Premium · ₹2,000 / $25
  • ◇1 architecture diagram
  • </>1 runnable code experiment
  • ▦2 results tables
  • ?5 interview questions with answers
  1. 03Key takeaways

    The three axes, the scoring rule, the headline rates, which CWEs were worst and what moved the rate

    Free account
  2. 04How it works

    Building scenarios, collecting options, CodeQL and manual checks, and the three axes

    Premium
  3. 05The numbers that matter

    Totals per axis, per-language and per-CWE rates, Table III, and two slips in the reported totals

    Premium
  4. 06Try it yourself

    Re-add the tables, test the prompt variants against chance, and run a cheap review gate

    Premium
  5. 07Where it breaks

    Built to be risky, one 2021 model, conservative and partly manual scoring, and small samples

    Premium
  6. 08Using it on a deployment

    Keep security gates on generated code, fix patterns in the codebase, focus review on risky classes

    Premium
  7. 09Interview angles

    Five questions this paper prepares you for, with answer hints

    Premium
Back to the paper library →