This article explains how retrieval-augmented generation (RAG) systems work, the security vulnerabilities they introduce, and the controls needed to defend them. RAG augments AI responses by retrieving relevant passages from a knowledge base, but this creates a new attack surface where poisoned or private documents can compromise answers. The guide covers attack vectors from ingestion through generation, defenses grounded in published research, practical testing methods, and clarifies common misconceptions about RAG security.
- RAG creates a new attack surface through the knowledge baseUnlike standard language models, RAG systems trust whatever the retrieval stage returns and incorporate it directly into the prompt given to the model. This means the knowledge base itself becomes an attack surface; if an attacker can influence what documents are stored or retrieved, they can shape the model's answers without needing to compromise the model itself.
- Small poisoning attacks can be highly effective at scaleThe 2024 PoisonedRAG research demonstrated that adding only a handful of crafted passages targeting a specific question was sufficient to make a RAG system give the attacker's answer most of the time, even in knowledge bases with millions of documents. This works because retrieval systems are designed to find the closest semantic match, so adversarial passages written specifically to match a question will rank highly and win the retrieval competition.
- Access control is the most common real failure pointMany RAG deployments begin by indexing all company documents, then add a chatbot interface without implementing permission checks in the vector database itself. Because documents had permissions in their original system but the vector database often does not enforce them, the assistant can retrieve and summarize sensitive content like salary files or board papers for any user who asks the right question. The fix is to carry document permissions into the index as metadata and filter results against the current user's identity before the model ever sees them.
- Retrieved content can inject instructions into the modelBeyond poisoning (changing what the assistant says), injection attacks change what it does. If a retrieved document contains text resembling instructions, the model may execute those instructions because it cannot reliably distinguish information from directives. In RAG systems with tools, this means a planted document in the knowledge base can become a data exfiltration or unauthorized action vulnerability.
- Embeddings are not anonymous and can be reversedResearch published in 2023 titled 'Text Embeddings Reveal (Almost) As Much As Text' demonstrated that short passages can be reconstructed from their embeddings with high accuracy. Teams sometimes treat vector databases as less sensitive than the source documents, but a stolen vector database should be treated as equivalent to stolen copies of the original text, requiring the same protective measures.
- Defenses require controls at every RAG stageNo single control secures a RAG system. Effective defense requires permission filtering at retrieval time, controlled ingestion with versioning and review, source labeling to show which documents contributed to answers, separation of trusted and untrusted sources, limiting what tools the assistant can invoke after reading untrusted content, protecting the vector database with encryption and access control, and maintaining retrieval logging for incident investigation.
- Testing must verify access control and tenant isolationThe article prescribes testing access control first by attempting to retrieve high-privilege content as a low-privilege user, testing tenant separation by confirming one tenant's queries never return another's chunks, and testing writable sources by adding marked test documents to verify whether they influence answers or behavior. Testing also requires checking retrieval logs and retesting after any changes to sources, chunking, or the model.
- RAG security responsibility lies with the application builderThe model provider cannot see the knowledge base, permissions, or users, so the team building the RAG application bears responsibility for security. This clarifies that outsourcing the model does not transfer responsibility for the access control, ingestion governance, or output validation required to secure RAG.
Bottom line: RAG security depends on treating the knowledge base as a sensitive asset with the same access controls as source documents, assuming the model will trust whatever it retrieves, and implementing controls not inside the model but in what can be ingested, who can retrieve what, and what the assistant can do after reading untrusted content. Access control at retrieval time and content poisoning prevention are the most critical and most commonly overlooked defenses.
Retrieval-augmented generation, or RAG, lets an AI assistant answer from your own documents: it searches a knowledge base, pulls back the most relevant passages and hands them to the model with the question. That makes answers more current and specific. It also means the model now trusts whatever the search returns, so the knowledge base becomes part of the attack surface.
This guide explains how RAG works, where it fails, what published research has shown, how to test a RAG system with permission, and the controls that limit the damage. Each section links to a deeper tutorial or a practice lab.
How RAG works
- Ingest. Documents are split into chunks. Each chunk is turned into an embedding, a list of numbers that represents its meaning, and stored in a vector database.
- Retrieve. The user’s question is embedded the same way, and the database returns the chunks whose embeddings are closest to it.
- Generate. The retrieved chunks are placed in the prompt with the question, and the model writes an answer from them.
Each stage has its own weakness. Ingestion decides what gets in, retrieval decides who sees what, and generation decides how much the model believes what it was handed.
Where RAG fails
| Weakness | What happens | Main control |
|---|---|---|
| Knowledge base poisoning | Someone adds or edits a document so the assistant gives a false or harmful answer to certain questions | Control who can write to the knowledge base; review and version sources |
| Indirect prompt injection | A retrieved document contains instructions, and the model follows them | Treat retrieved text as untrusted data; limit what the assistant can do after reading it |
| Missing access control | The search returns chunks the current user is not allowed to see, and the model includes them in the answer | Filter by the user’s permissions at retrieval time, in code |
| Cross-tenant leakage | One customer’s data is retrieved for another customer in a shared index | Separate indexes or strict tenant filters |
| Embedding inversion | Stored embeddings are used to reconstruct the original text | Protect the vector database like the source documents |
| Stale or conflicting sources | Old and new versions of a policy are both retrieved, and the answer mixes them | Remove superseded content; record dates |
| Unsafe output handling | The answer, shaped by retrieved text, is rendered or executed without checks | Validate output before it reaches a browser, database or tool |
OWASP’s 2026 list covers these under LLM09: Vector and Embedding Weaknesses and LLM05: Data and Model Poisoning.
the RAG poisoning lab and the embedding poisoning lab.
Poisoning: a little goes a long way
A knowledge base with millions of documents sounds hard to corrupt. Research says otherwise. In the PoisonedRAG study, published in 2024, researchers showed that adding only a handful of crafted passages aimed at a chosen question was enough to make a RAG system give the attacker’s answer most of the time, even when the knowledge base was very large. Retrieval is built to find the closest match to a question, so a passage written to be the closest match wins.
The practical lesson is about write access. Any source that outsiders can influence is a way in: a public wiki, a shared drive, support tickets, product reviews, scraped web pages. More in our RAG poisoning tutorial.
Injection through retrieved content
Poisoning changes what the assistant says. Injection changes what it does. If a retrieved document contains text that reads like an instruction, the model may act on it, because it cannot reliably tell information from instruction. In a RAG assistant that also has tools, this is how a planted document becomes a data leak or an unwanted action.
See prompt injection in RAG systems, the prompt injection guide and the AI agent security guide.
Access control is the most common real failure
Many RAG projects start by indexing everything a company has, then put a chatbot in front of it. The documents had permissions in their original system. The vector database often does not. The assistant will then summarise a salary file or a board paper for anyone who asks the right question.
The fix is unglamorous: carry each document’s permissions into the index as metadata, and filter on the current user’s identity before anything reaches the model. The model must never be the thing that decides who may see a document.
Embeddings are not anonymous
An embedding looks like a meaningless list of numbers, and teams sometimes treat a vector database as less sensitive than the documents behind it. Research published in 2023 under the title “Text Embeddings Reveal (Almost) As Much As Text” showed that short passages can be reconstructed from their embeddings with high accuracy. A stolen vector database should be treated as a stolen copy of the documents.
How to test a RAG system
Test only systems you own or have written permission to test.
- Map the sources. List every place content comes from, and who can write to each one.
- Map the readers. List the user roles, and what each should and should not be able to retrieve.
- Test access control first. As a low-privileged user, ask about content only a high-privileged user should see.
- Test tenant separation. In a shared system, check that one tenant’s questions never return another tenant’s chunks.
- Test a writable source. With permission, add a clearly marked test document to a source you are allowed to change, and check whether it changes answers or behaviour.
- Check what is logged. Confirm you can see which chunks were retrieved for each answer. Without that, incidents cannot be investigated.
- Record and retest. Keep the question, the retrieved chunks and the answer. Retest after any change to sources, chunking or the model.
Our AI red teaming guide explains how this fits into a full engagement.
Defences that hold up
| Defence | What it does | Its limit |
|---|---|---|
| Permission filtering at retrieval | Users only ever retrieve what they are allowed to read | Permissions must be kept in sync with the source system |
| Controlled ingestion | Only reviewed sources enter the index; changes are versioned | Slows down adding new content |
| Source labelling and citations | Each answer shows which documents it came from | Helps people notice a bad source; does not prevent one |
| Separating trusted and untrusted sources | Public or user-written content is kept apart from internal policy | Needs a clear rule for every source |
| Limiting tools after retrieval | An assistant that has read untrusted content cannot send data out | Reduces what one assistant can do |
| Protecting the vector database | Encryption, access control and backups, as for the documents themselves | Standard security work, often skipped |
| Retrieval logging | Every answer can be traced to its chunks | Detects; does not prevent |
The principle is the same as for every AI system: assume the model will believe what it is given, and control what it can be given and what it can do next.
Frequently asked questions
Is RAG safer than fine-tuning?
They carry different risks. RAG keeps data outside the model, so it can be removed or corrected, and access can be checked per user. It also adds a live channel through which poisoned or injected content can arrive. Fine-tuning bakes data into the model, where it is harder to remove.
Does RAG stop a model making things up?
It reduces it when the right passage is retrieved. If the search returns the wrong passage, or a poisoned one, the model will answer confidently from that instead.
Can a system prompt tell the model to ignore instructions in documents?
It helps a little and fails often. The dependable controls are outside the model: what can be ingested, what each user can retrieve, and what the assistant is allowed to do.
Who is responsible for RAG security?
The team that builds the application. The model provider cannot see your knowledge base, your permissions or your users.
Where can I practise legally?
On systems built for it. The SecurityElites labs include RAG and embedding scenarios against live models, and the SE-ARTCP certification tests the skill hands-on.
