AI Security
AI Security
RAG
LLM
Vector Database
Prompt Injection
Document Poisoning
Embeddings
Enterprise AI

Securing RAG Pipelines: Retrieval-Augmented Generation Threats & Defenses

SCR Security Research Team
December 10, 2025
18 min read

Introduction


Retrieval-Augmented Generation (RAG) has become the dominant architecture for enterprise LLM applications. Instead of relying solely on the model's training data, RAG retrieves relevant documents from a knowledge base and includes them in the prompt context. **Over 80% of enterprise LLM deployments use some form of RAG** (Gartner 2025).


But RAG introduces an entirely new attack surface. The retrieval pipeline — vector databases, embedding models, document ingestion, and chunk selection — is where most AI-specific vulnerabilities live. In 2024, researchers demonstrated that **a single poisoned document in a RAG knowledge base could compromise every response** the system generates on that topic.


---


How RAG Works (& Where It Breaks)


┌────────────────────────────────────────────────────┐

│ RAG ARCHITECTURE │

│ │

│ User Query │

│ │ │

│ ▼ │

│ ┌──────────────┐ │

│ │ Embedding │ ◄── Attack: Query manipulation │

│ │ Model │ │

│ └──────┬───────┘ │

│ │ Query Vector │

│ ▼ │

│ ┌──────────────┐ │

│ │ Vector DB │ ◄── Attack: Embedding poisoning │

│ │ (Similarity │ Attack: Index manipulation │

│ │ Search) │ │

│ └──────┬───────┘ │

│ │ Top-K Documents │

│ ▼ │

│ ┌──────────────┐ │

│ │ Context │ ◄── Attack: Document poisoning │

│ │ Assembly │ Attack: Prompt injection │

│ └──────┬───────┘ via retrieved content │

│ │ Augmented Prompt │

│ ▼ │

│ ┌──────────────┐ │

│ │ LLM │ ◄── Attack: Indirect injection │

│ │ Generation │ Attack: Data exfiltration │

│ └──────┬───────┘ │

│ │ │

│ ▼ │

│ Response to User │

└────────────────────────────────────────────────────┘


---


RAG-Specific Attack Vectors


1. Document Poisoning


