Directed 'Code Manual' Hijacking in Retrieval-Augmented Code Generation

Kai Ye, Liangcai Su, Chenxiong QianCCS 2025

TL;DR

This paper introduces "ImportSnare," a novel attack framework that exploits Retrieval-Augmented Code Generation (RACG) systems by poisoning documentation to trick LLMs into recommending malicious packages. It achieves high success rates even with low poisoning ratios, highlighting critical supply chain risks in AI-powered development.

What Problem Does This Paper Address?

Large Language Models (LLMs) are revolutionizing code generation, but the code they produce often contains bugs and security vulnerabilities, requiring manual debugging. Retrieval-Augmented Generation (RAG) aims to improve correctness and security by leveraging external code manuals. However, this integration introduces new attack surfaces. Specifically, this paper addresses the vulnerability of RACG systems to malicious dependency hijacking, where attackers can poison the documentation that RAG systems use, leading LLMs to suggest insecure or malicious third-party packages to developers. This exploits a "dual trust chain": the LLM's trust in RAG-retrieved documents and developers' trust in LLM suggestions.

Methods

The core of the attack, "ImportSnare," employs two synergistic strategies:

  1. ImportSnare-R (Ranking Sequence): This component uses a "position-aware beam search" to craft adversarial text that subtly alters documents. The goal is to increase the ranking of poisoned documents in retrieval results without being easily detectable by humans. It optimizes for embedding similarity between poisoned documents and proxy queries.
  2. ImportSnare-G (Inducing Sequence): This component generates "multilingual inductive suggestions" that are embedded into code comments. These suggestions are designed to manipulate LLMs into recommending specific malicious dependencies, often by framing them as safer or more robust alternatives to legitimate packages. This is achieved through LLM self-paraphrasing and leveraging multiple languages for broader applicability.

Datasets

The authors evaluated their framework using datasets for Python, Rust, and JavaScript.

  • RAG Database: Comprised code snippets from Huggingface, including explicit third-party package/library declarations, aiming to mimic real-world RAG retrieval scenarios.
  • Query Dataset: Consisted of real-world programming queries, split into 80% for poisoning and 20% for testing.

The paper also mentions the use of various LLMs and retrievers for their experiments, including DeepSeek, GPT-4, Claude, LlamaIndex, and LangGraph.

Key Insights and Ideas

  • Dual Trust Chain Exploitation: The attack effectively leverages both the LLM's reliance on RAG-retrieved information and the developer's tendency to trust LLM suggestions, creating a potent vulnerability.
  • Stealthy Poisoning: The use of nonsensical or subtly altered text in ranking sequences makes the poisoned documents difficult for humans to detect, enhancing the attack's stealth.
  • Low Poisoning Ratio Efficacy: The attack is effective even with very low poisoning ratios (as low as 0.01% of the total RAG database), making it practical for real-world scenarios where large databases are involved.
  • Transferability: The framework demonstrates transferability across different LLMs, RAG systems, and programming languages, indicating a broad threat.
  • "Safe" Naming Conventions as Vulnerabilities: Malicious packages with names like "safe" or "robust" are more likely to be recommended by LLMs, turning common security-oriented naming conventions into an attack vector.

Key Results

  • High Attack Success Rate (ASR): ImportSnare achieved ASRs exceeding 50% for popular libraries like matplotlib and seaborn against state-of-the-art LLMs (e.g., 67% on GPT-40, 51% on DeepSeek-r1).
  • Low Poisoning Ratio: The attack remained effective with poisoning ratios as low as 0.01% of the RAG database.
  • Precision@k: Achieved values exceeding 5 for Python/Rust targets, indicating over 50% of poisoned documents ranked in the Top-10 retrievals.
  • Query Efficiency: Required only about 3 surrogate queries on average to establish attack relevance.
  • Code Quality: Generated code using poisoned documentation showed no statistically significant deviation in quality metrics (security issues, Pylint score, Flake8 errors) compared to code generated from clean documentation, meaning the attack doesn't degrade the LLM's baseline code quality.

Limitations and Future Work

  • Real-world RAG Scale: The experimental RAG databases are significantly smaller than real-world systems, though the authors argue that the attack's effectiveness doesn't necessarily scale down proportionally with database size.
  • Mitigation Challenges: While LLM-based detection is proposed, it faces limitations in operational costs and success rates, especially for RAG documents.
  • Future Work: The authors suggest further exploration into the duality of code comments being both beneficial and vulnerable, and the potential for poisoning in more complex RAG systems with reranking mechanisms. They also plan to release their benchmark suite and datasets to foster further research.

Summary updated

BibTeX
@inproceedings{ye2025directed,
  author = {Kai Ye and Liangcai Su and Chenxiong Qian},
  title = {{Directed 'Code Manual' Hijacking in Retrieval-Augmented Code Generation}},
  booktitle = {CCS},
  year = {2025},
  url = {https://openreview.net/forum?id=vzKquo2OrZ},
}
Figure