Indirect Prompt Injection in AI Agents: Download-Directory Instruction Files and Layered Defenses
This article examines an indirect prompt injection scenario in which a browser download writes an instruction file into a user's download directory, and an agent later reads that file while performing a task. It explains why plain-text files bypass executable-file controls, how common user habits make the attack path viable, and why agents need clear trust boundaries between instructions and data. It also presents layered defenses, a Python scanner for suspicious instruction files, and a developer checklist.
1. Starting Point: Two Kinds of Statements in the Same Directory
AI coding assistants commonly read instruction files in a project to learn coding standards, test commands, and prohibited actions. These files are plain text: models can read them, and developers can edit them manually. The problem is that a model may encounter a statement such as “do not perform this action” alongside a configuration file that says “you are welcome to perform this action.” The two may conflict, and the model may not be able to determine which statement comes from a trusted source.
This article discusses a scenario in which a web page uses browser download behavior to write an instruction file into the user’s download directory. The user then starts an agent in that directory, and the agent reads the file and adopts its contents. This is a form of indirect prompt injection. The malicious content does not come from user input; it comes from external data that the agent reads while executing a task.
2. Step One: Why the File Write Is Not Blocked
A web page can usually write a file to the download directory without user clicks or confirmation. Browser and operating-system controls for downloaded files focus mainly on executable files: files carry a source marker, a prompt appears before execution, and some file types are blocked outright. These mechanisms address the risk of “a downloaded program running automatically.”
Plain-text instruction files do not trigger these rules. The system does not execute them, so the security model usually treats them as ordinary data. For an agent, however, text itself is control input. A passage written to look like a specification can be enough to change the agent’s behavior.
Protection rules are divided by file type, but risk depends on who treats the content as instructions. If an agent reads a certain class of file as instructions, that class of file should be subject to equally strong review.
3. Step Two: Habits Weaken the Defense
Telemetry data disclosed in a security research report shows that about half of users have run coding agents in their download directories, and about one quarter of agent sessions launch a real browser. These two figures show that the prerequisites for the attack path are not demanding. The attack relies on common operating habits, not rare configuration errors.
When users start an agent, they usually focus on “help me process this file” and rarely check “what instruction files exist in this directory.” The download directory is also often a gathering place for temporary files from various sources. Running an agent there is equivalent to letting it work in an entrance that no one reviews.
The ability to launch a browser amplifies the risk. Once an agent has both read contaminated instructions and can control a browser, it may access sensitive pages, submit forms, or perform actions without confirmation.
4. Root Cause: No Clear Boundary Between Instructions and Data
The core problem exposed by this attack chain is that agents lack trust grading for input. System prompts, explicit user requests, configuration in the project repository, and text read from web pages or download directories may all appear to the model as context. When they conflict, the model may choose the most compliant one.
A more robust design should clearly distinguish three classes of content:
- Trusted instructions: explicit directions from the runtime environment and the user.
- Controlled configuration: files from a reviewed project repository and managed under version control.
- Untrusted data: external content from web pages, downloaded files, email, third-party documents, and similar sources. It should be treated only as material to analyze, not as instructions.
Treating untrusted data as data is the foundation of defense. One practical approach is to state in the system prompt that any instructions appearing in external files must not be executed and should only be reported to the user. However, prompt-level constraints alone are not reliable, because the prompt itself is also text and can be overridden. Prompt-layer constraints need to be combined with tool-layer restrictions.
5. Layered Defenses
5.1 Isolate the Working Directory
Do not start agents in the download directory, on the desktop, or in temporary directories. Use a separate working directory for each project. External files should first be copied to an isolated area, inspected, and only then passed to the agent if appropriate.
5.2 Validate Instruction Sources
Agents should load instruction files only from trusted locations. For files that are newly appeared, not under version control, or whose modification time is inconsistent with project activity, the agent should first ask the user to confirm.
5.3 Require Confirmation for High-Risk Operations
Operations such as file deletion, network requests, form submission, and browser automation should require user confirmation by default. The confirmation interface should show the specific target, such as the target path, domain, or form field, rather than a vague “allow?” prompt.
5.4 Sandbox Browser Capabilities
Browser automation should run in a separate profile or container and should not share the user’s login state or cookies. In this way, even if instructions are contaminated, the possible damage has a clear upper bound.
5.5 Follow the Principle of Least Privilege
Grant only the file paths and tool permissions needed for the current task. Read permissions and write permissions should be configured separately, and the write scope should be limited to the project directory.
6. Example: Scanning for Suspicious Instruction Files
The following script traverses a specified directory, lists text files modified within the last seven days, and checks whether they contain common expressions that encourage privilege escalation or inducement. It does not replace a full security audit, but it can serve as a quick check before starting an agent.
import reimport sysimport timefrom pathlib import PathSUSPICIOUS = re.compile(r"(无需确认|不需要确认|自动执行|欢迎执行|忽略.{0,4}(之前|以上)|"r"ignore (all |previous )?instructions|without asking)",re.IGNORECASE,)TEXT_SUFFIXES = {".md", ".txt", ".json", ".yaml", ".yml", ".toml"}RECENT_SECONDS = 7 * 24 * 3600def scan(root: Path) -> int:now = time.time()hits = 0for path in root.rglob("*"):if not path.is_file() or path.suffix.lower() not in TEXT_SUFFIXES:continueif now - path.stat().st_mtime > RECENT_SECONDS:continuetext = path.read_text(encoding="utf-8", errors="replace")for m in SUSPICIOUS.finditer(text):hits += 1print(f"[Review needed] {path}: matched '{m.group(0)}'")return hitsif __name__ == "__main__":target = Path(sys.argv[1] if len(sys.argv) > 1 else ".").expanduser()count = scan(target)print(f"Found {count} suspicious expressions.")
The script only reads files and does not modify or delete anything. Keyword matching will produce false positives, and the results require human judgment. The path comes from a command-line argument, and the script does not execute any content in the files. When run in a large location such as a download directory, traversal may be slow; you can specify a smaller subdirectory.
7. Developer Checklist
- Is the agent started in the download directory, on the desktop, or in a temporary directory?
- Are there instruction files in the current working directory that are not under version control?
- Is the modification time of instruction files inconsistent with project activity?
- Does browser automation use a separate session and profile?
- Do file deletion, outbound network requests, and form submission require confirmation each time?
- Are the agent’s read and write permissions limited to the project directory?
