The idea
Company documents have permissions: HR files, board papers, customer contracts. A RAG system that indexes everything into one pool and answers anyone will happily leak salaries. Access control must be applied in the retrieval query, so forbidden chunks never reach the prompt.
Copy each document’s permissions (owner, groups, tenant) onto its chunks at indexing time, resolve the current user’s groups at query time, and filter by them in the vector search. Keep permissions in sync when they change in the source system, and re-check at answer time for very sensitive data.
Test with users of different roles, including someone who should see nothing.
It is the most common serious security bug in enterprise RAG deployments.
Words to know
- ACLAccess control list: who can access a resource.
Try it without code
Ask as the intern
- Imagine your RAG over a company drive.
- Write three questions an intern should not get answers to.
- Check your design blocks each one before retrieval.
Use it in code
Permission-filtered vector search.
def search_for_user(conn, qvec, user, k=5):
groups = list(user.groups) + [f"user:{user.id}"]
return conn.execute("""
SELECT body, doc_id, embedding <=> %(q)s AS dist
FROM chunks
WHERE tenant_id = %(tenant)s
AND allowed_principals && %(groups)s::text[] -- array overlap
ORDER BY dist LIMIT %(k)s""",
{"q": qvec, "tenant": user.tenant_id, "groups": groups, "k": k}).fetchall()Add allowed_principals to your chunks, index documents with different permissions, and write tests for three user roles.
Common mistakes
- Permissions copied once and never synced.
- Filtering after retrieval in application code.