12. RAG Security, Privacy, and Prompt Injection
Protect tenant data, treat retrieved content as untrusted, secure uploads, and manage retention.
Threat Model a RAG System
Assets include source documents, ACLs, credentials, conversation history, indexes, and tools. Threats include unauthorized retrieval, prompt injection, poisoned sources, stale permissions, SSRF, unsafe rendering, cost abuse, and sensitive logging. Map each threat to a control and a test.
The model should not decide whether a user may access a document. Trusted application policy must govern access before content enters context.
Upload -> validate -> parse safely -> index
Query -> authenticate -> ACL filter -> retrieve
Context -> model -> validate citations -> safe render- Inspect the relevant pipeline stage instead of guessing.
- Preserve source provenance, versions, and access scope.
- Measure quality on representative queries.
What should determine whether a chunk can be returned?
Indirect Prompt Injection
Retrieved pages can contain instructions to ignore policy or reveal private records. Treat retrieved text as untrusted evidence, not application instructions. Delimiters help but are not complete defenses. Strong controls include least-privilege tools, scoped data access, minimized secrets in context, argument validation, network restrictions, and approval for sensitive actions.
Test hostile documents that attempt to reveal prompts, cross tenants, or trigger tools. The model should not gain new permissions from a document.
const allowedTools = new Map([
["searchKnowledgeBase", searchKnowledgeBase],
["getCurrentUserProfile", getCurrentUserProfile]
]);
async function executeTool(name,args,ctx) {
const tool=allowedTools.get(name);
if(!tool) throw new Error("Tool not allowed");
return tool(args,ctx);
}- Inspect the relevant pipeline stage instead of guessing.
- Preserve source provenance, versions, and access scope.
- Measure quality on representative queries.
What is a strong defense against injected cross-tenant access?
Source Poisoning and Trust
Poisoned documents may contain false information or misleading metadata. Verify source identity, ownership, publication status, and provenance. Keep model-generated summaries untrusted unless reviewed. Monitor unusual changes and preserve source lineage. For critical corpora, use approval workflows and rollback.
{
"sourceId":"policy-v4",
"origin":"approved-policy-repository",
"ownerVerified":true,
"publicationStatus":"approved",
"contentHash":"sha256:..."
}- Inspect the relevant pipeline stage instead of guessing.
- Preserve source provenance, versions, and access scope.
- Measure quality on representative queries.
What is a key defense against poisoned sources?
Privacy, Retention, and Deletion
Sensitive content can be copied into normalized text, chunks, vectors, caches, logs, and evaluation datasets. Define data minimization, retention, access controls, and deletion propagation. Embeddings should not be assumed anonymous. When source access is revoked, remove or block derived chunks and invalidate related caches according to policy.
Review data handling terms for the exact provider and configuration rather than assuming a universal policy.
Source deleted -> block serving -> remove chunks/vectors
-> invalidate caches -> update derived records -> audit completion- Inspect the relevant pipeline stage instead of guessing.
- Preserve source provenance, versions, and access scope.
- Measure quality on representative queries.
Why include caches and vectors in deletion workflows?
Upload Safety, SSRF, and Citation Rendering
Validate file size, type, signatures, archive expansion, and parser behavior; isolate risky parsers. If users submit URLs, block loopback/private/link-local/metadata destinations, restrict schemes and redirects, and revalidate resolved hosts to defend against SSRF. Render plain model text as text and sanitize permitted rich content. Validate citation URLs from trusted metadata; never execute extracted code.
Never fetch arbitrary model-suggested URLs from a privileged backend without network and destination controls.
function validateUrl(raw) {
const url=new URL(raw);
if(!["https:","http:"].includes(url.protocol)) throw Error("Bad scheme");
if(isPrivateOrBlockedHost(url.hostname)) throw Error("Blocked host");
return url;
}- Inspect the relevant pipeline stage instead of guessing.
- Preserve source provenance, versions, and access scope.
- Measure quality on representative queries.
What is SSRF protection intended to prevent?