RAG Security Explained: How Retrieval Systems Are Attacked and How to Defend Them

✨ AI Summary
By Updated

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

  1. 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.
  2. Retrieve. The user’s question is embedded the same way, and the database returns the chunks whose embeddings are closest to it.
  3. 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

WeaknessWhat happensMain control
Knowledge base poisoningSomeone adds or edits a document so the assistant gives a false or harmful answer to certain questionsControl who can write to the knowledge base; review and version sources
Indirect prompt injectionA retrieved document contains instructions, and the model follows themTreat retrieved text as untrusted data; limit what the assistant can do after reading it
Missing access controlThe search returns chunks the current user is not allowed to see, and the model includes them in the answerFilter by the user’s permissions at retrieval time, in code
Cross-tenant leakageOne customer’s data is retrieved for another customer in a shared indexSeparate indexes or strict tenant filters
Embedding inversionStored embeddings are used to reconstruct the original textProtect the vector database like the source documents
Stale or conflicting sourcesOld and new versions of a policy are both retrieved, and the answer mixes themRemove superseded content; record dates
Unsafe output handlingThe answer, shaped by retrieved text, is rendered or executed without checksValidate 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.

  1. Map the sources. List every place content comes from, and who can write to each one.
  2. Map the readers. List the user roles, and what each should and should not be able to retrieve.
  3. Test access control first. As a low-privileged user, ask about content only a high-privileged user should see.
  4. Test tenant separation. In a shared system, check that one tenant’s questions never return another tenant’s chunks.
  5. 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.
  6. Check what is logged. Confirm you can see which chunks were retrieved for each answer. Without that, incidents cannot be investigated.
  7. 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

DefenceWhat it doesIts limit
Permission filtering at retrievalUsers only ever retrieve what they are allowed to readPermissions must be kept in sync with the source system
Controlled ingestionOnly reviewed sources enter the index; changes are versionedSlows down adding new content
Source labelling and citationsEach answer shows which documents it came fromHelps people notice a bad source; does not prevent one
Separating trusted and untrusted sourcesPublic or user-written content is kept apart from internal policyNeeds a clear rule for every source
Limiting tools after retrievalAn assistant that has read untrusted content cannot send data outReduces what one assistant can do
Protecting the vector databaseEncryption, access control and backups, as for the documents themselvesStandard security work, often skipped
Retrieval loggingEvery answer can be traced to its chunksDetects; 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.

⚡
Join free to earn XP for reading this article Track your progress, build streaks and compete on the leaderboard.
Join Free
Lokesh N. Singh aka Mr Elite
Lokesh N. Singh aka Mr Elite
Founder, Securityelites · AI Red Team Educator
Founder of Securityelites and creator of the SE-ARTCP credential. Working penetration tester focused on AI red team, prompt injection research, and LLM security education.
About Lokesh ->

Leave a Comment