Manuscript Offprint

arXiv:2610.09264

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

Yupu Wang, Zhengyuan Jiang, Reachal Wang, Neil Zhenqiang Gong

Abstract

Modern agentic coding frameworks increasingly rely on community-shared rule files (e.g., AGENTS.md or .cursorrules) to guide autonomous code generation, yet the security risks of this pipeline remain underexplored. To bridge this gap, we introduce the package hallucination attack, where an attacker injects malicious prompts into benign rule files to induce coding agents to replace legitimate dependencies with attacker-controlled packages. To obtain effective malicious prompts injected into rule files, we propose PackHallu, an evolutionary optimization framework that iteratively rewrites these injected prompts using trajectory-level feedback and LLM-guided mutations. Evaluations across multiple benchmarks, LLMs, and agent frameworks, show that PackHallu achieves high attack success rates and strong transferability across diverse models and agent combinations. Our findings demonstrate that coding agents are vulnerable to package hallucination attacks, highlighting the urgent need for stronger security safeguards in autonomous coding systems.

1 Introduction

Modern agentic coding frameworks, such as Claude Code [1], Cursor [2], and Codex [3], have transformed software development by providing autonomous capabilities like file system access, web browsing, and iterative code production. To steer the backbone LLM effectively and ensure the generation of well-structured, high-quality code, these frameworks require coding rule files [4] (such as ‘AGENTS.md’, ‘CLAUDE.md’, or ‘.cursorrules’). These files serve as the foundational guidance layer for the agent, containing specific constraints, policies, and guidelines that dictate how the model interacts with tools and handles code production. As a result, these rule files have become an indispensable component of the modern coding agent’s workflow. Unlike traditional, concise system prompts, coding rule files are significantly more complex and comprise numerous detailed textual segments [5]. Because customizing these intricate instructions for every unique project or programming environment requires substantial effort, it is common for users to download and share pre-configured rule files across open-source platforms [6, 7] and online rule marketplaces [8]. However, the security implications of this collaborative ecosystem have been largely ignored.

In this work, we introduce the package hallucination attack against the rule-file ecosystem, as illustrated in Fig. 1, by exploiting prompt injection in rule files. Specifically, an attacker injects malicious prompts into a rule file and publishes the contaminated file on open-source platforms or marketplaces. When a user inadvertently downloads the malicious rule file and uses it to guide a coding agent on a programming task, the malicious prompt induces the agent to replace a legitimate dependency with an attacker-chosen package, which we refer to as the malicious package, in the generated code. The attacker also uploads the malicious package to a package repository, such as the Python Package Index (PyPI). As a result, when the generated code is executed, the malicious package is installed and run, potentially leading to a wide range of security and privacy risks, including data exfiltration and unauthorized database modifications. Unlike prior work on package hallucination [9, 10], where coding agents import random non-existent packages in generated code, a package hallucination attack causes the agent to import a specific package chosen and controlled by the attacker.

∗^{*}Co-first authors with equal contributions.

Figure

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

Existing prompt injection attacks can be broadly divided into heuristic-based and optimization-based approaches. Heuristic-based attacks [11] use manually designed malicious prompts, while optimization-based attacks [12, 13, 14] iteratively refine prompts based on feedback from the victim agent to induce a desired behavior (e.g., generating code that imports a malicious package instead of the intended victim package). Our experiments show that both classes of attacks are considerably less effective in our setting. The key challenge is that the malicious prompt must be embedded within a coding rule file, which fundamentally changes the attack surface. First, coding rule files are typically long, causing the malicious prompt to be diluted by surrounding benign content and thereby reducing the effectiveness of heuristic-based attacks. Second, optimization-based attacks rely on informative feedback to guide prompt refinement, yet the feedback available from the agent's generated code is sparse. As a result, the optimization process becomes significantly more difficult, limiting the effectiveness of existing optimization-based attack methods.

To address these challenges, we propose PackHallu, an evolutionary search framework that optimizes malicious prompts embedded within coding rule files to induce package hallucination attacks. PackHallu does not require access to the victim agent. Instead, it leverages a locally deployed surrogate agent to iteratively optimize malicious prompts by incorporating feedback from the surrogate agent on candidate prompts. Specifically, PackHallu addresses the two challenges through two key components. First, to mitigate the contextual dilution caused by lengthy coding rule files, PackHallu introduces a Self-Attributed Refinement mechanism. This component employs an LLM to analyze why candidate prompts fail in previous iterations and uses these insights to guide prompt refinement in subsequent iterations. Second, to overcome the sparsity of feedback obtained from the surrogate agent's final generated code, PackHallu introduces a Trajectory-Level Signal. Rather than relying solely on the agent's terminal output, this component leverages the agent's complete reasoning trajectory to evaluate candidate prompts, providing a substantially denser and more informative optimization signal. Together, these two components enable PackHallu to overcome the limitations of existing prompt injection techniques, resulting in an effective attack framework for inducing package hallucination.

We evaluate PackHallu on 3 coding benchmarks spanning file-level and repository-level coding tasks, covering 10 widely used Python packages. Our evaluation encompasses 8 agent frameworks and 13 backbone LLMs. The results demonstrate that PackHallu consistently achieves high attack success rates and substantially outperforms existing heuristic-based and optimization-based prompt injection attacks. Moreover, PackHallu exhibits strong transferability: attacks optimized using a surrogate agent remain effective even when the victim agent employs a different agent framework and backbone LLM. Finally, we assess the effectiveness of state-of-the-art prompt injection detectors against malicious rule files generated by PackHallu. We find that existing detectors either miss most attacks or suffer from high false positive rates, underscoring the inadequacy of current defenses.

To summarize, our key contributions are as follows:

• We introduce and formally define the package hallucination attack against coding agents, a previously unexplored threat in which an attacker induces a coding agent to adopt an attacker-designated malicious package through prompt injection to rule files.

• We propose PackHallu, an evolutionary search framework for optimizing malicious prompts embedded within coding rule files to induce package hallucination attacks.

• We conduct a comprehensive evaluation of PackHallu across 3 coding benchmarks, 8 agentic coding frameworks, and 13 backbone LLMs. Our results demonstrate that PackHallu achieves high attack success rates and substantially outperforms existing heuristic-based and optimization-based prompt injection attacks.

• We further evaluate state-of-the-art prompt injection detectors and show that they are ineffective at reliably identifying malicious rule files generated by PackHallu, revealing a significant security weakness in the current coding rule-file ecosystem.

References (26)