Retrieval That Respects Permissions: Filter Before You Rank
Most RAG tutorials end when the right paragraph comes back. In a company, that's where the real work starts, because the right paragraph is sometimes one the person asking isn't allowed to read. The usual failure isn't
Most RAG tutorials end when the right paragraph comes back. In a company, that's where the real work starts, because the right paragraph is sometimes one the person asking isn't allowed to read.
The usual failure isn't dramatic. A support agent searches for "refund policy" and the index returns a page from a finance document that was never meant for support. Nobody notices until someone asks why the answer mentioned an internal margin.
Why post-filtering is the wrong order
The common shortcut is to retrieve the top results first and then remove the ones the user can't see. It looks harmless, but it breaks in two ways.
First, you can end up with an empty or thin result set. If the top five hits were all restricted, filtering leaves nothing, even though plenty of allowed documents would have answered the question.
Second, anything that touches the restricted content before filtering can leak it. A reranker, a summarizer or a log line that holds the raw hits has already seen text the user shouldn't.
The safer order is to filter first, rank second. Only documents the user could open in the normal tool ever enter the pipeline.
If you're still building the retrieval layer itself, this walkthrough on building a RAG chatbot for company documents covers chunking, prompts and evaluation in more depth than I can here. The permission pattern in this post sits on top of whichever stack you choose.
The pattern
Every document carries an access list when it's indexed, usually groups copied from the source system. At query time, the search takes the user's groups, drops everything outside them, and only then scores what remains.
I ran this small version in Node before pasting it:
const DOCS = [
{ id: "d1", text: "refund policy for enterprise contracts", acl: ["finance", "support"] },
{ id: "d2", text: "salary bands for engineering", acl: ["hr"] },
{ id: "d3", text: "refund escalation steps", acl: ["support"] },
];
function search(query, user) {
const words = query.toLowerCase().split(/\s+/);
return DOCS.filter((d) => d.acl.some((g) => user.groups.includes(g)))
.map((d) => ({ id: d.id, score: words.filter((w) => d.text.includes(w)).length }))
.filter((r) => r.score > 0)
.sort((a, b) => b.score - a.score);
}
console.log(search("refund", { groups: ["support"] }));
// [ { id: 'd1', score: 1 }, { id: 'd3', score: 1 } ]
console.log(search("salary", { groups: ["support"] }));
// []
console.log(search("salary", { groups: ["hr"] }));
// [ { id: 'd2', score: 1 } ]
The scoring here is a toy. In a real system it's a vector search, and the filter becomes a metadata condition on the query. The order is what matters: the filter happens before anything gets scored.
Where the access list comes from
This is the part that takes real effort. The index should mirror the permissions of the source system, not invent its own. If a document moves from a restricted folder to a shared one, the index needs to follow.
I'd sync permissions on a schedule and also re-check them at query time for anything sensitive. A stale access list is a quiet kind of bug, because nothing fails. The wrong person just reads the wrong page.
What to test
Three tests cover most of it. A user in the right group finds the document. A user outside the group doesn't, even if the query matches the text exactly. And a document with no access list is treated as restricted by default, not open.
That last one has saved me from a few unpleasant surprises. Missing metadata should mean "closed", never "open".
I'd also add a regression test whenever you find a leak in the wild. Write the exact query and user that exposed the page, run it in CI, and keep it forever. A permissions bug that comes back after a refactor is far more embarrassing than the first one, and a single saved test makes sure it can't.
Keeping it honest in production
Log every query with the user's groups and the document IDs that came back. When someone asks why an answer included a certain page, you want to answer from the log, not from memory. I'd also alert on queries that return nothing for a user who normally gets results, because that pattern often means a permission sync broke.
How are you carrying source-system permissions into your index? I'm curious whether anyone has found a clean way to handle documents that move between folders.
Disclosure: I advise teams on rollouts like this at AI Hive.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.