Securing RAG — Document Permissions and Data Leakage
Enforce per-user document permissions in retrieval, prevent cross-tenant leakage, and handle poisoned or untrusted documents.
Retrieval Must Respect the Same Permissions as the Source
Retrieval-augmented generation (RAG) fetches relevant material and places it in the context sent to a model. The retrieval index may contain public FAQs, internal procedures, and customer-specific documents. Similarity search answers "what seems relevant?" It does not answer "may this user read it?" A secure design makes that second decision before content enters a prompt.
Imagine a SaaS learning platform where instructors can see their own cohort’s feedback, while learners see only their own submissions and public course materials. If the application retrieves a different cohort’s document and relies on the model not to quote it, confidentiality has already failed: the data crossed the boundary into the model context, logs, or provider request.
Bind Every Retrieval to a Verified Principal
A retrieval request should carry a server-verified user or service identity, tenant membership, and relevant role. Do not accept a tenant ID, role, or access filter supplied only by the browser or generated by the model. Resolve those values from trusted session claims or an authorization service, then apply them in the retrieval layer and again at the source of truth when feasible.
| Boundary | Required decision | Common failure |
|---|---|---|
| Authentication | Who is making this request? | Trusting a user ID embedded in a prompt |
| Tenant isolation | Which tenant may this principal access now? | Using a client-supplied tenant filter |
| Document authorization | Does this user have access to this specific document? | Treating vector similarity as permission |
| Prompt construction | Which authorized excerpts are necessary for the task? | Sending every candidate chunk to the model |
| Output handling | Can the answer expose or render sensitive fields? | Assuming correct retrieval makes every output safe |
Permissions can change after indexing. Store stable source identifiers and current access metadata, and decide how quickly ACL changes invalidate indexes or caches. For sensitive records, fetch or verify permissions against an authoritative service rather than relying solely on stale metadata copied into an embedding store.
Apply Authorization Before Ranking and Prompt Assembly
The following Python example demonstrates the order of operations with in-memory records: derive the permitted set from the verified principal, then rank only those records, and finally limit the context. A production vector store should enforce equivalent filtering in the query and verify that the filter cannot be omitted or broadened by an untrusted caller.
from dataclasses import dataclass
@dataclass(frozen=True)
class Principal:
user_id: str
tenant_id: str
@dataclass(frozen=True)
class Document:
doc_id: str
tenant_id: str
allowed_users: frozenset[str]
text: str
score: float
def retrieve(principal: Principal, documents: list[Document], limit: int = 3) -> list[Document]:
if limit < 1 or limit > 10:
raise ValueError("limit is outside the service policy")
permitted = [
doc for doc in documents
if doc.tenant_id == principal.tenant_id
and principal.user_id in doc.allowed_users
]
return sorted(permitted, key=lambda doc: doc.score, reverse=True)[:limit]
principal = Principal(user_id="learner-8", tenant_id="cohort-a")
documents = [
Document("guide", "cohort-a", frozenset({"learner-8"}), "Public course guide", 0.91),
Document("feedback", "cohort-a", frozenset({"instructor-2"}), "Private learner feedback", 0.95),
Document("other", "cohort-b", frozenset({"learner-8"}), "Another cohort note", 0.99),
]
for document in retrieve(principal, documents):
print(document.doc_id, document.text)Keep Tenant Boundaries Through Every Layer
A correct retrieval filter can be undermined by a shared cache keyed only by query text, a background job using a global service identity, or a debug endpoint that bypasses the normal policy. Partition caches by tenant and relevant authorization context, avoid returning one user’s cached answer to another, and invalidate entries after permission changes. Treat the response cache as another data store with access rules.
- Derive principal and tenant from a trusted server-side identity, never from model-generated text.
- Apply tenant and document-level policy in the retrieval query before content is returned to the application.
- Check any high-sensitivity result against the authoritative source before prompt construction.
- Keep source IDs and provenance with each chunk; avoid mixing sensitive and public data into an untraceable blob.
- Scope caches, indexes, logs, and background service credentials to the same isolation model.
- Re-authorize sensitive actions and reads at the source of truth when permissions may have changed.
A cache key may need tenant, user or role, permission version, query normalization, and model or prompt version. The right key depends on your policy. If you cannot prove that cached context is safe to reuse, do not share that cache entry across principals.
Handle Poisoned or Misleading Documents
RAG content can be outdated, incorrect, or intentionally crafted to influence the model. Treat retrieved material as evidence with provenance, not as new system policy. Ingestion should authenticate sources, validate file type and size, scan and parse safely, preserve ownership and ACL metadata, and record a content hash or version. Quarantine suspicious or unreviewed sources and provide a correction or removal path.
from dataclasses import dataclass
from hashlib import sha256
@dataclass(frozen=True)
class IngestionRecord:
source_id: str
tenant_id: str
owner_id: str
approved: bool
digest: str
def register_source(source_id: str, tenant_id: str, owner_id: str, raw: bytes, approved: bool) -> IngestionRecord:
if not source_id or not tenant_id or not owner_id:
raise ValueError("source identity and ownership are required")
if not raw or len(raw) > 2_000_000:
raise ValueError("source is empty or exceeds the ingestion limit")
return IngestionRecord(
source_id=source_id,
tenant_id=tenant_id,
owner_id=owner_id,
approved=approved,
digest=sha256(raw).hexdigest(),
)
record = register_source("policy-7", "cohort-a", "staff-4", b"Approved course policy", True)
print(record.source_id, record.digest, record.approved)A digest helps identify whether bytes changed; it does not prove that the content is truthful, safe, or authorized. Approval workflows, source authentication, review, versioning, and permission checks address different properties. When retrieved text is included in a prompt, retain enough provenance to explain which source informed the answer.
Prove Isolation with Negative Tests
Unit tests should cover the policy function, but end-to-end tests are necessary too: test the actual query path, cache, prompt builder, and API response. Include tenant-A versus tenant-B records, users with different roles, changed ACLs, empty results, duplicate IDs, and retrieval-store errors. A fail-open fallback that searches without a filter is a confidentiality incident waiting to happen.
| Test case | Expected result |
|---|---|
| Allowed user and document in same tenant | Document may be retrieved if relevant |
| Same user ID but a different tenant | No cross-tenant content appears |
| Document belongs to another role in the same tenant | Document is excluded unless policy grants access |
| ACL revoked after a cache entry exists | Old context is invalidated or re-authorized before use |
| Authorization service unavailable | Sensitive retrieval fails closed with a safe user-facing response |
Monitor authorization denials and unusual retrieval patterns using minimized metadata. Restrict access to traces, set retention based on operational need, and avoid sending unnecessary PII to model providers. A security test should prove that a denied document never enters the model request—not merely that the final answer omitted its text.
Common Mistakes
Filtering only after retrieval results are returned
Why: Unauthorized content may already be included in logs, caches, or prompt assembly.
Fix: Apply authorization in the retrieval query and verify before context construction.
Trusting a client-supplied tenant ID
Why: A caller can change request fields without changing authenticated identity.
Fix: Derive tenant membership on the server from the principal and current policy.
Sharing answer caches across users without policy context
Why: A cached answer may contain information retrieved for a different identity.
Fix: Partition or disable caching unless reuse is proven safe under current ACLs.
Treating an embedding or digest as a trust signal
Why: Similarity and integrity hashes do not establish authorization or factual accuracy.
Fix: Keep source provenance, approval state, permissions, and content review distinct.
Design Tenant-Safe Course Search
Practice
Build and test a document authorization plan
- Define a principal, tenant membership, role, and document ACL model for learners, instructors, and public course resources.
- Write pseudocode for retrieval that applies permissions before similarity ranking and caps the amount of context.
- Create a cross-tenant test with an unauthorized document ranked above every allowed document.
- Describe cache keys and invalidation behavior after a role is removed or an ACL changes.
- Define how ingestion validates source, ownership, approval, size, version, and removal requests.
- Specify what a trace may record without storing full prompts or sensitive document text.
Hint: Use fake user and tenant identifiers. The strongest assertion checks that unauthorized source IDs never reach the model request.
🎯 Key Takeaways
- ✓Vector similarity finds relevant content; only an authorization policy determines whether a principal may read it.
- ✓Derive user and tenant identity from trusted server-side authentication, not client fields or model output.
- ✓Filter before prompt assembly and preserve source IDs, ACL metadata, and provenance through the RAG pipeline.
- ✓Treat caches, background jobs, logs, and indexes as data stores that need tenant-aware access controls.
- ✓Ingestion integrity checks do not prove a document is safe or accurate; maintain source review and correction workflows.
- ✓Test that unauthorized documents never reach model context, including failure and stale-cache cases.
- Reference: OWASP Top 10 for LLM Applications (2025), especially LLM02 Sensitive Information Disclosure and LLM08 Vector and Embedding Weaknesses.
- Reference: MITRE ATLAS current entries can help identify relevant AI attack scenarios; adapt them to your RAG architecture.
Next
What comes next
Retrieval is one boundary. Next, limit agent capabilities and protect external actions with least privilege, validation, approval, and isolation.
Lesson 05 · Securing Tools and Agents — Excessive Agency and Sandboxing →HELP US IMPROVE
How was this lesson?
Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.
Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.
