Home/ Workshops/ Enterprise AI Security Workshop/ Chapter 8 · Deep Dive

Chapter 8 of 8 · Deep Dive · ~10 min read

Deep Dive: RAG Security

Retrieval-Augmented Generation has seven distinct stages, and each one is a separate security boundary with its own failure modes.

Summary

Retrieval-Augmented Generation (RAG) grounds a model's responses in your own data — but "RAG" is not one thing to secure, it's seven stages: Ingestion, Storage, Indexing, Retrieval, Prompt Assembly, Model Inference, and Output. Each stage has a distinct risk profile, and a control that protects one stage (say, access control on the vector index) does nothing for another (say, a poisoned document making it into the index in the first place). This chapter walks the pipeline stage by stage.

Core concepts

The reason to think in stages rather than "the RAG system" as one unit: a document can be perfectly clean at Ingestion and still leak data at Retrieval if access control isn't enforced there too. Securing one stage never implies the others are secure.

Architecture discussion — stage by stage

1. Ingestion

RiskWhat can go wrongAzure-native controls
Poisoned documentsMalicious instructions embedded in PDFs/docsDefender for Storage (malware scanning); Azure AI Content Safety (pre-index scan); manual review workflow for high-risk sources
MalwareEmbedded exploits in filesDefender for Storage malware detection
Credential leakageSecrets embedded in docsContent Safety + regex scanning; Key Vault secret scanning policies
Unauthorized ingestionAnyone can upload contentEntra ID auth; managed identity for ingestion pipelines; RBAC on storage

2. Storage

RiskWhat can go wrongAzure-native controls
Data exfiltrationPublic blobs or shared keysDisable public access; Private Endpoints; RBAC-only access
Unauthorized accessBroad contributor rolesLeast-privilege RBAC; separate containers for raw vs. processed data
Compliance violationsData retention / deletion gapsImmutable blob policies; lifecycle management rules
Key compromiseStorage keys leakedAzure Policy: disable key-based auth

3. Indexing (Azure AI Search)

RiskWhat can go wrongAzure-native controls
Over-indexingSensitive fields indexed unintentionallyField-level controls (searchable, retrievable); index schema reviews
Cross-tenant leakageOne user sees another tenant's dataTenant ID stored as metadata; mandatory filter expressions
Index abuseDirect access to search endpointPrivate Endpoint only; Entra ID auth; disable API keys

4. Retrieval

RiskWhat can go wrongAzure-native controls
Over-retrievalToo many documents returnedTop-K limits enforced in code / APIM
Unauthorized data accessUser retrieves data beyond clearanceEntra ID claims → filter expressions; row-level security (RLS)
Prompt injection via retrieved textInstructions hidden in documentsSystem-prompt isolation; Content Safety scan on retrieved chunks

5. Prompt Assembly

RiskWhat can go wrongAzure-native controls
Prompt overrideUser says "ignore system prompt"APIM prompt firewall; fixed system-prompt templates
Context stuffingExcessive data injectedPrompt size limits in APIM
Tool abuseModel calls unintended toolsTool allow-listing; explicit tool schemas
Data leakageSensitive data added to promptPrompt-inspection policies; structured prompt formats

6. Model Inference

RiskWhat can go wrongAzure-native controls
Prompt injectionModel follows malicious instructionsDefender for AI runtime detection
Data exfiltrationModel outputs sensitive dataDefender for AI alerts; token anomaly detection
AbuseExcessive or automated usageRate limiting (APIM); quotas in Azure OpenAI
Shadow AIUnmanaged model usageDefender for Cloud AI discovery

7. Output

RiskWhat can go wrongAzure-native controls
PII leakageSSNs, emails, PHI in responseAzure AI Content Safety (PII detection); optional Presidio on Azure
Unsafe contentToxic or policy-violating outputContent Safety filters
Hallucinated actionsModel invents actions/commandsOutput schema validation; human-in-the-loop approvals

Key security considerations

  • The most dangerous risks span multiple stages. Prompt injection via retrieved text appears at both Retrieval (stage 4) and Model Inference (stage 6) — an attacker who successfully poisons a document at Ingestion is exercising a multi-stage attack chain, not a single exploit.
  • Cross-tenant leakage at the Indexing stage is a governance failure, not just a technical one — mandatory filter expressions only work if every query path actually enforces them; a debugging tool or admin console that bypasses the filter reintroduces the exact risk it was built to prevent.
  • Output validation is the last line of defense, not the first. By the time a hallucinated action or PII leak reaches Output, five prior stages have already had a chance to catch it — treat a repeated Output-stage failure as a signal to audit earlier stages, not just tighten the output filter.
  • Rate limiting and quotas (stage 6) are a security control, not just a cost control — unmetered inference is also unmetered exfiltration bandwidth.

Recommended practices

  1. Map your own RAG pipeline to these seven stages explicitly, and confirm you have at least one control per stage before calling the pipeline "secured" — a single strong control at one stage does not compensate for a missing control at another.
  2. Enforce tenant and clearance filtering (Indexing and Retrieval stages) as mandatory query parameters the search service itself rejects requests without — not as an application-layer convention that can be bypassed.
  3. Scan retrieved content for injected instructions at Retrieval, before it's assembled into a prompt — waiting until Model Inference to catch it means the model has already seen the malicious content.
  4. Apply the same "log hash, not content" telemetry discipline from Chapter 6 to RAG-specific logs — prompt assembly and retrieval logs are just as capable of leaking sensitive data as application logs.
  5. Require human-in-the-loop approval for any output that triggers a downstream action (not just a response) — hallucinated actions are a distinct risk from hallucinated facts.

Technologies referenced

  • Microsoft Defender for Storage — malware scanning at Ingestion.
  • Azure AI Content Safety — pre-index scanning, retrieved-chunk scanning, and PII/unsafe-content detection at Output.
  • Azure Key Vault — secret scanning policies at Ingestion.
  • Azure AI Search — field-level access controls and mandatory filter expressions at Indexing.
  • Azure API Management (APIM) — Top-K limits, prompt firewall, prompt-size limits, and rate limiting across Retrieval, Prompt Assembly, and Model Inference.
  • Microsoft Defender for AI — runtime prompt-injection and exfiltration detection at Model Inference.
  • Microsoft Entra ID — claims-based filtering enforced at Retrieval.

Key takeaways

  • RAG is seven distinct security boundaries — Ingestion, Storage, Indexing, Retrieval, Prompt Assembly, Model Inference, Output — not one system to secure once.
  • Multi-stage attacks (a poisoned document at Ingestion that triggers prompt injection at Retrieval and Model Inference) are the highest-risk pattern; single-stage controls don't stop them.
  • Output-stage filtering is the last line of defense; recurring output failures are a signal to audit ingestion, indexing, and retrieval controls, not just tighten the output filter.