arXiv:2509.07941cs.CRcs.AI2025-09中稿 · the ACM Conference…被引 6

攻击代码生成的外部文档,诱导模型引入恶意依赖。

ImportSnare: Directed "Code Manual" Hijacking in Retrieval-Augmented Code Generation

  • 设计新攻击框架ImportSnare,利用位置优化和多语言诱导实现隐蔽污染。
  • 在多种语言中成功触发超过50%的攻击率,最低0.01%污染比例即可生效。
  • 揭示LLM代码生成供应链风险,适合安全研究者与开发工具设计者参考。

代码生成已成为大语言模型(LLMs)的核心能力,显著提升程序员的开发效率。然而,数据结构与算法逻辑的复杂性常导致生成代码存在功能缺陷和安全漏洞,仅能作为需大量手动调试的原型。尽管检索增强生成(RAG)通过引用外部代码手册可提升正确性与安全性,却也引入了新的攻击面。本文首次探索检索增强代码生成(RACG)中的攻击面,聚焦恶意依赖劫持。我们证明,含隐藏恶意依赖(如matplotlib_safe)的污染文档可劫持RACG,利用双重信任链:模型依赖RAG,开发者盲信模型建议。为此,我们提出ImportSnare攻击框架,采用两种协同策略:1)位置感知束搜索优化隐藏排序序列,提升污染文档在检索结果中的排名;2)多语言归纳建议生成越狱序列,诱使模型推荐恶意依赖。在Python、Rust和JavaScript上的广泛实验表明,ImportSnare在主流库(如matplotlib、seaborn)上攻击成功率超50%,即使污染比例低至0.01%也能成功,适用于自定义及真实恶意包。研究揭示了基于LLM的开发存在严重供应链风险,凸显代码生成任务中安全对齐不足的问题。为支持未来研究,我们将发布多语言基准套件与数据集。项目主页:https://importsnare.github.io。

原文摘要 · Abstract (English)

Code generation has emerged as a pivotal capability of Large Language Models(LLMs), revolutionizing development efficiency for programmers of all skill levels. However, the complexity of data structures and algorithmic logic often results in functional deficiencies and security vulnerabilities in generated code, reducing it to a prototype requiring extensive manual debugging. While Retrieval-Augmented Generation (RAG) can enhance correctness and security by leveraging external code manuals, it simultaneously introduces new attack surfaces. In this paper, we pioneer the exploration of attack surfaces in Retrieval-Augmented Code Generation (RACG), focusing on malicious dependency hijacking. We demonstrate how poisoned documentation containing hidden malicious dependencies (e.g., matplotlib_safe) can subvert RACG, exploiting dual trust chains: LLM reliance on RAG and developers' blind trust in LLM suggestions. To construct poisoned documents, we propose ImportSnare, a novel attack framework employing two synergistic strategies: 1)Position-aware beam search optimizes hidden ranking sequences to elevate poisoned documents in retrieval results, and 2)Multilingual inductive suggestions generate jailbreaking sequences to manipulate LLMs into recommending malicious dependencies. Through extensive experiments across Python, Rust, and JavaScript, ImportSnare achieves significant attack success rates (over 50% for popular libraries such as matplotlib and seaborn) in general, and is also able to succeed even when the poisoning ratio is as low as 0.01%, targeting both custom and real-world malicious packages. Our findings reveal critical supply chain risks in LLM-powered development, highlighting inadequate security alignment for code generation tasks. To support future research, we will release the multilingual benchmark suite and datasets. The project homepage is https://importsnare.github.io.

代码生成安全攻击RAG供应链

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。