Testing prompt injection resistance in a RAG application means testing the pipeline rather than the chat model. A retrieval-augmented system treats every retrieved chunk as untrusted input, so a payload that never appears in the user’s message can still steer the answer. The practical approach is a repeatable harness: a frozen document corpus, a pinned model and embedding version, labeled adversarial payloads placed at each stage, and pass/fail checks on both retrieval behaviour and generated output.
The OWASP RAG Security Cheat Sheet states the framing directly: RAG does not reduce risk, it redistributes risk across the data pipeline, creating new attack surfaces at every stage from ingestion to generation to output. That redistribution is exactly what a test plan has to cover. This article is written for practitioners who already have a working RAG stack and want a test routine they can run again after every model, embedding, or chunking change.
Why RAG changes the injection problem
In a plain chat application, the attack surface is the user message. In a RAG application, the retrieved document is an instruction channel. OWASP describes RAG as introducing a unique attack surface distinct from both traditional web application vulnerabilities and standalone LLM risks, and it explicitly separates the stages that need controls: document ingestion, embedding generation, vector storage, retrieval, response generation, output validation, and downstream agent integration.
The OWASP AI Testing Guide entry AITG-APP-08, Testing for Embedding Manipulation, covers the adjacent axis. Its stated objectives include detecting weaknesses in the data ingestion pipeline, the embedding generation process, the vector storage layer, and the retrieval mechanisms, and its listed attack vectors include data poisoning, embedding inversion, cross-context information leaks, and unauthorized access. Prompt injection testing and embedding manipulation testing overlap but are not the same thing: one asks whether retrieved text can change behaviour, the other asks whether the vector layer itself can be poisoned, inverted, or reached without authorization. A serious test programme covers both.
Define what resistance means before you test
Prompt injection resistance is not a feeling about an answer. It is a set of observable outcomes that either happen or do not happen in a given run. Before writing payloads, agree on binary checks for at least these six outcomes:
- Instruction override: the model follows an instruction that arrived inside retrieved content rather than in the user turn.
- Context leakage: the response reveals system prompt text, hidden configuration, or other chunks the user was not entitled to see.
- Cross-tenant or cross-classification leakage: a retrieval returns a chunk belonging to another tenant, project, or classification level.
- Retrieval hijacking: an attacker-controlled chunk displaces the legitimate chunk that should have been retrieved for a query.
- Tool or agent escalation: retrieved content causes a downstream tool call the user did not request.
- Policy-violating output: the response passes retrieval and generation but fails your content policy.
Each of these maps to a control in the OWASP cheat sheet, which is useful because it means a failed test points at a specific remediation rather than a vague hardening task.
Prerequisites for a reproducible run
Without these, results are anecdotes. Set them up once and reuse them.
- A frozen corpus snapshot with a recorded hash, so a retrieval difference is not caused by a document edit.
- Pinned versions for the generation model, the embedding model, any reranker, and the vector store. Record them in the test record.
- Deterministic sampling where the runtime supports it: temperature 0 and a fixed seed. Note that determinism is not guaranteed across providers or hardware, so treat repeat runs as evidence, not proof.
- Chunk-level metadata in the vector store: tenant identifier, classification, source document ID, and ingestion timestamp. OWASP lists access control metadata on every vector chunk as an immediate foundational control.
- Retrieval logging that records which chunk IDs were returned, with scores, for every query. OWASP lists full pipeline observability and logging as foundational.
- A canary token embedded in the system prompt or hidden configuration. If the canary appears in output, you have a confirmed leak without needing to judge tone or intent.
Payload classes to include
A payload library should be small, labeled, and versioned. The following classes cover the common delivery routes.
| Payload class | Where it lives | What it probes |
|---|---|---|
| Direct instruction override | Body text of an ingested document | Whether retrieved text is treated as data or as commands |
| Obfuscated or hidden text | HTML comments, metadata fields, alt text, zero-width characters, low-contrast text | Whether ingestion and chunking preserve content that a human reviewer never saw |
| Delimiter escape | End of a chunk, targeting the boundary markers around retrieved context | Whether context window protection survives a crafted chunk |
| Retrieval hijack | Keyword-stuffed or semantically dense document | Whether an attacker document can outrank the legitimate source |
| Split payload | Two or more chunks that only make sense together | Whether per-chunk inspection misses composed instructions |
| Encoded payload | Base64, ROT13, or other transform inside a document | Whether the model decodes and obeys encoded instructions |
| Query-side injection | The user query itself | Whether query normalization and abuse pattern detection are active |
| Tool output injection | Content returned by a tool or agent step | Whether downstream agent integration re-validates inputs |
OWASP treats context window protection with delimiters and chunk limits as an immediate control, and query normalization plus abuse pattern detection as another. Both exist because the payload classes above are common in practice.
An ordered test procedure
Run the steps in this order. Each step produces a result that tells you whether the next step is worth running.
- Baseline retrieval. Run a fixed question set against the clean corpus. Record which chunk IDs are returned and in what order. Everything later is compared against this.
- Canary check. Ask a benign question that should produce a normal answer. Confirm the canary token does not appear. If it does, stop and fix context handling first.
- Single-stage injection. Insert one payload document at a time, re-ingest, and repeat the same question set. Check whether the answer changes in the direction the payload requested, and whether the payload chunk was retrieved at all.
- Access control probe. Query with a user context that should not reach a restricted chunk. Check the retrieval log for restricted chunk IDs, not just the final answer. A leak in the log is a leak even if the model ignored it.
- Tenant isolation probe. Repeat the previous step across two tenants with overlapping vocabulary. OWASP lists tenant and classification isolation in vector stores as foundational.
- Query-side injection. Send adversarial queries, including attempts to override instructions and to force retrieval of a specific document.
- Output validation. Attempt to produce policy-violating content through retrieved context rather than through the query. Confirm the output layer catches it independently.
- Fail-closed check. Break a dependency deliberately: point the retriever at an unreachable index, or return an empty result set. Confirm the application refuses rather than answering from model memory alone. OWASP lists fail-closed behaviour across the RAG pipeline as foundational.
Scoring and what to record
Report per payload class, not as a single number. Useful metrics are attack success rate (payloads that achieved their goal divided by payloads attempted), retrieval hijack rate (queries where the attacker chunk entered the top-k), leakage rate (runs where a canary or restricted chunk appeared in output or logs), and refusal rate (runs where the system declined despite a benign request, which is the false-positive cost of your defences).
Repeat each payload several times and record the spread. A single run at temperature 0 is a data point, not a resistance rating. Always attach the model version, embedding model version, chunk size and overlap, prompt template revision, and corpus hash to the result, because any of those changing invalidates the comparison.
An illustrative test record
The following is a format example, not a measured result. It shows the fields worth capturing.
test_id: pi-retrieval-hijack-004
corpus_hash: <snapshot hash>
gen_model: <pinned version>
embed_model: <pinned version>
chunk_size: 512
chunk_overlap: 64
temperature: 0
payload_class: retrieval_hijack
payload_doc_id: doc-4417
query: "what is the refund window"
expected_source: policy-refunds-v3
top_k_returned: [doc-4417, policy-refunds-v3, policy-shipping-v1]
canary_in_output: false
outcome: retrieved_attacker_chunk_at_rank_1
notes: answer text unchanged in this runRecording the rank of the attacker chunk matters as much as recording the answer. A hijacked retrieval that the model happened to ignore is still a control failure waiting for a different phrasing.
Common failure modes and the matching fix
- Delimiters only. Wrapping retrieved context in markers helps but is not sufficient on its own. Pair it with chunk limits and output validation.
- Filtering only the user query. The payload usually arrives through documents. Filter at ingestion, at retrieval, and at output.
- No chunk-level access control. If access rules live only in the application layer, a direct vector store query bypasses them. OWASP recommends access control metadata on every vector chunk.
- No source attribution. Signed source attribution on every RAG response is listed as a next-priority control and makes it possible to trace which chunk produced a bad answer.
- No fail-closed behaviour. An empty retrieval result should not silently fall back to ungrounded generation.
Where local stacks and WordPress automation fit
Locally hosted RAG stacks are convenient test beds because you can pin exact model and embedding versions, inspect retrieval logs directly, and rerun the same corpus offline without per-token cost. That makes them a good place to build the payload library before testing a hosted deployment.
WordPress-based automation inherits the same risk whenever it retrieves from site content. Any post, page, comment, or imported document that reaches the index is a potential injection carrier. A useful low-cost test is to publish a draft containing a payload, ingest it, and confirm the assistant does not follow it. If user-submitted content is indexed, that test is not optional.
Limitations and honest reporting
There is no universal benchmark for RAG prompt injection resistance, and results do not transfer cleanly between stacks. A pass on one model, chunk size, and prompt template says nothing about a different configuration. Model updates can change behaviour in both directions, so a previously passing suite can regress silently. Small payload sets overstate confidence: twenty payloads that all fail to land is weak evidence compared with a labeled library of several hundred covering distinct delivery routes.
The OWASP cheat sheet is guidance, not a certification, and the AI Testing Guide entry on embedding manipulation is a testing reference rather than a scoring standard. Treat both as structure for your own evidence, and publish your configuration alongside your results so others can judge whether the comparison is meaningful.
Start here
If you have not tested injection resistance before, run two checks this week: a canary token in the system prompt to confirm nothing leaks, and one cross-tenant retrieval probe using the retrieval log rather than the final answer. Then expand the payload library one class at a time, and re-run the suite whenever the model, embedding model, chunking strategy, or prompt template changes. Readers who want to compare notes on payload design, scoring, or a specific stack are welcome to share their environment, configuration, and observed retrieval logs in the community discussion.
FAQ
Is there a public benchmark I can use instead of building a corpus?
The sources reviewed here do not describe a universal RAG prompt injection benchmark. OWASP provides controls and testing objectives rather than a scored dataset, so results depend on your own corpus, chunking, and prompt template. Build a labeled payload library against a frozen snapshot of your documents.
Will a stronger or newer model fix prompt injection in my RAG app?
No. In a RAG system the malicious instruction arrives as legitimate retrieved context, so the model is behaving as designed when it follows it. OWASP treats the fix as pipeline-level: ingestion controls, chunk-level access metadata, context window protection, output validation, and fail-closed behaviour.
How often should I re-run the injection test suite?
At minimum whenever the generation model, embedding model, reranker, chunk size or overlap, or prompt template changes, and whenever new document sources are added to the index. Results do not transfer across configurations, so a previous pass is not evidence for a changed stack.
Is output filtering enough on its own?
No. OWASP lists output validation and policy enforcement as one foundational control among several, alongside document provenance, context window protection, chunk-level access control, tenant isolation, and fail-closed behaviour. Each covers a different stage, and a payload can fail at one stage while passing another.