TLDR: RAG-PULL is a novel black-box attack targeting Retrieval-Augmented Generation (RAG) systems in code generation. It leverages invisible Unicode characters inserted into user queries or external code repositories to subtly manipulate the retrieval process, forcing the RAG system to select and incorporate malicious code snippets. This leads to the generation of vulnerable code, such as those with remote code execution or SQL injection flaws, by the LLM. The attack is highly effective and imperceptible to human users, posing a significant threat to the safety alignment of AI code assistants.
Large Language Models (LLMs) have become indispensable tools across various applications, from creative writing to answering complex questions. A particularly impactful area is code generation, where LLMs assist developers in writing, debugging, and testing code. However, a significant challenge with LLMs is “hallucination,” where the model generates incorrect or nonsensical information, including faulty function calls or variable names in code.
To combat this, a technique called Retrieval-Augmented Generation (RAG) was developed. RAG enhances LLM reliability by incorporating external data into the model’s context, grounding its responses in factual information and reducing hallucinations. This external data can include code repositories, documentation, and other knowledge sources, making the LLM’s output more accurate and trustworthy.
Despite RAG’s benefits, new research introduces a novel and stealthy attack called RAG-PULL. This black-box attack exploits RAG systems by inserting hidden Unicode characters into user queries or external code repositories. These imperceptible characters subtly manipulate the retrieval process, redirecting it towards malicious code snippets. Once retrieved, these snippets can introduce severe vulnerabilities into the generated code, such as remote code execution and SQL injection.
How RAG-PULL Operates
- Perturbing the Query: An attacker can inject invisible Unicode characters into the user’s query. These characters, though unseen by humans, alter the query’s embedding in the retrieval system, making it more similar to a pre-selected malicious target.
- Perturbing the Target Code: The attacker can inject invisible characters directly into a malicious code snippet within a publicly accessible code repository. This makes the malicious snippet more likely to be retrieved by relevant queries.
- Combined Perturbations: The most effective scenario involves perturbing both the user query and the target code simultaneously. By consistently polluting both with the same invisible Unicode characters, the query and target embeddings become artificially similar, almost guaranteeing the retrieval of the malicious code.
A key aspect of RAG-PULL is its “imperceptibility.” The Unicode characters used are specifically chosen because they are invisible to human users across common fonts and text editors. This makes the attack incredibly difficult to detect through manual inspection.
Impact and Success Rates
The research demonstrates that RAG-PULL can achieve remarkably high success rates. When both the query and target are perturbed, the attack can reach nearly 100% retrieval success, meaning the malicious code is almost always among the top retrieved documents. Even with only query perturbations, success rates can be significant, especially in certain programming languages like Java.
Once retrieved, these malicious snippets are often incorporated into the LLM’s final code output. The study found end-to-end attack success rates as high as 99.44%, meaning the generated code contained the intended vulnerabilities. Automated vulnerability detection tools confirmed the presence of these security flaws, which were absent in baseline (uncompromised) LLM and RAG generations.
This attack highlights a critical vulnerability in LLM safety alignment. Small, imperceptible changes in input can cause the model to prefer unsafe code over benign alternatives, leading to the generation of exploitable vulnerabilities. This opens up a new class of attacks on LLMs, particularly those used in sensitive applications like code generation.
Also Read:
- RIPRAG: A Reinforcement Learning Approach to Black-Box Poisoning of RAG Systems
- LatentBreak: A Stealthy New Method for Bypassing LLM Safety Measures
Defending Against RAG-PULL
While RAG-PULL presents a significant threat, potential defenses exist. One effective countermeasure is the careful sanitization of input at the tokenizer interface, stripping invisible Unicode characters. However, this must be done cautiously, as some Unicode characters serve legitimate purposes. Another approach involves using tokenizers that explicitly handle invisible characters, mapping them to unknown tokens, or training embedding models to differentiate texts containing these characters. For more technical details, you can refer to the full research paper here.
In conclusion, RAG-PULL exposes a subtle yet powerful attack vector against RAG-based code generation systems. Its ability to manipulate retrieval through imperceptible Unicode perturbations, leading to the generation of malicious code, underscores the urgent need for robust, Unicode-aware defenses in AI pipelines to maintain the trustworthiness of coding assistants.


