Embedding Poisoning Lab: Outrank the Real Document in RAG

✨ AI Summary
PolicyBot answers questions from the three documents its retriever ranks closest to the user's query. Nobody gets to edit the legitimate refund policy. You do not need to: you only need a document that ranks higher than it. This is SEO spam, rebuilt for the RAG era, and it is the most underrated attack in AI security today.
Lab at a glance: Advanced · 145 XP · about 20 minutes · runs in your browser, no signup or setup. Try it yourself first; the full solution is behind a spoiler below.

What this lab teaches

In a RAG pipeline, whatever ranks highest becomes the truth the model speaks. Retrieval ranking is driven by similarity between the query and the document. If an attacker can submit documents, they can optimise for that similarity and push the real source out of the context.

How PolicyBot is set up

PolicyBot uses a similarity score (a simplified stand-in for real embeddings) to pick the top three documents for each query. A legitimate refund policy is already in the index. The lab is solved when your document outranks it for the query “refund policy” and PolicyBot repeats your claim that refunds are unconditional.

The attack approach

  • Find out what the target query is and which document currently wins it.
  • Write a document dense with the exact query terms, more than the legitimate document has.
  • Add close synonyms that real embeddings would treat as related.
  • Give it an authoritative-sounding title. The model, and any human glancing at sources, gives “updated” policies more weight.

Full walkthrough (spoiler)

Stuck after a few attempts? Open the steps below. Solving it yourself first is worth more XP, and more learning.

Show the step-by-step solution
  1. Check the legitimate document: it mentions the key term only a couple of times.
  2. Submit a document titled as an updated refund policy for this year.
  3. In the body, repeat the phrase “refund policy” several times and add related terms such as return, reimbursement and money-back, along with the false claim that all refunds are unconditional.
  4. Query “refund policy”. Your document now ranks first, and PolicyBot quotes it.

Why it works

Similarity is not authority. The retriever measures how close a document is to the question, not whether it is true or who wrote it. Keyword stuffing is crude against modern search engines, but many RAG pipelines have no spam defences at all.

Core principleRanking decides what the model believes. Anyone who can influence ranking can influence answers.

Where this happens in the real world

Academic work such as PoisonedRAG (2024) showed that a small number of optimised documents can control answers for targeted questions in realistic RAG setups. Real embeddings are harder to game than keyword overlap, but the attacker’s goal and method are the same.

How to defend against it

  • Authenticate sources: index policy content only from trusted internal repositories.
  • Re-rank with a second model that scores relevance and authority, not just similarity.
  • Carry provenance metadata into the prompt so the model can prefer official sources.
  • Flag anomalous keyword density and near-duplicate “updated” documents.
Mr Elite tip: In a real assessment, ask what the retriever's top-k is. With top-3, you only need to beat two documents to dominate an answer.

Try it, then go further

Reading about it is the easy part. Open the lab, try your own phrasing before peeking at the spoiler, and see how many attempts it takes.

❓ Frequently asked questions

What is embedding poisoning?
Crafting documents so a RAG system's retriever ranks them highly for chosen queries, putting attacker-controlled content into the model's context.
Is this the same as RAG poisoning?
They are related. RAG poisoning focuses on what the document says to the model; embedding poisoning focuses on making sure the document gets retrieved.
Do real embeddings stop keyword stuffing?
They make it harder, but optimised text and synonyms still shift similarity. Defences must not rely on the embedding model alone.
What should I try next?
The RAG poisoning lab, then combine both techniques in your own test environment.
⚡
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

Your email address will not be published. Required fields are marked *