An attacker with write access to the knowledge base (or through user-submitted content) injects documents containing:

  • **Indirect prompt injection** — Instructions that override the system prompt when retrieved
  • **Misinformation** — Factually incorrect documents that the LLM will cite confidently
  • **PII bait** — Content designed to make the LLM reveal personal data from other documents

  • **Real-world example:** Researchers at Princeton showed that poisoning just **0.001% of a RAG knowledge base** (5 documents out of 500,000) could cause the model to generate attacker-chosen content 88% of the time for targeted queries.


    2. Embedding Space Attacks


    Vector embeddings are the mathematical representations of text. Attackers can:

  • **Craft adversarial documents** that are semantically close to target queries but contain malicious content
  • **Collision attacks** — Create documents with embeddings that collide with high-value queries
  • **Embedding inversion** — Reconstruct original text from embeddings, leaking private documents

  • **Key stat:** A 2024 study showed embedding inversion attacks could reconstruct **92% of the original text** from its embedding vector on common models like OpenAI text-embedding-ada-002.


    3. Context Window Manipulation


    RAG systems have a fixed context window. Attackers can exploit this:

  • **Context flooding** — Submit many documents to push legitimate content out of the retrieval window
  • **Relevance hacking** — Craft documents that score artificially high on similarity, displacing real answers
  • **Chunk boundary exploitation** — Exploit how documents are split into chunks to hide malicious content at chunk boundaries

  • 4. Metadata Injection


    Many RAG systems include document metadata (titles, authors, dates) in the prompt. Attackers can:

  • Inject prompt instructions in metadata fields
  • Manipulate trust signals (e.g., set `source: "Internal Policy Document"`)
  • Use metadata to bypass content filtering

  • ---


    Securing RAG Pipelines


    Input Validation

    // Secure document ingestion pipeline

    interface DocumentIngestion {

    content: string;

    source: string;

    metadata: Record<string, string>;

    }


    function validateDocument(doc: DocumentIngestion): boolean {

    // 1. Scan for prompt injection patterns

    const injectionPatterns = [

    /ignore (all |your )?(previous |prior )?instructions/i,

    /you are now/i,

    /system prompt/i,

    /\[INST\]/i,

    /<<SYS>>/i,

    /### (System|Human|Assistant)/i,

    /\bdo anything now\b/i,

    ];


    for (const pattern of injectionPatterns) {

    if (pattern.test(doc.content) || pattern.test(JSON.stringify(doc.metadata))) {

    logSecurityEvent("injection_attempt", { source: doc.source, pattern: pattern.source });

    return false;

    }

    }


    // 2. Content length limits

    if (doc.content.length > 50000) return false;


    // 3. Metadata sanitization

    for (const [key, value] of Object.entries(doc.metadata)) {

    if (value.length > 500) return false;

    if (injectionPatterns.some(p => p.test(value))) return false;

    }


    // 4. Source verification

    if (!isAllowedSource(doc.source)) return false;


    return true;

    }


    Retrieval Security

    # Secure retrieval with access control and anomaly detection

    class SecureRAGRetriever:

    def __init__(self, vector_store, access_control):

    self.vector_store = vector_store

    self.access_control = access_control

    self.anomaly_detector = EmbeddingAnomalyDetector()


    def retrieve(self, query: str, user_id: str, top_k: int = 5):

    # 1. Get user's access level

    user_permissions = self.access_control.get_permissions(user_id)


    # 2. Filter documents by access control BEFORE retrieval

    allowed_collections = user_permissions.get_allowed_collections()


    # 3. Retrieve with access-filtered search

    results = self.vector_store.similarity_search(

    query=query,

    k=top_k * 3, # Over-retrieve then filter

    filter={"collection": {"$in": allowed_collections}}

    )


    # 4. Anomaly detection on retrieved embeddings

    safe_results = []

    for doc in results:

    if self.anomaly_detector.is_anomalous(doc.embedding):

    log_security_event("anomalous_embedding", doc.metadata)

    continue

    safe_results.append(doc)


    # 5. Content safety check on retrieved text

    safe_results = [

    doc for doc in safe_results

    if not self.contains_injection(doc.page_content)

    ]


    return safe_results[:top_k]


    ---


    RAG Security Checklist


  • **Document Ingestion:** Scan all documents for prompt injection before indexing
  • **Access Control:** Enforce per-user/per-role document access at the vector DB level
  • **Embedding Monitoring:** Track embedding distribution for anomalies
  • **Content Filtering:** Apply safety classifiers to both retrieved content and final output
  • **Chunk Isolation:** Never combine chunks from different trust levels in one context
  • **Metadata Sanitization:** Strip or validate all metadata before including in prompts
  • **Audit Logging:** Log all retrievals with document IDs and user context
  • **Freshness Controls:** Set TTL on indexed documents, re-validate periodically
  • **Canary Documents:** Insert tripwire documents that trigger alerts if retrieved inappropriately

  • ---


    Key Statistics & Research


  • **80%+** of enterprise LLM deployments use RAG (Gartner 2025)
  • **88%** attack success rate with just 5 poisoned documents in 500K (Princeton 2024)
  • **92%** text reconstruction from embeddings demonstrated (UC Berkeley 2024)
  • **67%** of RAG deployments have no document-level access control (LangChain survey)
  • **3 out of 4** RAG applications tested were vulnerable to indirect prompt injection (OWASP)
  • Vector database market projected to reach **$4.3 billion by 2028** (IDC)

  • ---


    Conclusion


    RAG is not inherently insecure — but the default implementation patterns are. Document poisoning, embedding attacks, and indirect prompt injection are real, demonstrated threats. Every RAG pipeline must include input validation, access control, anomaly detection, and output filtering.


    The good news: unlike LLM model-level vulnerabilities, RAG security is mostly an engineering problem with known solutions. Build the guardrails before you ship.


    **Related Resources:**

  • [AI Security & LLM Threats](/blog/ai-security-llm-threats) — Comprehensive AI threat guide
  • [AI Red Teaming Guide](/blog/ai-red-teaming-guide) — Adversarial testing methodologies
  • [OWASP Top 10 for AI/LLM](/owasp/top-10-ai) — Full vulnerability taxonomy
  • [Secure Code Examples](/secure-code) — Secure coding patterns