In this piece
Permission-Aware RAG: Keeping Private Data Private
A retrieval-augmented generation (RAG) assistant can only leak what it retrieves. If the search step ignores who is asking, an intern’s question can pull a salary spreadsheet into the model’s context, and from there into the answer. RAG access control has to happen at retrieval, not in the prompt: every chunk in the index carries the access list of its source document, every query is filtered by the user’s tenant and groups before the model sees any text, tenants are kept apart in storage, and every retrieval is logged. This guide shows how to build each of those four layers, with Postgres and pgvector examples you can adapt to any vector store, managed or self-hosted. Get these right before you tune answer quality, because a very good answer built from the wrong document is still a leak. It is how we approach RAG development for products that hold other people’s data.
We reviewed the external references on 19 November 2026. The SQL is a starting point: test it against your own roles and data before you trust it.
Building an assistant on top of private documents?
Scope your RAG systemWhy does RAG leak private data?
Most first versions of a RAG system are built the same way: export every document, split it into chunks, embed the chunks and put them in one index. The source system’s permissions, the sharing settings in Google Drive, SharePoint, Confluence or your own database, are lost at the export step. From then on the index answers every user with the same pool of text.
OWASP lists this as LLM08:2025 Vector and Embedding Weaknesses: weak access control on embeddings, and context leaking between users or groups that share a vector database. Its recommended fix is a permission-aware vector store with data partitioned by access. Two common shortcuts do not meet that bar:
- Telling the model not to reveal restricted content. Once a chunk is in the context window, a prompt injection or a cleverly worded question can get it out. Instructions are not permissions.
- Filtering after retrieval. Retrieving the top 8 chunks and then dropping the ones the user cannot read leaks through side channels (counts, citations, latency) and often leaves the user with nothing useful, because the 8 best matches were someone else’s documents.
How do you store document permissions in the index?
Treat the access list as part of the chunk, not as something you look up later. At ingestion, read the document’s permissions from the source system and copy them onto every chunk: the tenant, the groups or users allowed to read it, a classification label, and the time the permissions were last synced. Use group IDs rather than names, and resolve nested groups at sync time so the query only has to compare flat lists.
create table chunks (
id bigserial primary key,
tenant_id uuid not null,
doc_id text not null, -- id in the source system
allowed_groups text[] not null, -- copied from the source ACL
classification text not null default 'internal',
acl_synced_at timestamptz not null,
content text not null,
embedding vector(1536)
);
create index on chunks using hnsw (embedding vector_cosine_ops);
create index on chunks (tenant_id);
create index on chunks using gin (allowed_groups);The hard part is keeping these lists current. Subscribe to change events where the source offers them, run a frequent permission-only sync where it does not, and treat a revoked share as urgent: removing someone’s access should take effect in minutes, not at the next full re-index. For the most sensitive classes of document, add a second check against the source system’s API at query time before a chunk is used.
How do you filter by permission at query time?
Put the filter inside the search itself, so the nearest-neighbor search only ever ranks chunks the user may read. Most vector databases support metadata filters on a query; in Postgres you can go one step further and let the database enforce it with row security policies, so a forgotten where clause cannot turn into a leak.
alter table chunks enable row level security;
alter table chunks force row level security;
create policy chunks_read on chunks for select using (
tenant_id = current_setting('app.tenant_id')::uuid
and allowed_groups && string_to_array(current_setting('app.groups'), ',')
);Two details matter. Superusers and roles with BYPASSRLS skip these policies, and table owners skip them unless you force row level security, so the application must connect as a separate role with no bypass rights. And the tenant and groups must come from your verified session or token, never from anything the user or the model can type.
begin;
select set_config('app.tenant_id', $1, true); -- from the verified session
select set_config('app.groups', $2, true); -- e.g. 'all-staff,finance'
set local hnsw.iterative_scan = relaxed_order; -- pgvector 0.8.0+
select doc_id, content
from chunks
order by embedding <=> $3
limit 8;
commit;Filtering also changes search quality. The pgvector documentation notes that with approximate indexes the filter is applied after the index is scanned, so a selective filter can return fewer rows than you asked for. Version 0.8.0 added iterative index scans to fix that; on other stores, check how filtered search behaves before you rely on it.
| Approach | Where access is enforced | Leak risk | Use it when |
|---|---|---|---|
| Prompt instruction only | The model | High | Never as a control |
| Post-filter after top-k | Application, after search | Medium; poor recall | Only as a second check |
| Metadata filter in the query | Vector store query | Low, if every code path sets it | Managed vector databases |
| Row level security policy | Database, on every query | Low | Postgres and pgvector |
| Separate index per tenant | Storage boundary | Lowest | Large or regulated tenants |
How do you isolate tenants in a shared RAG system?
Tenant isolation is the boundary that must never fail, because a leak between companies is far worse than a leak between colleagues. Pick the strength of the boundary per tenant:
- Shared index, mandatory tenant filter. Cheapest and fine for many small tenants, provided the filter is enforced by the database or a single retrieval function that every caller must use.
- Namespace, collection or schema per tenant. A stronger boundary that also makes deleting a tenant’s data simple and provable.
- Separate database or account. For enterprise or regulated customers who ask where their data lives and who holds the keys.
Isolation is wider than the index. Caches keyed only on the question text, shared conversation memory, evaluation datasets built from production logs and fine-tuning data can all carry one tenant’s text to another. Key every cache by tenant and user, and keep tenant data out of shared prompts and examples.

