Treat every document your RAG pipeline reads as untrusted input, then test it with poisoned documents
The short answer: a document your RAG pipeline or agent retrieves is input from someone you may not know. Treat it like a form field on a public website. Then prove your defences work by feeding the pipeline poisoned documents before an attacker does.
A thread on r/LLMDevs asks the question this article builds on: IBM trained small models to ignore instructions hidden in documents. Would you trust that in a RAG pipeline? I take only its title from the thread. The rest is a method you can run this week.
Why documents are the attack surface
In a RAG pipeline, the model reads retrieved text and your instructions in the same context window. Anyone who can get text into your corpus can try to put instructions in it. That is indirect prompt injection.
I wrote about where this bites in AI attackers go after Integrity first. Your control roadmap probably doesn’t.. My line there: "For most enterprise deployments, indirect prompt injection at the tool layer is where AI security stops being theoretical."
The scale needed is small: the same article cites PoisonedRAG (Zou et al., arXiv 2402.07867), which "reached 90 percent attack success against RAG corpora of a million documents with just five poisoned documents."
One caveat from that article, which applies to my own framing too: its heatmap cell values "are editorial judgments, not measurements." The PoisonedRAG result is a published finding. My ranking of risks is a judgement.
Step 1: List every way text reaches the model
Before you test, map the inputs. For each one, write down who can change it.
- Uploaded files and shared drives
- Web pages your retriever or agent fetches
- Emails, tickets and chat messages
- Tool and API responses
- Wiki pages and knowledge-base entries
If the answer is "anyone with an account" or "anyone on the internet", it is untrusted.
Step 2: Build a small set of poisoned documents
Make a test folder of documents that look normal but carry a hidden instruction. Vary the hiding place so the test covers the whole pipeline:
- Plain instruction in the body. A paragraph that says to ignore previous instructions and do something else.
- Instruction in metadata. Title, author field, alt text, filename.
- Instruction in a table cell or footnote. Places a parser reads but a human skips.
- Instruction that asks for a tool call. For example, asking the agent to send a retrieved passage to an address you control (use a test address).
- Instruction that asks for disclosure. For example, asking the model to repeat its system prompt.
Plant them in a copy of your corpus, not production. Give each a unique marker string so you can search logs for it.
Step 3: Ask questions that retrieve them
A poisoned document does nothing if it is never retrieved. Write queries that should pull each one into context. Run each query many times, because model output varies. Record three things per run:
- Was the poisoned chunk retrieved?
- Did the output follow the injected instruction?
- Did any tool call fire that the user never asked for?
The third is the one that matters most. A wrong sentence is a quality bug. An unrequested tool call is an incident.
Step 4: Move the rules out of the model
In my article I argued that tool allowlisting, agent identity and action authorisation become core AI security controls. For a RAG pipeline, that means the rules sit in your code, where the model cannot change them.
So decide what must never happen, and enforce it in code:
- Allow-list tools per task. A summarising agent does not need a send-email tool.
- Validate tool arguments outside the model. Check recipients, URLs and file paths against rules the model cannot edit.
- Require human approval for irreversible actions. Payments, deletions, external sends.
- Keep retrieved text out of the instruction channel. Label it as data in the prompt, and do not let it change which tools are available.
- Log the retrieval and the action together. You want to answer "which document caused this call?" later.
That last point ties to integrity. In the same article I wrote: "If your AI governance program can’t produce tamper-evident evidence of a decision three years after it was made, it won’t survive contact with a regulator or a litigant." Logs that link a document to an action are the start of that evidence.
Step 5: Turn the test into a regression suite
One run proves little. Put the poisoned documents in CI.
- Each document has an expected safe outcome: the agent makes no tool call, discloses nothing and gives the same answer as it would without the poisoned text.
- A failure blocks the release.
- Every time someone finds a new injection that works, add it to the folder.
- Re-run after any change to the model, the prompt, the retriever, the chunking or the tool list.
Step 6: Report what you find
Write down three numbers per release: how many poisoned documents you tested, how many were retrieved, and how many produced a bad action. Do not report an "injection resistance score" unless you can explain how it was measured. If a leader asks whether the pipeline is safe, the honest answer is "these cases pass, these controls hold, and here is what we have not tested."
A short checklist
- [ ] Every input source listed, with who can write to it
- [ ] Poisoned documents covering body, metadata, tables, tool requests and disclosure
- [ ] Queries that retrieve each one
- [ ] Tool allow-lists and argument validation in code
- [ ] Human approval on irreversible actions
- [ ] Retrieval and action logged together
- [ ] Suite running in CI and growing
Where this leaves you
Ship RAG with the safety rules in the pipeline you control, because you cannot count on the model to ignore what it reads.
Which of your input sources can someone outside your organisation write to today, and have you ever tested it with a poisoned document?


