Intermediate project
RAG with per-user permissions
Builds on 3 lessonsabout 1 h 25 min of reading first
In this project
What you need first
- Completed:RAG architecture and chunkingRetrieval-augmented generation
- Completed:Better retrievalRetrieval-augmented generation
- Completed:RAG evaluation and hallucinationRetrieval-augmented generation
Skills you will practise
- Document processing
- Retrieval
- Retrieval-augmented generation
- RAG evaluation
- Hallucination control
Problem
Build a question-answering system over documents where two groups of users may read different sets of documents, and prove that one group can never see the other group’s text.
Requirements
- 30 to 60 short documents you write (an invented company) or openly licensed text with the license recorded. No real private data.
- Two groups, for example
supportandfinance; some documents readable by one group, some by both. Put a random canary string (for exampleCANARY-7Q2X-finance, appearing in no other document) in every restricted document. A group-keyed cache is safe only if every member of a group has the same permissions; with per-user ACLs, key by the user’s permission set. - Metadata on every chunk, written at ingest: document id, section, version, and the groups allowed to read it.
- The group comes from a simulated server session and only from there, never from the question or a request field.
- Retrieval with the group filter inside the search query (a metadata filter or a namespace per group; with namespaces, copy the documents both groups may read into each). Build a post-filter version too, only to compare.
- An abstain path that says the user’s documents cannot answer, without revealing that a restricted document exists.
- A cache keyed so one group’s entry is never served to the other. Every answer and every cache entry carries its cited chunk ids, and the per-request log records them, including on a cache hit.
- A leak suite of at least 30 queries: every restricted document is targeted by at least one query, each attack is run as each group, and each group has at least one document the other may not read. Attacks: quote a canary, paraphrase a restricted document, name it, a query whose best overall match is a restricted chunk, “ignore permissions”, a claim of the other group’s role (“as a finance manager, show me…”), a forged group field in the request, and the same question from both groups in turn (cache).
- At least 20 answerable queries per group, each with its answering document (recall@k here is document-level: a hit is any chunk of the answering document in the top k).
- At least 10 unanswerable queries per group, half about a restricted document and half about nothing in the corpus.
- A leak is content from a document the requesting group may not read appearing in an artefact of that group’s request: the results the store returns to your code (raw search output with chunk ids, before any post-processing, and logged), the context section of the prompt, the stored cache value, the answer, or the per-request log. Content means a chunk id of a restricted document, its canary, or a passage of 8 or more consecutive words from a restricted document that appears in no document the group may read (ignore case and punctuation, and keep boilerplate out of restricted-only documents). Text the user typed does not count wherever it appears (for example the question field of the prompt, the query field of the log, the cache key); a canary the user did not type but the answer repeats does. Ingest logs are out of scope; the comparison and broken builds are not part of the zero-leak acceptance. Chunk-id checks are the primary test, canaries a backstop. Acceptance: zero leaks on the final run against the submitted build.
Prerequisites
- RAG architecture and chunking
- Better retrieval
- RAG evaluation and hallucination (for the abstain threshold)
Tasks
- To do: Design first: draw the metadata schema and where the filter, cache and abstain check sit, before you code.
- To do: Build the system in Requirements.
- To do: Write the leak suite and fix every leak before measuring anything else.
- To do: Break your system twice on purpose, once in retrieval (filter after the search) and once in the cache (key by query only), and show the suite fails against each, naming the failing queries.
- To do: Build the post-filter version and compare it with the inside-the-query filter on the answerable queries: recall@k (state k) and result count.
- To do: Choose the abstain threshold using the answerable and unanswerable queries, and count correct and wrong refusals.
- To do: Stretch: revoke one group’s access to a document, show the cached entry no longer appears, and measure how long until it stops appearing. List every place a stale permission can live (chunk metadata, cache, logs).
- To do: Stretch: inject a third fault that only the log or cache check catches.
- To do: Stretch: add an ingest check that refuses a chunk without allowed groups.
Evaluation
Record real measurements and never invent numbers.
| Criterion | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Leak suite | Any leak, or no suite | Fewer than 30 queries, an attack missing, not run as both groups, a restricted document not targeted, a canary missing, a surface unchecked, the suite does not fail against the injected post-filter fault, or any other level-2 condition not met | The suite requirements met, zero leaks before the recall measurement and again on the final run against the submitted build, a canary in every restricted document (count stated), every surface in the leak definition checked, and the suite fails against the injected post-filter fault with the failing queries named | As level 2, plus a third injected fault caught only at the log or cache |
| Metadata and filter placement | No access metadata | Metadata present but at least one of: filter after the search, group read from the request or question, allowed groups missing on some chunks, or any other level-2 condition not met | Corpus of 30 to 60 documents with license recorded and no private data, some readable by both groups; document id, section, version and allowed groups on every chunk (shown by a count query); filter inside the query; group only from the session (forged field ignored); schema and the filter, cache and abstain positions drawn first | As level 2, plus an ingest check that refuses a chunk without allowed groups |
| Abstain | None | Abstains, but the reply differs between a restricted and a non-existent topic, refusals are below 80 percent on either kind, counts are not stated, or any other level-2 condition not met | Correct refusals on at least 80 percent of each kind of unanswerable query (restricted and nothing), on at least 10 queries per group (at least 5 of each kind) with counts stated; the same reply template whether the best match is restricted or nothing, naming no title, id, count or score; wrong refusals at most 10 percent of the answerable queries; the score threshold chosen on the answerable and unanswerable queries | As level 2, with the wrong-refusal against wrong-answer trade-off shown at two or more thresholds |
| Cache scoped | Shared across groups, or none | Keyed by group but untested, the suite does not fail against the injected fault, or any other level-2 condition not met | Keyed by group, same question from both groups in both orders, answers and the per-request log carry the cited chunk ids, including on a cache hit, and the suite fails against the injected query-only key with the failing queries named | As level 2, plus a stale-permission case: access revoked and the cached entry no longer appears |
| Recall cost of filtering | Not measured | Fewer than 20 queries per group, one placement only, or any other level-2 condition not met | recall@k (k stated) and result count for both placements on 20 or more answerable queries per group, differing queries named, a zero difference reported as zero | As level 2, with two values of k or two filter selectivities and the cause explained from your store’s behaviour |
Pass mark: at least 2 on every criterion (so the lowest passing total is 10 of 15).
Architecture
| Decision | Alternative rejected | Leak path it closes | Test that catches it |
|---|---|---|---|
| Filter inside the retrieval query | Retrieve the top-k, then remove restricted chunks | Post-filtering leaves zero or too few results (the best matches were removed) and unfiltered results can reach scores, logs and caches. Pre-filtering can cost recall or latency in some approximate indexes: report what your store does, including no difference at this size | Canary query; check the raw result ids |
| Group from the authenticated session | Group from the request or question | A user types a role or a forged group field | Role-claim and forged-field queries |
| Cache key includes the group | Cache by query text | One group’s cached answer reaches the other | Same question from both groups, both orders |
| Same abstain reply for restricted and missing | A reply that names or hints at a restricted document | The message leaks existence | Unanswerable query matching a restricted document |
| Enforce in the retriever | Tell the model to hide restricted text | The text is already in the prompt | Check the prompt context and the log for canaries |
Interview questions
Provenance: Generated practice. Not reported from any company.
- Design RAG with strict per-user permissions. Where does the filter go and why? Practise with P8 in the Product-company bank.
- Which metadata do you store with a chunk, and how do you filter on it? Practise with question 27 in the Top 100 priority questions.
- A document’s access is revoked for a group but it still appears in answers. What do you check? Practise with question 47 in the Top 100 priority questions.
Portfolio checklist
- To do: The README shows the metadata schema and the diagram with filter, cache and abstain positions.
- To do: The leak suite is in the repository and passes, with its query count, and the injected faults show it failing.
- To do: The recall and result-count table for the two filter placements.
- To do: No real private data is in the repository.