Retrieval-augmented generation, or RAG, is the most common way businesses put large language models to work: an assistant searches your documents, wikis, tickets or shared drives, and the model answers from what it finds. It is useful and it is risky, because the knowledge base becomes part of the attack surface. A RAG assistant can hand any user a document they should never see, and a single poisoned document can steer the model for everyone who asks a related question. This guide covers how RAG systems are attacked and the controls that defend them.
How a RAG system works
Documents are split into chunks, converted into numerical embeddings and stored in a vector database. When a user asks a question, the system finds the most similar chunks and passes them to the model with the question, and the model writes an answer from them. Each stage, ingestion, storage, retrieval and generation, has its own security questions.
Risk 1: Data leakage through retrieval
The most common RAG problem is not an attack at all. Teams index a whole shared drive or wiki so the assistant can answer anything, without carrying over the permissions on each document. Now an employee can ask about salaries, board papers or a colleague's HR case and get an answer. The model is doing exactly what it was built to do.
The fix is to enforce the user's permissions at retrieval time, so the search only returns chunks from documents that user could open directly. Where that is not possible, keep separate indexes for different audiences and only index content every user of that assistant is allowed to read.
Risk 2: Indirect prompt injection
Any text the model reads can contain instructions. An attacker who can get a document into your knowledge base, through a shared folder, a support ticket, an uploaded file or a public web page you crawl, can hide text telling the model to ignore its rules, give false answers, include a malicious link or, if the assistant has tools, take an action. Hidden text such as white-on-white or tiny fonts is invisible to people reviewing the document but plain to the model.
No filter reliably catches this, so limit the damage. Keep the assistant's tools and permissions minimal, require confirmation for actions, strip hidden content during ingestion, and treat sources you do not control as untrusted.
Risk 3: Knowledge base poisoning
Even without explicit instructions, a planted or edited document can change answers: wrong bank details in a finance policy, a fake IT support number, or an outdated procedure that looks current. Control who can add or change indexed content, log every change, prefer authoritative sources and show users which documents an answer came from so they can check.
Risk 4: The vector store itself
Vector databases hold your data in a form that can often be partly reversed into the original text, so treat them as sensitive as the documents. Require authentication, keep them off the public internet, encrypt them, back them up and include them in access reviews. Several vector database and LLM tooling products have shipped with default settings that allowed open access, so check configuration rather than assuming.
Risk 5: Output handling and logging
Answers can include sensitive text from retrieved chunks, and logs of prompts and answers can become a second copy of everything users asked about. Encode model output before rendering it, filter obvious sensitive patterns as a last check, and apply the same retention and access rules to logs as to the source data.
A checklist for secure RAG
Before going live and after major changes, check each of these.
- Document permissions are enforced at retrieval, tested with users of different roles.
- Only approved sources are indexed, with an owner for each.
- Changes to indexed content are controlled and logged.
- Hidden text and active content are stripped during ingestion.
- The vector store is authenticated, private and encrypted.
- Assistant tools are minimal and risky actions need confirmation.
- Answers cite their sources.
- Prompts, retrieved chunks and answers are logged with proper retention.
Test it like an attacker
An AI and LLM penetration test checks whether users can retrieve documents they should not see, whether planted content can steer the assistant, and whether the vector store and APIs are exposed. For the wider picture of how data escapes through AI tools, see our guide to AI data leakage, and for why filters alone fall short, why LLM output filtering is not enough.
Frequently asked questions
What does RAG security cover?
Can a RAG chatbot leak confidential documents?
How does prompt injection work in RAG?
Is the vector database sensitive?
Test your RAG assistant before users do
We test retrieval permissions, prompt injection through documents and the security of your vector store and APIs, with clear fixes.

