This article presents a hands-on lab demonstrating embedding poisoning, an attack where adversaries craft documents optimized for high similarity scores to outrank legitimate sources in RAG (retrieval-augmented generation) systems. The attack parallels SEO spam but targets AI systems, exploiting the fact that retrievers rank by similarity, not authority, allowing attackers to inject false information into model responses. This matters because RAG systems increasingly power enterprise applications, and the technique is technically simple yet widely undefended against.
- Ranking determines model truth in RAGRAG systems return the top-k documents ranked by similarity to the query and feed them into the model's context. Whatever ranks highest becomes the factual basis for the model's response. An attacker who controls ranking controls what the model believes and outputs, regardless of the actual truth or source authority.
- Similarity scoring is decoupled from authorityThe retriever measures how closely a document matches query terms and semantics, not whether the content is accurate or from a trusted source. This fundamental decoupling means an attacker can write dense, keyword-optimized text that scores high on similarity without any legitimate claim to authority or accuracy.
- Attack technique uses keyword and semantic densityThe practical attack repeats exact query terms more frequently than the legitimate document, adds synonyms (e.g., 'return,' 'reimbursement,' 'money-back' for 'refund'), and wraps the false claim in an authoritative-sounding title like 'Updated Refund Policy 2024.' This combination exploits both keyword overlap and modern embedding models' treatment of semantic similarity.
- Real-world validation through academic researchThe article cites PoisonedRAG (2024) as evidence that a small number of optimized documents can control answers in realistic RAG setups. Real embeddings are harder to game than simple keyword overlap, but the attacker's fundamental approach—optimizing for retriever ranking—remains effective across different embedding models.
- Many RAG pipelines lack spam defensesThe article states that keyword stuffing is crude against modern search engines, but 'many RAG pipelines have no spam defences at all.' This defense gap means the attack succeeds not because the technique is sophisticated, but because systems assume documents in their index are trustworthy or that ranking is inherently reliable.
- Defense requires authentication and re-rankingRecommended defenses include: authenticating sources by indexing only from trusted repositories, re-ranking with a second model that scores both relevance and authority separately, carrying provenance metadata into the model prompt to favor official sources, and flagging anomalous keyword density or near-duplicate 'updated' documents.
- Attack surface expands with small top-k valuesWith a retriever's top-k set to 3 documents, an attacker only needs to outrank two legitimate sources to dominate the answer. Smaller top-k values reduce the attacker's work and increase the likelihood of poisoning success for targeted queries.
Bottom line: Embedding poisoning is a practical, underdefended attack that exploits RAG retrievers' reliance on similarity scoring over source authority; defenders must implement multi-layered controls including source authentication, dual-model re-ranking, and provenance tracking rather than assuming the retrieval ranking is reliable on its own.
📋 In this walkthrough
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
- Check the legitimate document: it mentions the key term only a couple of times.
- Submit a document titled as an updated refund policy for this year.
- 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.
- 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.
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.
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.
