IamBishal
AuthorAI / LLMLibraries

Permission-aware RAG

RAG that keeps document permissions: no query returns a chunk the user cannot open at the source.

Syncs documents and their access permissions from Google Drive, Confluence and GitHub models and enforces them inside Postgres with row-level security, proven by property-based leak tests.

Amber cover for permission-aware-rag: "No query returns a chunk the user cannot open at the source." beside two parag peek runs where Alice and Dave see different chunks.
Your role
Author
Language
Python
License
MIT
Status
Active

Overview

Why it exists

Most RAG systems copy company documents into a vector store and lose the permissions on the way. permission-aware-rag syncs documents and their access permissions, and enforces them inside Postgres with row-level security, so a query can only ever see chunks the asking user may open in the source system.

Every chunk stores the principals allowed to read it: users, groups, domains, public and repository teams. A user is expanded into every principal they hold through nested groups, and each query runs in a read-only transaction that tells Postgres those principals. Even a query with no WHERE clause cannot see past the policy, and the query connection is checked on connect so it cannot bypass row-level security.

Search takes one of two paths, both under row-level security: exact for readers who can see little, so recall stays complete, and HNSW for wide readers. A background worker keeps documents current from each source's change feed, identity poll and a periodic reconcile; a source that falls behind is hidden until it catches up, and an expired share stops working at its expiry by the database clock.

The sources are faithful fakes of Google Drive, Confluence and GitHub, each with an oracle that answers "can this person open this?" from the product's native rules. Property-based leak tests run 10,000 random sequences of permission changes in CI and check every step against the oracles, and the time a revocation takes to reach search is measured per source and kind of change. Embeddings run on a local model by default, so no API key is needed.

Topics

  • Secure RAG
  • AI security
  • PostgreSQL

Features

What it can do

  • Permissions enforced inside Postgres with row-level security: even an unfiltered query sees only allowed chunks
  • Principals expanded through nested groups, with cycles handled safely
  • Two search paths under the same policy: exact for narrow readers (full recall) and HNSW for wide ones
  • Fail closed: a source that falls behind on any sync pass is hidden until it catches up
  • Expiring shares stop working at their expiry, by the database clock, without waiting for a sync
  • Sync engine with change feeds, identity polls and reconciles on a schedule, on the same Postgres
  • Leak tests against independent oracles: every person, every document, both search paths
  • Local embedding model by default (granite-embedding-small-english-r2); no API key needed
  • CLI (parag ask, peek, principals) and a query API with citations

Quick start

Up and running

  1. Start Postgres and migrate

    Needs Docker and uv.

    Terminal
    git clone https://github.com/bishal-saha/permission-aware-rag.git && cd permission-aware-rag
    docker compose -f deploy/docker-compose.yml up -d --wait
    set -a; . ./.env.example; set +a
    uv run parag db upgrade
    uv run parag db doctor
  2. Seed the demo and look as two people

    peek runs a deliberately unfiltered query; row-level security decides what comes back.

    Terminal
    uv run parag demo seed
    uv run parag peek --as alice@kestrel.example
    uv run parag peek --as dave@contractor.example
  3. Ask a question as a user

    The local embedding model downloads on first use.

    Terminal
    uv sync --extra local
    uv run --extra local parag demo synth --reset --check
    uv run --extra local parag ask "Who has to approve a big refund?" --as rahul@kestrel.example
  4. Run the leak test

    10,000 random sequences of permission changes, one shard per core.

    Terminal
    make leak-test

Tech stack

Built with

Language
  • Python 3.12
Database
  • PostgreSQL 18
  • row-level security
  • pgvector
Search
  • exact scan
  • HNSW
Embeddings
  • granite-embedding-small-english-r2
  • Voyage (optional)
Sync
  • Procrastinate worker
  • change feeds
  • reconcile
Testing
  • Hypothesis
  • pytest
  • source oracles

Screens

A look inside

01Same corpus, answers scoped to the asker
02Permissions enforced inside Postgres
030 leaks, checked against source rules

Bishal SahaLet's build together

Have a system to design, or an AI feature to get right?

Tell me about the problem. You'll get a clear, honest view of the best way forward.

  • Reply within 24 hoursOn business days, with my honest view and a next step.
  • Accepting select engagementsArchitecture, AI features and full builds.
  • Remote, worldwideWorking with startups, SMEs and enterprises.