TL;DR
Researchers have unveiled INSEC, the first practical black-box adversarial attack against LLM-powered code completion engines like GitHub Copilot. By stealthily injecting a short, optimized comment into a user's code, INSEC can increase the rate of insecure code suggestions by over 50% while maintaining the engine's helpfulness and speed.
What Problem Does This Paper Address?
Large Language Models (LLMs) have revolutionized coding, but they have a known habit of occasionally suggesting insecure code. While previous research explored "poisoning" attacks to exacerbate this issue, those methods required modifying the model's training data or weights. Such actions are impossible for an outsider attacking a commercial service like GitHub Copilot or OpenAI’s API.
This paper introduces a more realistic black-box threat model. In this scenario, the attacker cannot see the model's internals or training data. Instead, they influence the engine by manipulating the input (the code prefix) through a malicious IDE plugin or a compromised dependency. The challenge is to make the attack "stealthy": it must trigger vulnerabilities without breaking the code's functionality or slowing down the user's workflow.
Methods
The authors developed INSEC, which works by inserting a carefully crafted "attack string" as a comment just above where the code completion is needed.
To find these strings, they use a query-based random optimization algorithm. This algorithm starts with several clever initialization strategies:
- TODO: Using comments like
# TODO: fix vul. - Security-Critical Tokens: Using keywords that differentiate secure code from insecure code (e.g., forcing the use of
md5instead ofsha256). - Sanitizer Inversion: Tricking the model into thinking input has already been cleaned, so it skips security checks.
- Inversion: Asking the model to "complete the comment" for a piece of vulnerable code to learn what patterns precede bugs.
The algorithm then iteratively mutates these strings, testing them against the target LLM to see which ones most effectively trigger vulnerabilities. Crucially, the attacker doesn't even need the model's specific tokenizer; they can use a "proxy" tokenizer and still achieve high success rates.
Datasets
The study utilizes several benchmarks to ensure a rigorous evaluation:
- $D_{vul}$: A newly curated dataset of 16 Common Weakness Enumerations (CWEs) across 5 programming languages (Python, Ruby, JavaScript, C/C++, and Go).
- HumanEval-X: A multi-lingual version of the standard HumanEval benchmark used to measure if the attack breaks the functional correctness of the code.
- RepoBench: Used to evaluate how the attack affects repository-level code completion quality.
Key Insights and Ideas
- Instruction-Following as a Double-Edged Sword: Modern LLMs are so good at following instructions that they can be easily "steered" by short comments, even if those comments look like gibberish or minor notes to a human.
- Stealth through Utility: For an attack to be successful in the real world, the user must continue to trust the tool. INSEC succeeds because it barely impacts the "pass@k" rate (the probability that at least one of the top $k$ suggestions is correct).
- Low Barrier to Entry: Developing a highly effective attack string for a specific vulnerability costs less than $10 USD using commercial APIs and standard hardware.
- Real-World Delivery: The authors demonstrated the attack's feasibility by building a functional VS Code plugin that stealthily modifies the queries sent to the GitHub Copilot extension.
Key Results
The results across various models—including StarCoder, CodeLlama, GPT-3.5-Turbo, and GitHub Copilot—are striking:
- Vulnerability Spike: INSEC increased the absolute rate of insecure code generation by an average of 50% across all tested engines.
- Preserved Correctness: For high-capability models like GPT-3.5 and Copilot, the attack had virtually zero impact on the functional correctness of the code.
- Multi-Vulnerability Attacks: By concatenating different attack strings, the researchers could trigger multiple different security flaws simultaneously.
- Initialization Matters: While different strategies worked for different models, "Security-critical token" initialization was the most frequent winner.
Limitations and Future Work
While INSEC is highly effective, the authors note some limitations. Smaller or less capable models showed a more significant drop in functional correctness when attacked, potentially making the attack more noticeable to the end user. Furthermore, while the paper discusses defenses like "comment scrubbing" (removing all comments from prompts), they find that this significantly hurts the LLM's helpfulness for legitimate tasks.
Future work may involve extending these attacks to "coding agents" (LLMs that can execute actions) and developing more robust, "utility-preserving" defenses that can distinguish between helpful developer comments and malicious adversarial strings.
Summary updated