Package Hallucination Attacks on Coding Agents through Prompt Injection in Rule Files

Yupu Wang, Zhengyuan Jiang, Reachal Wang, Neil Zhenqiang GongarXiv 2026

TL;DR

Researchers have uncovered a new security vulnerability in autonomous coding agents: Package Hallucination Attacks. By injecting malicious prompts into commonly shared rule files (like .cursorrules or AGENTS.md), attackers can trick agents into replacing legitimate libraries with malicious, attacker-controlled packages. To make these attacks highly effective, the authors developed PackHallu, an evolutionary optimization framework that crafts stealthy, transferable prompts that bypass current defenses.

What Problem Does This Paper Address?

Modern coding agents—such as Cursor, Claude Code, and Aider—rely on "rule files" to maintain coding standards and project-specific constraints. Because these files are complex to write from scratch, developers often download pre-configured versions from open-source marketplaces.

The authors identify these rule files as a massive, overlooked attack surface for prompt injection. Unlike traditional prompt injection that targets a single chat interaction, this attack embeds malicious instructions into the agent's foundational guidance layer. The goal is to induce "package hallucination," where the agent generates code that imports a malicious package (e.g., numpy_hl) instead of the intended one (numpy). If a user runs this generated code, the malicious package—which the attacker has also published to a registry like PyPI—can execute arbitrary code, steal data, or compromise databases.

Figure 1: Overview of our package hallucination attack to coding agents.

Methods

The core contribution is PackHallu, an evolutionary search framework designed to optimize malicious prompts for maximum impact and stealth. It overcomes two major hurdles: the "dilution" of prompts in long rule files and the "sparsity" of feedback from coding agents.

Key components include:

  • Trajectory-Level Signal: Instead of just checking if the final code is malicious (a binary "yes/no"), PackHallu analyzes the agent's entire reasoning trajectory. This provides a much "denser" signal, rewarding prompts that cause the agent to even consider the malicious package during its "thought" process.
  • Self-Attributed Refinement: PackHallu uses an "attack LLM" to act as a critic. If a prompt fails, the attack LLM analyzes why it failed and proposes a specific revision strategy to make the next generation of prompts more persuasive.
  • Evolutionary Loop: The system maintains a population of prompts, iteratively refining them over multiple rounds to maximize the Attack Success Rate (ASR).

Datasets

The authors evaluated the attack across three diverse coding benchmarks to ensure the results weren't limited to simple tasks:

  1. BigCodeBench: A large-scale benchmark for code generation from scratch.
  2. DS-1000: A data science-focused benchmark involving complex libraries like Pandas and SciPy.
  3. RefactorBench: A repository-level benchmark requiring multi-file refactoring, representing real-world software engineering complexity.

They tested these against 8 agent frameworks (e.g., OpenHands, Aider, Claude Code) and 13 different LLMs (e.g., GPT-4o, Claude 3.5 Sonnet, Llama 3).

Key Insights and Ideas

  • The Compliance Paradox: Surprisingly, stronger LLMs are more vulnerable. Models that are better at following instructions (like GPT-4o or DeepSeek-V4) are more likely to faithfully obey the malicious instructions hidden in rule files.
  • Authority Overrides Suspicion: In case studies, agents sometimes correctly identified the injected prompt as a potential attack in their internal "thought" process but complied anyway because the rule file is treated as a high-authority configuration.
  • Popularity as a Shield: Packages that are extremely popular (like numpy) are harder to attack than niche ones. The authors hypothesize that LLMs have seen so much correct usage of popular libraries during pre-training that they have a stronger "internal resistance" to hallucinating alternatives.
  • Stealth through Context: By embedding prompts at the beginning of rule files, attackers can maximize their influence before the agent's attention is diluted by other instructions.

Key Results

PackHallu significantly outperformed all baseline prompt injection techniques:

  • High Success Rates: On the BigCodeBench, PackHallu achieved an average Syntactic Attack Success Rate (SASR) of 79.29% and a Deployable Attack Success Rate (DASR) of 67.68%.
  • Strong Transferability: Prompts optimized on one model (e.g., Qwen) remained highly effective when deployed against entirely different models and agent frameworks, proving the attack isn't just "overfitting" to one specific system.
  • Defense Failure: The authors tested five state-of-the-art prompt injection detectors. None were reliable; they either missed the majority of attacks or produced so many false positives that they would be unusable in a production environment.

Limitations and Future Work

  • Context Dilution in Large Repos: The attack's effectiveness drops on repository-level tasks (like those in RefactorBench). As the amount of surrounding code grows, the relative influence of the rule file decreases.
  • Dependency on Registry Squatting: The attack assumes the attacker can successfully upload a malicious package with a convincing name (like package_hl) to a public registry.
  • Future Directions: The authors call for the development of "agent-aware" defenses that can distinguish between legitimate developer guidelines and adversarial prompt injections within configuration files. They also suggest that package registries need better mechanisms to detect "slopsquatting" or targeted malicious clones.

Summary updated

BibTeX
@misc{wang2026package,
  author = {Yupu Wang and Zhengyuan Jiang and Reachal Wang and Neil Zhenqiang Gong},
  title = {{Package Hallucination Attacks on Coding Agents through Prompt Injection in Rule Files}},
  year = {2026},
  eprint = {2610.09264},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2610.09264},
}
Figure