"If the only thing between a user and a confidential document is a sentence in the system prompt, the document is not protected. Permissions belong in the query, where the database can enforce them."
What should a RAG audit log record?
When a customer asks who saw what, the answer should come from a log, not from a guess. OWASP recommends detailed, immutable logs of retrieval activity, and they also make debugging bad answers much faster. Record one entry per question:
{
"ts": "2027-01-11T09:42:17Z",
"tenant_id": "t_4821",
"user_id": "u_193",
"groups": ["all-staff", "finance"],
"query_hash": "sha256:9f2c...",
"retrieved": [
{ "doc_id": "drive:1a7Q", "chunk": 14, "classification": "confidential" },
{ "doc_id": "wiki:payroll-faq", "chunk": 2, "classification": "internal" }
],
"acl_synced_at_oldest": "2027-01-11T09:30:02Z",
"model": "<model id>",
"answer_id": "a_77d0"
}Store a hash of the question rather than the text if questions may contain personal data, keep the log in append-only storage with its own retention period, and alert on unusual patterns, such as one user retrieving many confidential chunks in a short time. Before launch, test the whole chain with two users in two tenants: sign in as each, ask questions whose answers live only in the other’s documents, and confirm that nothing comes back and that the log shows why.
The mistakes we look for first when we review an existing RAG system:
- One shared index with no tenant column at all, because the first customer was the only customer.
- A tenant filter that is applied in the chat endpoint but not in the background jobs, exports or evaluation scripts that query the same index.
- Group names taken from a request parameter or from text the model produced, instead of the signed token.
- Permission syncs that only run during a full re-index, so a revoked share stays searchable for days.
- Citations and source links shown to the user before the permission check, revealing that a document exists.
- Answer caches keyed on the question text alone, so one user’s answer is served to the next.
At Devoun, our RAG development work starts with exactly this: mapping the source permissions, choosing the tenant boundary and writing the cross-tenant tests before the first answer is tuned. It is delivered in fixed-price milestones covered by our money-back guarantee.
Frequently Asked Questions
What is permission-aware RAG?
It is retrieval-augmented generation where the search step only returns chunks the current user is allowed to read. Permissions from the source system travel with every chunk into the index, and every query is filtered by the user’s identity before any text reaches the model.
Can I tell the LLM not to reveal documents the user cannot see?
No. A prompt instruction is not a security control: prompt injection or a cleverly phrased question can get around it. If a restricted chunk reaches the model’s context, assume it can reach the user. Filter before retrieval instead.
Should each customer get a separate vector index?
For a few large or regulated customers, a separate index, namespace or database is the simplest strong boundary. For many small tenants, a shared index with a mandatory tenant filter enforced in the database is usually enough, as long as it is tested.
How do permission changes reach the index?
Sync access lists from the source system on every change event, or on a short schedule if events are not available, and re-check permissions against the source for sensitive documents at query time. Removing access should take effect in minutes, not at the next full re-index.
Final thoughts
A RAG system inherits the trust your users place in the systems it reads from. Keep the permissions with the data, filter before the model sees anything, draw the tenant boundary deliberately and log every retrieval. Then the assistant can be useful without becoming the easiest way to read documents nobody shared with you.