Skip to content

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.

Source + ACLwho may read itRedact PIIPresidio, regexScan texthidden instructionsIndexchunk + ACL + tenantUser tokenverified groupsFiltered searchACL in the queryMark as datanever as instructionsAudit logwho saw which chunksIngestionQueryACL travels with every chunk
Permissions are enforced inside the search, from identity your server verified, never by asking the model to behave.

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.

Permission-aware retrievalQuery: “What is the salary band for a senior engineer?” 12 documents, each with the groups allowed to read it. Top 5 go to the model.

Intern is in all-staff · chunks sent to the model 5 · restricted chunks in the context 5

ScoreDocumentReadable by
0.94Compensation bands 2026 (confidential)hrLEAKED to context
0.91Salary review notes: engineeringhr, eng-managersLEAKED to context
0.87Offer letter templates with salary rangeshrLEAKED to context
0.85Engineering levels: Senior Engineer expectationsengineeringLEAKED to context
0.83Payroll export, Octoberhr, financeLEAKED to context
0.80Career framework overviewall-staff
0.78Promotion process for engineersengineering
0.74Total rewards: benefits at a glanceall-staff
0.72How pay reviews work (public FAQ)all-staff
0.66Interview rubric: senior backendengineering
0.31Holiday calendar 2026all-staff
0.29Quarterly budget forecastfinance
Guided experiments
  1. 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?
  2. 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.
  3. 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?
  4. 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?
Common misconception: “We tell the model not to reveal restricted documents.” If a restricted chunk is in the context, one prompt injection or one slip leaks it. Permissions belong in the query, not the prompt.

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.

In production: Put the tenant filter in one function that every search must call, and make the test suite fail if any code path searches without it.

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 code

Labs

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

Further reading

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

1. Where should the user’s groups for the permission filter come from?
2. Filtering the top 5 after search returns nothing for an intern. Fix?
3. A document’s ACL changes. What design avoids re-indexing every chunk?
4. Why store the source document id on every chunk?
5. A markdown image in a retrieved chunk points to an unknown domain with data in the URL. Risk?

Interview questions

  1. How do you enforce document permissions in a RAG system? Where do they come from?
  2. Compare separate collections per tenant with a shared collection and a tenant filter.
  3. How do you test that no user can see another tenant’s data?
  4. How do you handle a right-to-erasure request for a RAG index?
  5. 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).