Why retrieval-time access control beats post-filtering in RAG
By Amine Zaghdoudi, CTO and co-founder, Ragirag ·
If a user may not read a document, the language model must never receive its text on that user’s behalf. The only reliable place to enforce that is inside retrieval, before ranking, with a second check before the context is built.
Three places you could enforce permissions
A retrieval-augmented system answers a question in three stages: it retrieves candidate chunks, it builds a context from the best ones, and it asks a language model to write an answer. Document-level permissions can, in principle, be applied at any of these stages.
- After generation: let the model answer, then hide or redact citations from documents the user may not see.
- After retrieval: retrieve the top results, then drop the chunks from restricted documents before building the context.
- During retrieval: exclude restricted documents from the search itself, so they are never candidates.
Why filtering after generation leaks
Hiding citations after the model has answered is the most common mistake because it looks correct in the interface. The restricted document is not listed as a source, so reviewers assume it was not used. But the model already read it. Its content can appear in the answer as a paraphrase, a number, or a confident summary, and the user has no way to know where it came from.
Redacting the answer text does not solve this either. You cannot reliably detect every paraphrase of a confidential paragraph, and an answer with holes in it tells the reader that something sensitive exists.
Why filtering after retrieval degrades answers
Dropping restricted chunks after retrieval is safer: the model does not see them. It has a quality cost, though. Suppose you retrieve the top ten chunks and seven come from a confidential document the user cannot access. After filtering, the model gets three chunks, perhaps weak ones, and writes a thin answer, even though dozens of relevant, accessible chunks were ranked just below the cut-off.
Teams then raise the retrieval limit to compensate, which increases cost and latency for every query, and still fails whenever restricted documents dominate a topic.
Enforce it inside retrieval
The robust approach is to compute, for each question, the set of documents the user may not access, and pass it to the retrieval engine as an exclusion. The vector store and any keyword index apply it as a filter before ranking. The top results are then the best accessible chunks, and the model never sees restricted text.
In Ragirag the access rules live in the relational database, which is the single source of truth: a document is confidential or not, and a confidential document has explicit grants to named users. Team admins see every document in their team. For each question, the application computes the user’s excluded document identifiers and sends them with the query. The retrieval engine applies them to the vector search and to the BM25 keyword search before fusing the two rankings.
Keep permissions out of the vector index
It is tempting to store allowed user lists in each chunk’s metadata and filter on that. It works until permissions change. Granting a colleague access to a 400-page contract would require rewriting the metadata of every chunk in it, and a failure halfway leaves the index inconsistent with the database.
Storing only the document identifier in chunk metadata, and computing exclusions from the database at query time, avoids that. Grants and revocations take effect on the very next question, with no re-indexing.
Add defence in depth
Retrieval-time filtering should be the main control, not the only one. Two cheap extra checks catch bugs before they reach a user.
- Before building the context, verify again that no remaining chunk belongs to an excluded document, and drop it if one does.
- Before returning sources to the browser, check each cited document against the user’s access again in the application layer.
- Make restricted documents invisible everywhere else too: document lists, search suggestions and direct links should behave as if the document does not exist, typically by returning 404 rather than 403.
How to test it
Write a test that uploads a confidential document containing a distinctive phrase, asks a question that should retrieve it as a user without a grant, and asserts that the phrase appears neither in the context sent to the model nor in the sources returned. Then grant access and assert that the same question now retrieves it. Run the test on every change to the retrieval code.
Summary
Post-filtering either leaks content or starves the model of context. Enforcing access inside retrieval avoids both: compute exclusions from your permission store for every question, apply them to every index before ranking, keep permissions out of chunk metadata, and back the main control with checks before the context is built and before sources are shown.
