Area 06 · AI Security · Concept 17 of 20

Access Control in RAG

Users only retrieve documents they are allowed to see. Filter at search time.

11–20 · Production levelPhase 7 · Weeks 24–26≈ 1 hour

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.

Why it matters

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

  1. Imagine your RAG over a company drive.
  2. Write three questions an intern should not get answers to.
  3. Check your design blocks each one before retrieval.

Use it in code

Permission-filtered vector search.

python
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()
PRACTICE · TICK IT AFTER THIS

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.

Go deeper