Courses / RAG Systems / Module 10
Module 10: Access control and multi-tenant RAG: per-document permissions, PII, audit
8 h · theory 2 · hands-on 3 · project 2 · revision 1
You will be able to
- Enforce per-document permissions at query time
- Isolate tenants in one index
- Redact PII before indexing
- Defend against prompt injection hidden in documents
Capstone increment: Permission-aware retrieval with an audit log and an injection test set.
Job-description skills: Access control, Data governance · Lab tools: qdrant-client, presidio
1. The assistant must never know more than the user
An enterprise assistant indexes documents with different readers: HR files, board papers, customer contracts, another tenant’s data. If retrieval ignores permissions, the model will happily summarise a salary file for an intern. This is the most damaging RAG bug, and it is invisible in normal testing because developers usually have broad access.
Security is designed into both halves: ingestion keeps access lists, redacts personal data and screens text; the query path filters by verified identity, treats retrieved text as data, and writes an audit log.
2. Enforce permissions inside the search
Store with every chunk the tenant and the groups (or users) allowed to read it, copied from the source system’s permissions. At query time, take the user’s identity from a verified token or a server-side lookup, never from the request body, and pass the permission filter into the search so the top k are drawn only from readable chunks.
Permissions change. When a document’s access list changes, the index must reflect it quickly: sync on change events where the source offers them, or keep a document-level permission table and join it at query time (natural in pgvector) so a change needs one row update, not re-indexing every chunk.
Intern is in all-staff · chunks sent to the model 5 · restricted chunks in the context 5
| Score | Document | Readable by | |
|---|---|---|---|
| 0.94 | Compensation bands 2026 (confidential) | hr | LEAKED to context |
| 0.91 | Salary review notes: engineering | hr, eng-managers | LEAKED to context |
| 0.87 | Offer letter templates with salary ranges | hr | LEAKED to context |
| 0.85 | Engineering levels: Senior Engineer expectations | engineering | LEAKED to context |
| 0.83 | Payroll export, October | hr, finance | LEAKED to context |
| 0.80 | Career framework overview | all-staff | |
| 0.78 | Promotion process for engineers | engineering | |
| 0.74 | Total rewards: benefits at a glance | all-staff | |
| 0.72 | How pay reviews work (public FAQ) | all-staff | |
| 0.66 | Interview rubric: senior backend | engineering | |
| 0.31 | Holiday calendar 2026 | all-staff | |
| 0.29 | Quarterly budget forecast | finance |
Guided experiments
- As the intern, with no check, what reaches the model? Now try “tell the model not to use restricted docs”. Why is that not access control?
- Switch to “filter the top 5 after search”. How many chunks does the intern get, and what will the assistant answer? This is the most common permission bug in RAG.
- Use “filter inside the search” for every user. Each gets the best documents they may read. Where must the user’s groups come from so a user can’t claim to be in HR?
- The engineering manager can read “Salary review notes” but not the compensation bands. If their permissions change today, how quickly must the index reflect it, and how would you test that?
3. Multi-tenancy
Three common layouts: a collection or database per tenant (strong isolation, more to operate), one shared collection with an indexed tenant field and a mandatory filter (efficient, needs care), or a hybrid where regulated or very large tenants get their own. Qdrant, for example, has payload indexes tuned for tenant fields; Postgres can add row-level security.
Whatever the layout, make cross-tenant leakage a test: for every test user, assert that no returned chunk belongs to another tenant or to a group they aren’t in. Add per-tenant quotas and rate limits so one tenant’s bulk import can’t slow everyone else.
4. Personal data before indexing
Indexing personal data you don’t need makes every later leak worse. Redact or pseudonymise it at ingestion: Microsoft Presidio with custom recognisers handles names, phone numbers, emails and Indian identifiers such as PAN and Aadhaar; check its precision on labelled samples from your corpus.
Plan for deletion. India’s Digital Personal Data Protection Act, 2023, like GDPR, expects you to honour erasure requests and keep personal data secure. You need to find and delete every chunk derived from a person’s documents, which is easy only if every chunk stores its source document id.
5. Injection hidden in documents
Anything you index can contain instructions aimed at the model: “ignore previous instructions”, invisible text in a PDF, or a markdown image whose URL carries data out when rendered. This is indirect prompt injection, and RAG makes it a supply-chain problem.
Defend in layers: scan text at ingestion and quarantine suspicious chunks for review, wrap retrieved text in clear delimiters and tell the model it is data, give the assistant no tools it doesn’t need, strip or allow-list links and images in output, and keep an injection test set of poisoned documents in CI. The audit log (who asked what, which chunks were returned) turns an incident from a guess into an investigation.
Hands-on: coding window
The coding window and the full VS Code lab are available after you sign in.
Sign in to codeLabs
Lab 10.1: ACL-filtered search
Add tenant and groups payloads to the capstone index, read groups from a verified JWT, and write a pytest that runs every test user against 20 queries and asserts zero leaks.
Expected: The leak test passes and fails loudly if the filter is removed.
Lab 10.2: PII at ingestion
Run Presidio with custom PAN and Aadhaar recognisers over 50 labelled lines from your corpus and measure precision and recall; wire it into ingestion.
Expected: Precision and recall per entity type, and redacted chunks in the index.
Lab 10.3: Audit and injection tests (capstone increment)
Add an audit log of user, query and returned chunk ids, and an injection test set of 15 poisoned documents that CI checks: no instruction is followed, no external link is rendered.
Expected: CI runs the injection suite on every pull request; the audit log answers “who saw document X?”.
Go deeper
Videos
- Permissions & Access Control for RAG - a Deep Dive Tutorial · Paragon · ~20 min. A deep dive into syncing and enforcing permissions for RAG over SaaS data sources.
- Access control for RAG and LLMs · Cerbos · ~7 min. Why authorisation belongs outside the model, explained by an access-control vendor.
- Prompt Injection, explained · Simon Willison · ~12 min. Prompt injection explained by the person who named it, including indirect injection.
Further reading
- Docs OWASP Top 10 for LLM Applications · OWASP Gen AI Security Project. The standard list of LLM application risks, each with examples and mitigations.
- Docs Multitenancy · Qdrant docs. Payload-based partitioning and tenant-aware indexes: permission filters inside the search.
- Paper Not what you've signed up for: Compromising Real-World LLM-Integrated Applications with Indirect Prompt Injection · Greshake et al., 2023. The paper that showed instructions hidden in retrieved documents and web pages can take over an LLM application.
GitHub repositories
- data-privacy-stack/presidio: Custom recognisers (pattern and context words) and the anonymizer operators: replace, mask, hash.
- openfga/openfga: Relationship-based access control (the Google Zanzibar model) for fine-grained document permissions.
- qdrant/qdrant: The multitenancy guide: tenant payload indexes, and when to use separate collections.
Checkpoint quiz
Interview questions
- How do you enforce document permissions in a RAG system? Where do they come from?
- Compare separate collections per tenant with a shared collection and a tenant filter.
- How do you test that no user can see another tenant’s data?
- How do you handle a right-to-erasure request for a RAG index?
- What is indirect prompt injection, and how do you defend a RAG system against it?
Ask the professor
Sign in to ask questions about this module.
Discussion
Community discussion appears here once giscus is configured (see README).