Can a Retrieved Document Hijack Your RAG Agent?

A retrieved document can be relevant and still contain instructions that a reader did not authorize. In retrieval-augmented generation, that distinction matters: the model is supposed to use source content as evidence, not grant the source authority over tools or secrets. A citation shows where text came from; it does not establish that every instruction in the source is trustworthy.
A real RAG demo that changed persona
Developer Simon Willison documented an accidental case in June 2024. A developer identified as @deepfates used documentation for Willison’s LLM project in a RAG demonstration. The resulting chatbot answered as a gerbil. Willison traced that behavior to an example instruction in the project’s release notes, which had been retrieved as source material. His original account includes the developer’s screenshot and links to the release notes.
The source instruction was part of documentation illustrating a command, not a malicious campaign. Willison describes it as accidental indirect prompt injection. This case demonstrates an observed answer change in one setup; it does not establish a failure rate or prove that a tool was executed. Read the original screenshot and surrounding explanation for the full context.
Why the source was retrieved
Willison explains that the developer’s question was not a question about his documentation. The documentation contained example prompts, and the retrieval system nevertheless selected a fragment that included a role-play instruction. His explanation points to a retrieval design issue: similarity search can return a best match even when none of the available documents answers the question. The exact retrieval configuration and source corpus matter when interpreting the example; his post is not a controlled comparison of retrieval algorithms.
For a team building an assistant, inspect both the query and the actual retrieved passages before changing the model. A list of document titles is not enough to show which excerpt entered the prompt. Keep an appropriately sanitized trace of the excerpts, their origin and the retrieval scores if the system produces them. Then compare the answer with the evidence actually provided. Do not copy private customer documents into public bug reports.
A retrieval threshold can be useful when calibrated against your own data, but it is not a universal safety boundary. An instruction can be highly relevant to the search terms and still be inappropriate to execute. Conversely, a legitimate answer might have a weak score. Treat retrieval selection, answer support and action authorization as separate questions.
A documented example
Microsoft’s account of indirect prompt injection describes a historical webpage attack against a browsing assistant. Instructions on a page caused the assistant to construct a request that transmitted conversation data to an attacker-controlled server. Microsoft says the specific issue was fixed. This is evidence of a particular historical attack, not a claim that the same exploit works on current products.
The important boundary is the one crossed: material retrieved to answer a question influenced an external action. A RAG application may retrieve internal documents, public pages or uploaded files. Their authors and access rights can differ from the person who configured the assistant. The more capabilities the assistant has, the more important it is to verify the proposed action outside the model’s generated text.
Separate an answer from an action
Start by identifying which content the system can retrieve and who can modify it. For each tool, document what it can read, write or send. Use application permissions to constrain access to the current user’s authorized resources. If an assistant proposes a write or an external request, validate the destination and arguments in code; do not rely on a sentence inside a prompt as the only gate.
Require an appropriate approval for consequential actions, and keep that approval in trusted application state rather than in a retrieved document. Approval helps only if the user can see what will happen and the application checks the approved action before execution. A document that says “approved” is not itself proof of authorization.
Test the boundary in a safe environment
Use a synthetic document and dummy data. Put an ordinary fact in the document alongside a benign instruction asking the assistant to change its answer or invoke a mock tool. Run the same question with and without that instruction. Record the retrieved text, model and version, tool calls, permissions, and actual result. Keep the mock tool isolated from real accounts and network destinations.
A useful result distinguishes several outcomes: the instruction was never retrieved; it was retrieved but ignored; it changed the answer; or it caused a proposed tool action. A single passing case does not prove that all injections will fail. Test variants and monitor the actual enforcement layer, including malformed tool arguments and attempts to access resources the current user cannot use.
What the sources support
OWASP’s prompt-injection guidance describes indirect attacks and notes that prompt hardening does not make them impossible. A source label, delimiter or instruction to ignore malicious text can help organize the task, but it is not a substitute for access controls. Likewise, retrieval quality and citation checks address whether evidence is relevant and supported; they do not independently authorize an action.
If the problem is an answer that invents facts rather than an unsafe tool action, use the companion RAG hallucination guide to check retrieval, source coverage and answer support. If a tool action is involved, use the authorization guide to inspect the application boundary as well.
Walk through one request
Suppose a support assistant must answer a question about a subscription. The application retrieves three policy pages and gives their text to a model. One page says that refunds must be requested within a specified period. Another contains a hidden-looking paragraph telling the assistant to send the full conversation to an external address. The first passage is potential evidence for the answer. The second is an instruction from whoever could edit the page, not a request from the user.
A safe application does not need to guess the page author’s intentions before deciding who can authorize a tool. It can let the model summarize the policy while a separate permission layer checks any proposed action. If the model returns a request to send data outside the service, the application should validate the destination, the user’s permission and the amount of data to be sent. This scenario illustrates a design boundary; it is not a reported experiment or a measurement of a particular product.
For a read-only assistant, the consequence may be an incorrect or misleading answer rather than an outbound request. Ask the model for a citation to the passage it used, then check whether that passage actually supports the statement. A real citation can point to a forged document or a passage that says the opposite. Citation checking and action authorization address different problems, so a workflow that needs both should perform both checks.
Map the people who can change the evidence
A system diagram is more useful when it identifies the people and services behind each box. List the repositories, shared drives, websites and upload areas that can feed retrieval. Note which groups can add or edit each source, how quickly edits enter the index, and whether deleted documents remain searchable. An internal document is not automatically trustworthy: a large organization can have many document owners and different permission levels.
Then trace a request from the user to the application, the retriever, the model and any connected tools. At each step, write down which component can enforce a rule. The retriever can restrict which documents are returned. The application can pass only the current user’s permitted documents. A tool gateway can validate arguments and credentials. A confirmation screen can ask the person to approve a specific action. Each control has a different job; none needs to be described as a complete prompt-injection cure.
Keep permissions consistent when content is indexed. For example, if an employee loses access to a private document, the retrieval system must not continue serving an older indexed copy to that person. Check how access-control changes reach the index and what happens during any delay. If you cannot verify that behavior, test it with a disposable account and a harmless document before exposing the feature to sensitive data.
Inspect a proposed tool call
A model response that looks like a command is still a proposal. Before execution, validate the tool name, the caller’s identity, the destination and the arguments against the application’s rules. Use a small allowlist of operations and destinations where the workflow permits one. Parse structured arguments with a schema and reject missing or unexpected fields. Record the decision and its reason without placing secrets or private document contents in application logs.
For example, a proposed email action may include a recipient, subject and message body. The user may have permission to send a summary to a colleague but not to send the entire conversation to an arbitrary domain. The application can display the actual recipient and message, require the necessary approval, and then verify that the action being executed matches what the person approved. If a field changes after approval, require a new decision. The exact policy depends on the product and its users.
Run these checks even when the retrieved page claims that the action is urgent or already approved. Text supplied by a source cannot update the application’s trusted approval state. The same principle applies when the instruction is embedded in a PDF, a repository comment or a search result. Which defense is appropriate depends on the available tools and permissions, so document those choices instead of promising that a single prompt can cover every case.
Build a small regression set
A useful evaluation starts with a few cases that represent real workflows. Create a normal policy document, a document containing an instruction unrelated to the question, and a document that asks for a prohibited mock action. Keep the content synthetic and give the assistant a test account with no production credentials. Run the same user question against each case and capture the retrieved passages, proposed tool call and application decision.
The result should make it easy to see where the boundary held or failed. If a passage was not retrieved, the test says little about how the application handles that passage after retrieval. If it was retrieved and the model proposed an action, inspect whether the permission layer rejected it. If the model answered incorrectly but made no tool call, record that as an answer-quality issue. Do not combine these distinct outcomes into a single safety score unless the scoring method is defined.
After a change, rerun the same cases and compare the records. Add variants that use a different document format, a modified destination or a relevant passage that also contains an unwanted instruction. Keep the original examples so that new controls do not silently break a legitimate answer. A regression suite provides evidence about the cases it runs; it cannot cover every document or every future model behavior.
Decide what to record and share
A useful incident note includes the user request, a sanitized version of the retrieved passage, the expected behavior and the observed behavior. Record the model and application versions, retrieval settings where available, the requested tool action and the final authorization decision. Separate facts seen in logs from a hypothesis about why the model behaved that way. A screenshot can help a reader follow the sequence, but it should be accompanied by enough context to understand what it shows.
Remove credentials, personal details and proprietary source material before sharing a trace. Replace external addresses with a clearly synthetic destination and use dummy account identifiers. If the original case involved a product or service, check the maintainer’s reporting policy before publishing sensitive reproduction details. When a source says a flaw was fixed, include that context and the date of the account so a reader can distinguish a historical example from a current claim.
These notes also make an internal fix easier to review. A reviewer can compare the original case with a test after the change, inspect the code that enforces permission and decide whether the test covers the actual failure. If the example has not been run, label it as a design scenario. That small distinction allows a reader to reuse the idea without mistaking it for a benchmark.
A local test you can run
I ran three offline cases with a small Node.js mock: an answer with no action, an attempted external send, and an allowed local read. The screenshots show the actual command output. This checks the mock policy; it does not test a model.
Extra check: I tried a destination that starts with the permitted local address but adds an external target. The mock rejected it because the whole address did not match. This is one exact-match rule, not a general prompt-injection defense.
node demo-v2.mjs lookalike


Conceptual example: a proposed action is checked outside the model.
proposed_action → permission_check → approved_action